Skip to content

Commit 97f981b

Browse files
authored
Merge pull request #1995 from Saransh-cpp/docstring-for-layers
Update docstrings in `upsample.jl`, `recurrent.jl`, and `normalise.jl`
2 parents 952c4a5 + 2ab42cd commit 97f981b

File tree

4 files changed

+237
-28
lines changed

4 files changed

+237
-28
lines changed

docs/src/models/layers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Much like the core layers above, but can be used to process sequence data (as we
4242
RNN
4343
LSTM
4444
GRU
45+
GRUv3
4546
Flux.Recur
4647
Flux.reset!
4748
```
@@ -67,7 +68,6 @@ These layers don't affect the structure of the network but may improve training
6768
```@docs
6869
Flux.normalise
6970
BatchNorm
70-
Flux.dropout
7171
Dropout
7272
AlphaDropout
7373
LayerNorm

src/layers/normalise.jl

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ChainRulesCore.@non_differentiable dropout_mask(::Any, ::Any, ::Any)
5555
"""
5656
Dropout(p; dims=:, rng = rng_from_array())
5757
58-
Dropout layer. In the forward pass, apply the [`Flux.dropout`](@ref) function on the input.
58+
Dropout layer. In the forward pass, applies the [`Flux.dropout`](@ref) function on the input.
5959
6060
To apply dropout along certain dimension(s), specify the `dims` keyword.
6161
e.g. `Dropout(p; dims = 3)` will randomly zero out entire channels on WHCN input
@@ -65,6 +65,27 @@ Specify `rng` to use a custom RNG instead of the default.
6565
Custom RNGs are only supported on the CPU.
6666
6767
Does nothing to the input once [`Flux.testmode!`](@ref) is `true`.
68+
69+
# Examples
70+
```jldoctest
71+
julia> m = Chain(Dense(1 => 1), Dropout(1));
72+
73+
julia> Flux.trainmode!(m);
74+
75+
julia> y = m([1]);
76+
77+
julia> y == [0]
78+
true
79+
80+
julia> m = Chain(Dense(1000 => 1000), Dropout(0.5));
81+
82+
julia> Flux.trainmode!(m);
83+
84+
julia> y = m(ones(1000));
85+
86+
julia> isapprox(count(==(0), y) / length(y), 0.5, atol=0.1)
87+
true
88+
```
6889
"""
6990
mutable struct Dropout{F,D,R<:AbstractRNG}
7091
p::F
@@ -105,6 +126,22 @@ The AlphaDropout layer ensures that mean and variance of activations
105126
remain the same as before.
106127
107128
Does nothing to the input once [`testmode!`](@ref) is true.
129+
130+
# Examples
131+
```jldoctest
132+
julia> using Statistics
133+
134+
julia> x = randn(1000,1);
135+
136+
julia> m = Chain(Dense(1000 => 1000, selu), AlphaDropout(0.2));
137+
138+
julia> Flux.trainmode!(m);
139+
140+
julia> y = m(x);
141+
142+
julia> isapprox(std(x), std(y), atol=0.2)
143+
true
144+
```
108145
"""
109146
mutable struct AlphaDropout{F,R<:AbstractRNG}
110147
p::F
@@ -154,6 +191,20 @@ If `affine=true`, it also applies a learnable shift and rescaling
154191
using the [`Scale`](@ref) layer.
155192
156193
See also [`BatchNorm`](@ref), [`InstanceNorm`](@ref), [`GroupNorm`](@ref), and [`normalise`](@ref).
194+
195+
# Examples
196+
```jldoctest
197+
julia> using Statistics
198+
199+
julia> xs = rand(3, 3, 3, 2); # a batch of 2 images, each having 3 channels
200+
201+
julia> m = LayerNorm(3);
202+
203+
julia> y = m(xs);
204+
205+
julia> isapprox(std(y, dims=1:3), ones(1, 1, 1, 2), atol=0.1) && std(y, dims=1:3) != std(xs, dims=1:3)
206+
true
207+
```
157208
"""
158209
struct LayerNorm{F,D,T,N}
159210
λ::F
@@ -256,12 +307,16 @@ Use [`testmode!`](@ref) during inference.
256307
257308
# Examples
258309
```julia
259-
m = Chain(
260-
Dense(28^2 => 64),
261-
BatchNorm(64, relu),
262-
Dense(64 => 10),
263-
BatchNorm(10),
264-
softmax)
310+
julia> using Statistics
311+
312+
julia> xs = rand(3, 3, 3, 2); # a batch of 2 images, each having 3 channels
313+
314+
julia> m = BatchNorm(3);
315+
316+
julia> Flux.trainmode!(m);
317+
318+
julia> isapprox(std(m(xs)), 1, atol=0.1) && std(xs) != std(m(xs))
319+
true
265320
```
266321
"""
267322
mutable struct BatchNorm{F,V,N,W}
@@ -339,6 +394,20 @@ that will be used to renormalize the input in test phase.
339394
340395
**Warning**: the defaults for `affine` and `track_stats` used to be `true`
341396
in previous Flux versions (< v0.12).
397+
398+
# Examples
399+
```jldoctest
400+
julia> using Statistics
401+
402+
julia> xs = rand(3, 3, 3, 2); # a batch of 2 images, each having 3 channels
403+
404+
julia> m = InstanceNorm(3);
405+
406+
julia> y = m(xs);
407+
408+
julia> isapprox(std(y, dims=1:2), ones(1, 1, 3, 2), atol=0.2) && std(y, dims=1:2) != std(xs, dims=1:2)
409+
true
410+
```
342411
"""
343412
mutable struct InstanceNorm{F,V,N,W}
344413
λ::F # activation function
@@ -416,6 +485,23 @@ through to learnable per-channel bias `β` and scale `γ` parameters.
416485
417486
If `track_stats=true`, accumulates mean and var statistics in training phase
418487
that will be used to renormalize the input in test phase.
488+
489+
# Examples
490+
```jldoctest
491+
julia> using Statistics
492+
493+
julia> xs = rand(3, 3, 4, 2); # a batch of 2 images, each having 4 channels
494+
495+
julia> m = GroupNorm(4, 2);
496+
497+
julia> y = m(xs);
498+
499+
julia> isapprox(std(y[:, :, 1:2, 1]), 1, atol=0.1) && std(xs[:, :, 1:2, 1]) != std(y[:, :, 1:2, 1])
500+
true
501+
502+
julia> isapprox(std(y[:, :, 3:4, 2]), 1, atol=0.1) && std(xs[:, :, 3:4, 2]) != std(y[:, :, 3:4, 2])
503+
true
504+
```
419505
"""
420506
mutable struct GroupNorm{F,V,N,W}
421507
G::Int # number of groups

src/layers/recurrent.jl

Lines changed: 84 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,67 @@ in the background. `cell` should be a model of the form:
6363
6464
For example, here's a recurrent network that keeps a running total of its inputs:
6565
66-
```julia
67-
accum(h, x) = (h + x, x)
68-
rnn = Flux.Recur(accum, 0)
69-
rnn(2) # 2
70-
rnn(3) # 3
71-
rnn.state # 5
72-
rnn.(1:10) # apply to a sequence
73-
rnn.state # 60
66+
# Examples
67+
```jldoctest
68+
julia> accum(h, x) = (h + x, x)
69+
accum (generic function with 1 method)
70+
71+
julia> rnn = Flux.Recur(accum, 0)
72+
Recur(accum)
73+
74+
julia> rnn(2)
75+
2
76+
77+
julia> rnn(3)
78+
3
79+
80+
julia> rnn.state
81+
5
7482
```
7583
7684
Folding over a 3d Array of dimensions `(features, batch, time)` is also supported:
7785
78-
```julia
79-
accum(h, x) = (h .+ x, x)
80-
rnn = Flux.Recur(accum, zeros(Int, 1, 1))
81-
rnn([2]) # 2
82-
rnn([3]) # 3
83-
rnn.state # 5
84-
rnn(reshape(1:10, 1, 1, :)) # apply to a sequence of (features, batch, time)
85-
rnn.state # 60
86+
```jldoctest
87+
julia> accum(h, x) = (h .+ x, x)
88+
accum (generic function with 1 method)
89+
90+
julia> rnn = Flux.Recur(accum, zeros(Int, 1, 1))
91+
Recur(accum)
92+
93+
julia> rnn([2])
94+
1-element Vector{Int64}:
95+
2
96+
97+
julia> rnn([3])
98+
1-element Vector{Int64}:
99+
3
100+
101+
julia> rnn.state
102+
1×1 Matrix{Int64}:
103+
5
104+
105+
julia> out = rnn(reshape(1:10, 1, 1, :)); # apply to a sequence of (features, batch, time)
106+
107+
julia> out |> size
108+
(1, 1, 10)
109+
110+
julia> vec(out)
111+
10-element Vector{Int64}:
112+
1
113+
2
114+
3
115+
4
116+
5
117+
6
118+
7
119+
8
120+
9
121+
10
122+
123+
julia> rnn.state
124+
1×1 Matrix{Int64}:
125+
60
86126
```
87-
88127
"""
89128
mutable struct Recur{T,S}
90129
cell::T
@@ -107,8 +146,34 @@ Base.show(io::IO, m::Recur) = print(io, "Recur(", m.cell, ")")
107146
Reset the hidden state of a recurrent layer back to its original value.
108147
109148
Assuming you have a `Recur` layer `rnn`, this is roughly equivalent to:
110-
```julia
111-
rnn.state = hidden(rnn.cell)
149+
150+
rnn.state = hidden(rnn.cell)
151+
152+
# Examples
153+
```jldoctest
154+
julia> r = Flux.RNNCell(relu, ones(1,1), zeros(1,1), ones(1,1), zeros(1,1)); # users should use the RNN wrapper struct instead
155+
156+
julia> y = Flux.Recur(r, ones(1,1));
157+
158+
julia> y.state
159+
1×1 Matrix{Float64}:
160+
1.0
161+
162+
julia> y(ones(1,1)) # relu(1*1 + 1)
163+
1×1 Matrix{Float64}:
164+
2.0
165+
166+
julia> y.state
167+
1×1 Matrix{Float64}:
168+
2.0
169+
170+
julia> Flux.reset!(y)
171+
1×1 Matrix{Float64}:
172+
0.0
173+
174+
julia> y.state
175+
1×1 Matrix{Float64}:
176+
0.0
112177
```
113178
"""
114179
reset!(m::Recur) = (m.state = m.cell.state0)

src/layers/upsample.jl

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,67 @@ end
7575
"""
7676
PixelShuffle(r::Int)
7777
78-
Pixel shuffling layer with upscale factor `r`.
78+
Pixel shuffling layer with upscale factor `r`. Usually used for generating higher
79+
resolution images while upscaling them.
7980
8081
See [`NNlib.pixel_shuffle`](@ref).
82+
83+
# Examples
84+
```jldoctest
85+
julia> p = PixelShuffle(2);
86+
87+
julia> xs = [2row + col + channel/10 for row in 1:2, col in 1:2, channel in 1:4, n in 1:1]
88+
2×2×4×1 Array{Float64, 4}:
89+
[:, :, 1, 1] =
90+
3.1 4.1
91+
5.1 6.1
92+
93+
[:, :, 2, 1] =
94+
3.2 4.2
95+
5.2 6.2
96+
97+
[:, :, 3, 1] =
98+
3.3 4.3
99+
5.3 6.3
100+
101+
[:, :, 4, 1] =
102+
3.4 4.4
103+
5.4 6.4
104+
105+
julia> p(xs)
106+
4×4×1×1 Array{Float64, 4}:
107+
[:, :, 1, 1] =
108+
3.1 3.3 4.1 4.3
109+
3.2 3.4 4.2 4.4
110+
5.1 5.3 6.1 6.3
111+
5.2 5.4 6.2 6.4
112+
113+
julia> xs = [3row + col + channel/10 for row in 1:2, col in 1:3, channel in 1:4, n in 1:1]
114+
2×3×4×1 Array{Float64, 4}:
115+
[:, :, 1, 1] =
116+
4.1 5.1 6.1
117+
7.1 8.1 9.1
118+
119+
[:, :, 2, 1] =
120+
4.2 5.2 6.2
121+
7.2 8.2 9.2
122+
123+
[:, :, 3, 1] =
124+
4.3 5.3 6.3
125+
7.3 8.3 9.3
126+
127+
[:, :, 4, 1] =
128+
4.4 5.4 6.4
129+
7.4 8.4 9.4
130+
131+
julia> p(xs)
132+
4×6×1×1 Array{Float64, 4}:
133+
[:, :, 1, 1] =
134+
4.1 4.3 5.1 5.3 6.1 6.3
135+
4.2 4.4 5.2 5.4 6.2 6.4
136+
7.1 7.3 8.1 8.3 9.1 9.3
137+
7.2 7.4 8.2 8.4 9.2 9.4
138+
```
81139
"""
82140
struct PixelShuffle
83141
r::Int

0 commit comments

Comments
 (0)