diff --git a/src/layers/stateless.jl b/src/layers/stateless.jl index 5298b634bb..06c8b6a4a9 100644 --- a/src/layers/stateless.jl +++ b/src/layers/stateless.jl @@ -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)) diff --git a/src/losses/functions.jl b/src/losses/functions.jl index c0278bd395..ebf2de2301 100644 --- a/src/losses/functions.jl +++ b/src/losses/functions.jl @@ -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 ``` """ @@ -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) @@ -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) @@ -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)) diff --git a/src/utils.jl b/src/utils.jl index 15525c6357..c67d1a6270 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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 diff --git a/test/losses.jl b/test/losses.jl index 2ca697a657..c1061bb388 100644 --- a/test/losses.jl +++ b/test/losses.jl @@ -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