@@ -67,32 +67,24 @@ Custom RNGs are only supported on the CPU.
6767Does nothing to the input once [`Flux.testmode!`](@ref) is `true`.
6868
6969# Examples
70- ```julia
71- julia> m = Chain(Dense(2 => 2), Dropout(1))
72- Chain(
73- Dense(2 => 2), # 6 parameters
74- Dropout(1),
75- )
70+ ```jldoctest
71+ julia> m = Chain(Dense(1 => 1), Dropout(1));
72+
73+ julia> Flux.trainmode!(m);
7674
77- julia> Flux.trainmode!(m); # activating the layer without actually training it
75+ julia> y = m([1]);
7876
79- julia> m([1, 2]) # drops neurons with a probability of 1
80- 2-element Vector{Float32}:
81- -0.0
82- -0.0
77+ julia> count(i->(i == 0), y) == m[2].p # number of zeros == 1
78+ true
8379
84- julia> m = Chain(Dense(2 => 2), Dropout(0.5))
85- Chain(
86- Dense(2 => 2), # 6 parameters
87- Dropout(0.5),
88- )
80+ julia> m = Chain(Dense(1 => 1), Dropout(0.5));
8981
9082julia> Flux.trainmode!(m);
9183
92- julia> m([1, 2]) # drops neurons with a probability of 0.5
93- 2-element Vector{Float32}:
94- -4.537827
95- -0.0
84+ julia> y = m([1]);
85+
86+ julia> m[2].p - 0.5 <= count(i->(i == 0), y) <= m[2].p + 0.5 # number of zeros can be 0 or 1
87+ true
9688```
9789"""
9890mutable struct Dropout{F,D,R<: AbstractRNG }
@@ -136,7 +128,9 @@ remain the same as before.
136128Does nothing to the input once [`testmode!`](@ref) is true.
137129
138130# Examples
139- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
131+ ```jldoctest
132+ julia> using Statistics
133+
140134julia> x = randn(20,1);
141135
142136julia> m = Chain(Dense(20 => 10, selu), AlphaDropout(0.5));
@@ -145,17 +139,8 @@ julia> Flux.trainmode!(m);
145139
146140julia> y = m(x);
147141
148- julia> Flux.std(x)
149- 1.097500619939126
150-
151- julia> Flux.std(y) # maintains the standard deviation of the input
152- 1.1504012188827453
153-
154- julia> Flux.mean(x) # maintains the mean of the input
155- -0.3217018554158738
156-
157- julia> Flux.mean(y)
158- -0.2526866470385106
142+ julia> isapprox(std(x), std(y), rtol=0.6)
143+ true
159144```
160145"""
161146mutable struct AlphaDropout{F,R<: AbstractRNG }
@@ -208,24 +193,20 @@ using the [`Scale`](@ref) layer.
208193See also [`BatchNorm`](@ref), [`InstanceNorm`](@ref), [`GroupNorm`](@ref), and [`normalise`](@ref).
209194
210195# Examples
211- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
196+ ```jldoctest
197+ julia> using Statistics
198+
212199julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
213200
214201julia> m = LayerNorm(3);
215202
216203julia> y = m(xs);
217204
218- julia> Flux.std(xs[:, :, :, 1])
219- 0.28713812337208383
220-
221- julia> Flux.std(y[:, :, :, 1]) # normalises each image (or all channels in an image)
222- 1.018993632693022
205+ julia> isapprox(std(y[:, :, :, 1]), 1, atol=0.1) && std(xs[:, :, :, 1]) != std(y[:, :, :, 1])
206+ true
223207
224- julia> Flux.std(xs[:, :, :, 2])
225- 0.22540260537916373
226-
227- julia> Flux.std(y[:, :, :, 2]) # normalises each image (or all channels in an image)
228- 1.018965249873791
208+ julia> isapprox(std(y[:, :, :, 2]), 1, atol=0.1) && std(xs[:, :, :, 2]) != std(y[:, :, :, 2])
209+ true
229210```
230211"""
231212struct LayerNorm{F,D,T,N}
@@ -329,17 +310,16 @@ Use [`testmode!`](@ref) during inference.
329310
330311# Examples
331312```julia
332- julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
313+ julia> using Statistics
333314
334- julia> Flux.std(xs)
335- 2.6822461565718467
315+ julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
336316
337317julia> m = BatchNorm(3);
338318
339- julia> Flux.trainmode!(m); # activating the layer without actually training it
319+ julia> Flux.trainmode!(m);
340320
341- julia> Flux. std(m(xs)) # normalises the complete batch
342- 1.0093209961092855
321+ julia> isapprox( std(m(xs)), 1, atol=0.1) && std(xs) != std(m(xs))
322+ true
343323```
344324"""
345325mutable struct BatchNorm{F,V,N,W}
@@ -419,24 +399,20 @@ that will be used to renormalize the input in test phase.
419399in previous Flux versions (< v0.12).
420400
421401# Examples
422- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
402+ ```jldoctest
403+ julia> using Statistics
404+
423405julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
424406
425407julia> m = InstanceNorm(3);
426408
427409julia> y = m(xs);
428410
429- julia> Flux. std(xs [:, :, 1, 1]) # original standard deviation of the first channel of image 1
430- 0.2989802650787384
411+ julia> isapprox( std(y [:, :, 1, 1]), 1, atol=0.1) && std(xs[:, :, 1, 1]) != std(y[:, :, 1, 1])
412+ true
431413
432- julia> Flux.std(y[:, :, 1, 1]) # each channel of the batch is normalised
433- 1.0606027381538408
434-
435- julia> Flux.std(xs[:, :, 2, 2]) # original standard deviation of the second channel of image 2
436- 0.28662705400461197
437-
438- julia> Flux.std(y[:, :, 2, 2]) # each channel of the batch is normalised
439- 1.06058729821187
414+ julia> isapprox(std(y[:, :, 2, 2]), 1, atol=0.1) && std(xs[:, :, 2, 2]) != std(y[:, :, 2, 2])
415+ true
440416```
441417"""
442418mutable struct InstanceNorm{F,V,N,W}
@@ -517,24 +493,20 @@ If `track_stats=true`, accumulates mean and var statistics in training phase
517493that will be used to renormalize the input in test phase.
518494
519495# Examples
520- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
496+ ```jldoctest
497+ julia> using Statistics
498+
521499julia> xs = rand(3, 3, 4, 2); # a batch of 2 3X3X4 images
522500
523501julia> m = GroupNorm(4, 2);
524502
525503julia> y = m(xs);
526504
527- julia> Flux.std(xs[:, :, 1:2, 1]) # original standard deviation of the first 2 channels of image 1
528- 0.307588490584917
529-
530- julia> Flux.std(y[:, :, 1:2, 1]) # normalises channels in groups of 2 (as specified)
531- 1.0289339365431291
532-
533- julia> Flux.std(xs[:, :, 3:4, 2]) # original standard deviation of the last 2 channels of image 2
534- 0.3111566100804274
505+ julia> isapprox(std(y[:, :, 1:2, 1]), 1, atol=0.1) && std(xs[:, :, 1:2, 1]) != std(y[:, :, 1:2, 1])
506+ true
535507
536- julia> Flux. std(y[:, :, 3:4, 2]) # normalises channels in groups of 2 (as specified )
537- 1.0289352493058574
508+ julia> isapprox( std(y[:, :, 3:4, 2]), 1, atol=0.1) && std(xs[:, :, 3:4, 2]) != std(y[:, :, 3:4, 2] )
509+ true
538510```
539511"""
540512mutable struct GroupNorm{F,V,N,W}
0 commit comments