Skip to content

Commit a9f5c63

Browse files
committed
Add doctests for regilarisation.md
1 parent 903070f commit a9f5c63

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

docs/src/models/regularisation.md

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,79 @@ add the result to the overall loss.
66

77
For example, say we have a simple regression.
88

9-
```julia
10-
using Flux
11-
using Flux.Losses: logitcrossentropy
12-
m = Dense(10 => 5)
13-
loss(x, y) = logitcrossentropy(m(x), y)
9+
```jldoctest regularisation; setup = :(using Random; Random.seed!(0))
10+
julia> using Flux
11+
12+
julia> using Flux.Losses: logitcrossentropy
13+
14+
julia> m = Dense(10 => 5)
15+
Dense(10 => 5) # 55 parameters
16+
17+
julia> loss(x, y) = logitcrossentropy(m(x), y)
18+
loss (generic function with 1 method)
1419
```
1520

1621
We can apply L2 regularisation by taking the squared norm of the parameters , `m.weight` and `m.bias`.
1722

18-
```julia
19-
penalty() = sum(abs2, m.weight) + sum(abs2, m.bias)
20-
loss(x, y) = logitcrossentropy(m(x), y) + penalty()
23+
```jldoctest regularisation
24+
julia> penalty() = sum(abs2, m.weight) + sum(abs2, m.bias)
25+
penalty (generic function with 1 method)
26+
27+
julia> loss(x, y) = logitcrossentropy(m(x), y) + penalty()
28+
loss (generic function with 1 method)
2129
```
2230

2331
When working with layers, Flux provides the `params` function to grab all
2432
parameters at once. We can easily penalise everything with `sum`:
2533

26-
```julia
34+
```jldoctest regularisation
2735
julia> Flux.params(m)
28-
2-element Array{Any,1}:
29-
param([0.355408 0.533092; 0.430459 0.171498])
30-
param([0.0, 0.0, 0.0, 0.0, 0.0])
36+
Params([Float32[0.34704182 -0.48532376 … -0.06914271 -0.38398427; 0.5201164 -0.033709668 … -0.36169025 -0.5552353; … ; 0.46534058 0.17114447 … -0.4809643 0.04993277; -0.47049698 -0.6206029 … -0.3092334 -0.47857067], Float32[0.0, 0.0, 0.0, 0.0, 0.0]])
3137
3238
julia> sqnorm(x) = sum(abs2, x)
39+
sqnorm (generic function with 1 method)
3340
3441
julia> sum(sqnorm, Flux.params(m))
35-
26.01749952921026
42+
8.34994f0
3643
```
3744

3845
Here's a larger example with a multi-layer perceptron.
3946

40-
```julia
41-
m = Chain(
42-
Dense(28^2 => 128, relu),
43-
Dense(128 => 32, relu),
44-
Dense(32 => 10))
47+
```jldoctest regularisation
48+
julia> m = Chain(Dense(28^2 => 128, relu), Dense(128 => 32, relu), Dense(32 => 10))
49+
Chain(
50+
Dense(784 => 128, relu), # 100_480 parameters
51+
Dense(128 => 32, relu), # 4_128 parameters
52+
Dense(32 => 10), # 330 parameters
53+
) # Total: 6 arrays, 104_938 parameters, 410.289 KiB.
4554
46-
sqnorm(x) = sum(abs2, x)
55+
julia> sqnorm(x) = sum(abs2, x)
56+
sqnorm (generic function with 1 method)
4757
48-
loss(x, y) = logitcrossentropy(m(x), y) + sum(sqnorm, Flux.params(m))
58+
julia> loss(x, y) = logitcrossentropy(m(x), y) + sum(sqnorm, Flux.params(m))
59+
loss (generic function with 1 method)
4960
50-
loss(rand(28^2), rand(10))
61+
julia> loss(rand(28^2), rand(10))
62+
300.76693683244997
5163
```
5264

5365
One can also easily add per-layer regularisation via the `activations` function:
5466

55-
```julia
67+
```jldoctest regularisation
5668
julia> using Flux: activations
5769
5870
julia> c = Chain(Dense(10 => 5, σ), Dense(5 => 2), softmax)
59-
Chain(Dense(10 => 5, σ), Dense(5 => 2), softmax)
71+
Chain(
72+
Dense(10 => 5, σ), # 55 parameters
73+
Dense(5 => 2), # 12 parameters
74+
NNlib.softmax,
75+
) # Total: 4 arrays, 67 parameters, 524 bytes.
6076
6177
julia> activations(c, rand(10))
62-
3-element Array{Any,1}:
63-
Float32[0.84682214, 0.6704139, 0.42177814, 0.257832, 0.36255655]
64-
Float32[0.1501253, 0.073269576]
65-
Float32[0.5192045, 0.48079553]
78+
([0.3274892431795043, 0.5360197770386552, 0.3447464835514667, 0.5273025865532305, 0.7513168089280781], [-0.3533774181890544, -0.010937055274926138], [0.4152168057978045, 0.5847831942021956])
6679
6780
julia> sum(sqnorm, ans)
68-
2.0710278f0
81+
1.9953131077618562
6982
```
7083

7184
```@docs

0 commit comments

Comments
 (0)