Skip to content

Commit

Permalink
use rand32 etc
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Jan 29, 2023
1 parent 3b4f772 commit b400b12
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
12 changes: 7 additions & 5 deletions docs/src/tutorials/linear_regression.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ Let's start by initializing our dataset. We will be using the [`BostonHousing`](
julia> dataset = BostonHousing();
julia> x, y = BostonHousing(as_df=false)[:];
julia> x, y = Float32.(x), Float32.(y)
```

We can now split the obtained data into training and testing data -
Expand All @@ -287,7 +289,7 @@ This data contains a diverse number of features, which means that the features h

```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
julia> std(x_train)
134.06784844377117
134.06786f0
```

The data is indeed not normalised. We can use the [`Flux.normalise`](@ref) function to normalise the training data.
Expand All @@ -296,7 +298,7 @@ The data is indeed not normalised. We can use the [`Flux.normalise`](@ref) funct
julia> x_train_n = Flux.normalise(x_train);
julia> std(x_train_n)
1.0000843694328236
1.0000844f0
```

The standard deviation is now close to one! Our data is ready!
Expand All @@ -318,7 +320,7 @@ julia> function loss(model, x, y)
end;
julia> loss(model, x_train_n, y_train)
676.165591625047
676.1656f0
```

We can now proceed to the training phase!
Expand Down Expand Up @@ -363,7 +365,7 @@ Let's have a look at the loss -

```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
julia> loss(model, x_train_n, y_train)
27.127200028562164
27.1272f0
```

The loss went down significantly! It can be minimized further by choosing an even smaller `δ`.
Expand All @@ -376,7 +378,7 @@ The last step of this tutorial would be to test our model using the testing data
julia> x_test_n = Flux.normalise(x_test);
julia> loss(model, x_test_n, y_test)
66.91014769713368
66.91015f0
```

The loss is not as small as the loss of the training data, but it looks good! This also shows that our model is not overfitting!
Expand Down
14 changes: 7 additions & 7 deletions src/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ true
julia> m = Chain(Dense(10 => 5, tanh), Dense(5 => 2));
julia> x = rand(10, 32);
julia> x = rand32(10, 32);
julia> m(x) == m[2](m[1](x))
true
Expand Down Expand Up @@ -132,11 +132,11 @@ The weight matrix and/or the bias vector (of length `out`) may also be provided
julia> d = Dense(5 => 2)
Dense(5 => 2) # 12 parameters
julia> d(rand(Float32, 5, 64)) |> size
julia> d(rand32(5, 64)) |> size
(2, 64)
julia> d(rand(Float32, 5, 1, 1, 64)) |> size # treated as three batch dimensions
(2, 1, 1, 64)
julia> d(rand32(5, 6, 4, 64)) |> size # treated as three batch dimensions
(2, 6, 4, 64)
julia> d1 = Dense(ones(2, 5), false, tanh) # using provided weight matrix
Dense(5 => 2, tanh; bias=false) # 10 parameters
Expand Down Expand Up @@ -476,7 +476,7 @@ julia> model = Chain(Dense(3 => 5),
Parallel(vcat, Dense(5 => 4), Chain(Dense(5 => 7), Dense(7 => 4))),
Dense(8 => 17));
julia> model(rand(3)) |> size
julia> model(rand32(3)) |> size
(17,)
julia> model2 = Parallel(+; α = Dense(10, 2, tanh), β = Dense(5, 2))
Expand All @@ -486,10 +486,10 @@ Parallel(
β = Dense(5 => 2), # 12 parameters
) # Total: 4 arrays, 34 parameters, 392 bytes.
julia> model2(rand(10), rand(5)) |> size
julia> model2(rand32(10), rand32(5)) |> size
(2,)
julia> model2[:α](rand(10)) |> size
julia> model2[:α](rand32(10)) |> size
(2,)
julia> model2[:β] == model2[2]
Expand Down
6 changes: 3 additions & 3 deletions src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ See also [`Conv`](@ref), [`MaxPool`](@ref).
# Examples
```jldoctest
julia> xs = rand(Float32, 100, 100, 3, 50); # a batch of images
julia> xs = rand32(100, 100, 3, 50); # a batch of images
julia> layer = Conv((2,2), 3 => 7, pad=SamePad())
Conv((2, 2), 3 => 7, pad=(1, 0, 1, 0)) # 91 parameters
Expand Down Expand Up @@ -96,7 +96,7 @@ See also [`ConvTranspose`](@ref), [`DepthwiseConv`](@ref), [`CrossCor`](@ref).
# Examples
```jldoctest
julia> xs = rand(Float32, 100, 100, 3, 50); # a batch of images
julia> xs = rand32(100, 100, 3, 50); # a batch of 50 RGB images
julia> layer = Conv((5,5), 3 => 7, relu; bias = false)
Conv((5, 5), 3 => 7, relu, bias=false) # 525 parameters
Expand Down Expand Up @@ -238,7 +238,7 @@ See also [`Conv`](@ref) for more detailed description of keywords.
# Examples
```jldoctest
julia> xs = rand(Float32, 100, 100, 3, 50); # a batch of 50 RGB images
julia> xs = rand32(100, 100, 3, 50); # a batch of 50 RGB images
julia> layer = ConvTranspose((5,5), 3 => 7, relu)
ConvTranspose((5, 5), 3 => 7, relu) # 532 parameters
Expand Down
2 changes: 1 addition & 1 deletion src/layers/normalise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Does nothing to the input once [`testmode!`](@ref) is true.
```jldoctest
julia> using Statistics
julia> x = randn(1000,1);
julia> x = randn32(1000,1);
julia> m = Chain(Dense(1000 => 1000, selu), AlphaDropout(0.2));
Expand Down
10 changes: 5 additions & 5 deletions src/train.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ It differs from `Optimisers.setup` in that it:
# Example
```jldoctest
julia> model = Dense(2=>1, leakyrelu; init=ones32);
julia> model = Dense(2=>1, leakyrelu; init=ones);
julia> opt_state = Flux.setup(Momentum(0.1), model) # this encodes the optimiser and its state
(weight = Leaf(Momentum{Float64}(0.1, 0.9), Float32[0.0 0.0]), bias = Leaf(Momentum{Float64}(0.1, 0.9), Float32[0.0]), σ = ())
(weight = Leaf(Momentum{Float64}(0.1, 0.9), [0.0 0.0]), bias = Leaf(Momentum{Float64}(0.1, 0.9), [0.0]), σ = ())
julia> x1, y1 = [0.2, -0.3], [0.4]; # use the same data for two steps:
Expand All @@ -39,11 +39,11 @@ julia> Flux.train!(model, [(x1, y1), (x1, y1)], opt_state) do m, x, y
end
julia> model.bias # was zero, mutated by Flux.train!
1-element Vector{Float32}:
10.190001
1-element Vector{Float64}:
10.19
julia> opt_state # mutated by Flux.train!
(weight = Leaf(Momentum{Float64}(0.1, 0.9), Float32[-2.018 3.027]), bias = Leaf(Momentum{Float64}(0.1, 0.9), Float32[-10.09]), σ = ())
(weight = Leaf(Momentum{Float64}(0.1, 0.9), [-2.018 3.027]), bias = Leaf(Momentum{Float64}(0.1, 0.9), [-10.09]), σ = ())
```
"""
function setup(rule::Optimisers.AbstractRule, model)
Expand Down

0 comments on commit b400b12

Please sign in to comment.