Skip to content

Commit

Permalink
Clean up the doctests + fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp committed Jun 14, 2022
1 parent e06f5c5 commit 416f0fa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
18 changes: 6 additions & 12 deletions src/layers/stateless.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,21 @@ Per default, `dims` is the last dimension.
# Examples
```jldoctest
julia> x = [9, 10, 20, 60];
julia> using Statistics
julia> Flux.std(x)
24.01908963026423
julia> x = [9, 10, 20, 60];
julia> y = Flux.normalise(x);
julia> Flux.std(y)
1.1546999832655012
julia> isapprox(std(y), 1, atol=0.2) && std(y) != std(x)
true
julia> x = rand(1:100, 10, 2);
julia> Flux.std(x, dims=1)
1×2 Matrix{Float64}:
28.5324 34.6425
julia> y = Flux.normalise(x, dims=1);
julia> Flux.std(y, dims=1)
1×2 Matrix{Float64}:
1.05409 1.05409
julia> isapprox(std(y, dims=1), ones(1, 2), atol=0.2) && std(y, dims=1) != std(x, dims=1)
true
```
"""
@inline function normalise(x::AbstractArray; dims=ndims(x), ϵ=ofeltype(x, 1e-5))
Expand Down
30 changes: 15 additions & 15 deletions src/losses/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ distribution `y`; calculated as -
```jldoctest
julia> y_model = [1, 3, 3]; # data should only take integral values
julia> poisson_loss(y_model, 1:3)
julia> Flux.poisson_loss(y_model, 1:3)
0.5023128522198171
```
"""
Expand Down Expand Up @@ -430,14 +430,14 @@ julia> y_pred = [0.1, 0.3, 1, 1.5];
julia> Flux.hinge_loss(y_pred, y_true)
0.55
julia> Flux.hinge_loss(y_pred[1], y_true[1]) # same sign but |ŷ| < 1
0.9
julia> Flux.hinge_loss(y_pred[1], y_true[1]) != 0 # same sign but |ŷ| < 1
true
julia> Flux.hinge_loss(y_pred[end], y_true[end]) # same sign but |ŷ| >= 1 -> loss = 0
0.0
julia> Flux.hinge_loss(y_pred[end], y_true[end]) == 0 # same sign but |ŷ| >= 1
true
julia> Flux.hinge_loss(y_pred[2], y_true[2]) # opposite signs -> loss != 0
1.3
julia> Flux.hinge_loss(y_pred[2], y_true[2]) != 0 # opposite signs
true
```
"""
function hinge_loss(ŷ, y; agg = mean)
Expand Down Expand Up @@ -465,14 +465,14 @@ julia> y_pred = [0.1, 0.3, 1, 1.5];
julia> Flux.squared_hinge_loss(y_pred, y_true)
0.625
julia> Flux.squared_hinge_loss(y_pred[1], y_true[1]) # same sign but |ŷ| < 1
0.81
julia> Flux.squared_hinge_loss(y_pred[1], y_true[1]) != 0
true
julia> Flux.squared_hinge_loss(y_pred[end], y_true[end]) # same sign and |ŷ| >= 1 -> loss = 0
0.0
julia> Flux.squared_hinge_loss(y_pred[end], y_true[end]) == 0
true
julia> Flux.squared_hinge_loss(y_pred[2], y_true[2]) # opposite signs -> loss != 0
1.6900000000000002
julia> Flux.squared_hinge_loss(y_pred[2], y_true[2]) != 0
true
```
"""
function squared_hinge_loss(ŷ, y; agg = mean)
Expand Down Expand Up @@ -527,8 +527,8 @@ julia> ŷ_fnp = [1, 1, 0, 1, 1, 0]; # 1 false negative, 1 false positive -> 2
julia> Flux.tversky_loss(ŷ_fnp, y)
0.19999999999999996
julia> Flux.tversky_loss(ŷ_fp, y) # should be smaller than tversky_loss(ŷ_fnp, y), as FN is given more weight
0.1071428571428571
julia> Flux.tversky_loss(ŷ_fp, y) < Flux.tversky_loss(ŷ_fnp, y) # FN is given more weight
true
```
"""
function tversky_loss(ŷ, y; β = ofeltype(ŷ, 0.7))
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ Flux
julia> for i = 1:4 # sleeps for 1 second -> the function can be called in alternate iterations
a()
sleep(1)
sleep(1.5)
end
Flux
Flux
Expand Down
4 changes: 2 additions & 2 deletions test/losses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ y = [1.0 0.5 0.3 2.4]
end

@testset "tversky_loss" begin
@test Flux.tversky_loss(ŷ, y) -0.06772009029345383
@test Flux.tversky_loss(ŷ, y, β=0.8) -0.09490740740740744
@test Flux.tversky_loss(ŷ, y) 0.028747433264887046
@test Flux.tversky_loss(ŷ, y, β=0.8) 0.050200803212851364
@test Flux.tversky_loss(y, y) -0.5576923076923075
end

Expand Down

0 comments on commit 416f0fa

Please sign in to comment.