Skip to content

Commit 02c8a95

Browse files
committed
Clean up the doctests + fix tests
1 parent dcbdf07 commit 02c8a95

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

src/layers/stateless.jl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,21 @@ Per default, `dims` is the last dimension.
3535
3636
# Examples
3737
```jldoctest
38-
julia> x = [9, 10, 20, 60];
38+
julia> using Statistics
3939
40-
julia> Flux.std(x)
41-
24.01908963026423
40+
julia> x = [9, 10, 20, 60];
4241
4342
julia> y = Flux.normalise(x);
4443
45-
julia> Flux.std(y)
46-
1.1546999832655012
44+
julia> isapprox(std(y), 1, atol=0.2) && std(y) != std(x)
45+
true
4746
4847
julia> x = rand(1:100, 10, 2);
4948
50-
julia> Flux.std(x, dims=1)
51-
1×2 Matrix{Float64}:
52-
28.5324 34.6425
53-
5449
julia> y = Flux.normalise(x, dims=1);
5550
56-
julia> Flux.std(y, dims=1)
57-
1×2 Matrix{Float64}:
58-
1.05409 1.05409
51+
julia> isapprox(std(y, dims=1), ones(1, 2), atol=0.2) && std(y, dims=1) != std(x, dims=1)
52+
true
5953
```
6054
"""
6155
@inline function normalise(x::AbstractArray; dims=ndims(x), ϵ=ofeltype(x, 1e-5))

src/losses/functions.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ distribution `y`; calculated as -
401401
```jldoctest
402402
julia> y_model = [1, 3, 3]; # data should only take integral values
403403
404-
julia> poisson_loss(y_model, 1:3)
404+
julia> Flux.poisson_loss(y_model, 1:3)
405405
0.5023128522198171
406406
```
407407
"""
@@ -430,14 +430,14 @@ julia> y_pred = [0.1, 0.3, 1, 1.5];
430430
julia> Flux.hinge_loss(y_pred, y_true)
431431
0.55
432432
433-
julia> Flux.hinge_loss(y_pred[1], y_true[1]) # same sign but |ŷ| < 1
434-
0.9
433+
julia> Flux.hinge_loss(y_pred[1], y_true[1]) != 0 # same sign but |ŷ| < 1
434+
true
435435
436-
julia> Flux.hinge_loss(y_pred[end], y_true[end]) # same sign but |ŷ| >= 1 -> loss = 0
437-
0.0
436+
julia> Flux.hinge_loss(y_pred[end], y_true[end]) == 0 # same sign but |ŷ| >= 1
437+
true
438438
439-
julia> Flux.hinge_loss(y_pred[2], y_true[2]) # opposite signs -> loss != 0
440-
1.3
439+
julia> Flux.hinge_loss(y_pred[2], y_true[2]) != 0 # opposite signs
440+
true
441441
```
442442
"""
443443
function hinge_loss(ŷ, y; agg = mean)
@@ -465,14 +465,14 @@ julia> y_pred = [0.1, 0.3, 1, 1.5];
465465
julia> Flux.squared_hinge_loss(y_pred, y_true)
466466
0.625
467467
468-
julia> Flux.squared_hinge_loss(y_pred[1], y_true[1]) # same sign but |ŷ| < 1
469-
0.81
468+
julia> Flux.squared_hinge_loss(y_pred[1], y_true[1]) != 0
469+
true
470470
471-
julia> Flux.squared_hinge_loss(y_pred[end], y_true[end]) # same sign and |ŷ| >= 1 -> loss = 0
472-
0.0
471+
julia> Flux.squared_hinge_loss(y_pred[end], y_true[end]) == 0
472+
true
473473
474-
julia> Flux.squared_hinge_loss(y_pred[2], y_true[2]) # opposite signs -> loss != 0
475-
1.6900000000000002
474+
julia> Flux.squared_hinge_loss(y_pred[2], y_true[2]) != 0
475+
true
476476
```
477477
"""
478478
function squared_hinge_loss(ŷ, y; agg = mean)
@@ -527,8 +527,8 @@ julia> ŷ_fnp = [1, 1, 0, 1, 1, 0]; # 1 false negative, 1 false positive -> 2
527527
julia> Flux.tversky_loss(ŷ_fnp, y)
528528
0.19999999999999996
529529
530-
julia> Flux.tversky_loss(ŷ_fp, y) # should be smaller than tversky_loss(ŷ_fnp, y), as FN is given more weight
531-
0.1071428571428571
530+
julia> Flux.tversky_loss(ŷ_fp, y) < Flux.tversky_loss(ŷ_fnp, y) # FN is given more weight
531+
true
532532
```
533533
"""
534534
function tversky_loss(ŷ, y; β = ofeltype(ŷ, 0.7))

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ Flux
535535
536536
julia> for i = 1:4 # sleeps for 1 second -> the function can be called in alternate iterations
537537
a()
538-
sleep(1)
538+
sleep(1.5)
539539
end
540540
Flux
541541
Flux

test/losses.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ y = [1.0 0.5 0.3 2.4]
163163
end
164164

165165
@testset "tversky_loss" begin
166-
@test Flux.tversky_loss(ŷ, y) -0.06772009029345383
167-
@test Flux.tversky_loss(ŷ, y, β=0.8) -0.09490740740740744
166+
@test Flux.tversky_loss(ŷ, y) 0.028747433264887046
167+
@test Flux.tversky_loss(ŷ, y, β=0.8) 0.050200803212851364
168168
@test Flux.tversky_loss(y, y) -0.5576923076923075
169169
end
170170

0 commit comments

Comments
 (0)