@@ -67,32 +67,24 @@ Custom RNGs are only supported on the CPU.
67
67
Does nothing to the input once [`Flux.testmode!`](@ref) is `true`.
68
68
69
69
# 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);
76
74
77
- julia> Flux.trainmode!(m); # activating the layer without actually training it
75
+ julia> y = m([1]);
78
76
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
83
79
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));
89
81
90
82
julia> Flux.trainmode!(m);
91
83
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
96
88
```
97
89
"""
98
90
mutable struct Dropout{F,D,R<: AbstractRNG }
@@ -136,7 +128,9 @@ remain the same as before.
136
128
Does nothing to the input once [`testmode!`](@ref) is true.
137
129
138
130
# Examples
139
- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
131
+ ```jldoctest
132
+ julia> using Statistics
133
+
140
134
julia> x = randn(20,1);
141
135
142
136
julia> m = Chain(Dense(20 => 10, selu), AlphaDropout(0.5));
@@ -145,17 +139,8 @@ julia> Flux.trainmode!(m);
145
139
146
140
julia> y = m(x);
147
141
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
159
144
```
160
145
"""
161
146
mutable struct AlphaDropout{F,R<: AbstractRNG }
@@ -208,24 +193,20 @@ using the [`Scale`](@ref) layer.
208
193
See also [`BatchNorm`](@ref), [`InstanceNorm`](@ref), [`GroupNorm`](@ref), and [`normalise`](@ref).
209
194
210
195
# Examples
211
- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
196
+ ```jldoctest
197
+ julia> using Statistics
198
+
212
199
julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
213
200
214
201
julia> m = LayerNorm(3);
215
202
216
203
julia> y = m(xs);
217
204
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
223
207
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
229
210
```
230
211
"""
231
212
struct LayerNorm{F,D,T,N}
@@ -329,17 +310,16 @@ Use [`testmode!`](@ref) during inference.
329
310
330
311
# Examples
331
312
```julia
332
- julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
313
+ julia> using Statistics
333
314
334
- julia> Flux.std(xs)
335
- 2.6822461565718467
315
+ julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
336
316
337
317
julia> m = BatchNorm(3);
338
318
339
- julia> Flux.trainmode!(m); # activating the layer without actually training it
319
+ julia> Flux.trainmode!(m);
340
320
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
343
323
```
344
324
"""
345
325
mutable struct BatchNorm{F,V,N,W}
@@ -419,24 +399,20 @@ that will be used to renormalize the input in test phase.
419
399
in previous Flux versions (< v0.12).
420
400
421
401
# Examples
422
- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
402
+ ```jldoctest
403
+ julia> using Statistics
404
+
423
405
julia> xs = rand(3, 3, 3, 2); # a batch of 2 3X3X3 images
424
406
425
407
julia> m = InstanceNorm(3);
426
408
427
409
julia> y = m(xs);
428
410
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
431
413
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
440
416
```
441
417
"""
442
418
mutable struct InstanceNorm{F,V,N,W}
@@ -517,24 +493,20 @@ If `track_stats=true`, accumulates mean and var statistics in training phase
517
493
that will be used to renormalize the input in test phase.
518
494
519
495
# Examples
520
- ```jldoctest; filter = r"[+-]?([0-9]*[.])?[0-9]+"
496
+ ```jldoctest
497
+ julia> using Statistics
498
+
521
499
julia> xs = rand(3, 3, 4, 2); # a batch of 2 3X3X4 images
522
500
523
501
julia> m = GroupNorm(4, 2);
524
502
525
503
julia> y = m(xs);
526
504
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
535
507
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
538
510
```
539
511
"""
540
512
mutable struct GroupNorm{F,V,N,W}
0 commit comments