Skip to content

Commit

Permalink
implement properties for margin losses
Browse files Browse the repository at this point in the history
  • Loading branch information
Evizero committed Oct 6, 2016
1 parent a00c07a commit 0b13dbf
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/supervised/margin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ doc"""
"""
immutable ZeroOneLoss <: MarginLoss end

value(loss::ZeroOneLoss, target::Number, output::Number) = value(loss, target * output)
deriv(loss::ZeroOneLoss, target::Number, output::Number) = zero(output)
deriv2(loss::ZeroOneLoss, target::Number, output::Number) = zero(output)

value{T<:Number}(loss::ZeroOneLoss, agreement::T) = sign(agreement) < 0 ? one(T) : zero(T)
deriv{T<:Number}(loss::ZeroOneLoss, agreement::T) = zero(T)
deriv2{T<:Number}(loss::ZeroOneLoss, agreement::T) = zero(T)
value_deriv{T<:Number}(loss::ZeroOneLoss, agreement::T) = sign(agreement) < 0 ? (one(T), zero(T)) : (zero(T), zero(T))

isminimizable(::ZeroOneLoss) = true
isdifferentiable(::ZeroOneLoss) = false
isdifferentiable(::ZeroOneLoss, at) = at != 0
istwicedifferentiable(::ZeroOneLoss) = false
istwicedifferentiable(::ZeroOneLoss, at) = at != 0
isnemitski(::ZeroOneLoss) = true
islipschitzcont(::ZeroOneLoss) = true
isconvex(::ZeroOneLoss) = false
isclasscalibrated(loss::ZeroOneLoss) = true
isclipable(::ZeroOneLoss) = true
Expand Down Expand Up @@ -154,6 +160,7 @@ deriv{T<:Number}(loss::L1HingeLoss, agreement::T) = agreement >= 1 ? zero(T) : -
deriv2{T<:Number}(loss::L1HingeLoss, agreement::T) = zero(T)
value_deriv{T<:Number}(loss::L1HingeLoss, agreement::T) = agreement >= 1 ? (zero(T), zero(T)) : (one(T) - agreement, -one(T))

isfishercons(::L1HingeLoss) = true
isdifferentiable(::L1HingeLoss) = false
isdifferentiable(::L1HingeLoss, at) = at != 1
istwicedifferentiable(::L1HingeLoss) = false
Expand Down Expand Up @@ -197,6 +204,7 @@ deriv{T<:Number}(loss::L2HingeLoss, agreement::T) = agreement >= 1 ? zero(T) : T
deriv2{T<:Number}(loss::L2HingeLoss, agreement::T) = agreement >= 1 ? zero(T) : T(2)
value_deriv{T<:Number}(loss::L2HingeLoss, agreement::T) = agreement >= 1 ? (zero(T), zero(T)) : (abs2(one(T) - agreement), T(2) * (agreement - one(T)))

isunivfishercons(::L2HingeLoss) = true
isdifferentiable(::L2HingeLoss) = true
isdifferentiable(::L2HingeLoss, at) = true
istwicedifferentiable(::L2HingeLoss) = false
Expand All @@ -205,8 +213,8 @@ islocallylipschitzcont(::L2HingeLoss) = true
islipschitzcont(::L2HingeLoss) = false
islipschitzcont_deriv(::L2HingeLoss) = true
isconvex(::L2HingeLoss) = true
isstrictlyconvex(::L2HingeLoss) = true
isstronglyconvex(::L2HingeLoss) = true
isstrictlyconvex(::L2HingeLoss) = false
isstronglyconvex(::L2HingeLoss) = false
isclipable(::L2HingeLoss) = true

# ============================================================
Expand Down
193 changes: 193 additions & 0 deletions test/tst_properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,196 @@ end
@test issymmetric(loss) == true
end

# --------------------------------------------------------------

@testset "ZeroOneLoss" begin
loss = ZeroOneLoss()

@test isminimizable(loss) == true

@test isdifferentiable(loss) == false
@test isdifferentiable(loss, 0) == false
@test isdifferentiable(loss, 1) == true
@test istwicedifferentiable(loss) == false
@test istwicedifferentiable(loss, 1) == true

@test isstronglyconvex(loss) == false
@test isstrictlyconvex(loss) == false
@test isconvex(loss) == false

@test isnemitski(loss) == true
@test islipschitzcont(loss) == true
@test islocallylipschitzcont(loss) == true
@test isclipable(loss) == true
@test ismarginbased(loss) == true
@test isdistancebased(loss) == false
@test issymmetric(loss) == false
@test isclasscalibrated(loss) == true
end

@testset "PerceptronLoss" begin
loss = PerceptronLoss()

@test isminimizable(loss) == true

@test isdifferentiable(loss) == false
@test isdifferentiable(loss, 0) == false
@test isdifferentiable(loss, 1) == true
@test istwicedifferentiable(loss) == false
@test istwicedifferentiable(loss, 0) == false
@test istwicedifferentiable(loss, 1) == true

@test isstronglyconvex(loss) == false
@test isstrictlyconvex(loss) == false
@test isconvex(loss) == true

@test isnemitski(loss) == true
@test islipschitzcont(loss) == true
@test islocallylipschitzcont(loss) == true
@test isclipable(loss) == true
@test ismarginbased(loss) == true
@test isdistancebased(loss) == false
@test issymmetric(loss) == false
@test isclasscalibrated(loss) == false
end

@testset "LogitMarginLoss" begin
loss = LogitMarginLoss()

@test isminimizable(loss) == true

@test isdifferentiable(loss) == true
@test isdifferentiable(loss, 0) == true
@test isdifferentiable(loss, 1) == true
@test istwicedifferentiable(loss) == true
@test istwicedifferentiable(loss, 0) == true
@test istwicedifferentiable(loss, 1) == true

@test isstronglyconvex(loss) == false
@test isstrictlyconvex(loss) == true
@test isconvex(loss) == true

@test isnemitski(loss) == true
@test islipschitzcont(loss) == true
@test islocallylipschitzcont(loss) == true
@test isclipable(loss) == false
@test ismarginbased(loss) == true
@test isdistancebased(loss) == false
@test issymmetric(loss) == false
@test isclasscalibrated(loss) == true
@test isfishercons(loss) == true
@test isunivfishercons(loss) == true
end

@testset "L1HingeLoss" begin
loss = L1HingeLoss()

@test isminimizable(loss) == true

@test isdifferentiable(loss) == false
@test isdifferentiable(loss, 0) == true
@test isdifferentiable(loss, 1) == false
@test istwicedifferentiable(loss) == false
@test istwicedifferentiable(loss, 0) == true
@test istwicedifferentiable(loss, 1) == false

@test isstronglyconvex(loss) == false
@test isstrictlyconvex(loss) == false
@test isconvex(loss) == true

@test isnemitski(loss) == true
@test islipschitzcont(loss) == true
@test islocallylipschitzcont(loss) == true
@test isclipable(loss) == true
@test ismarginbased(loss) == true
@test isdistancebased(loss) == false
@test issymmetric(loss) == false
@test isclasscalibrated(loss) == true
@test isfishercons(loss) == true
@test isunivfishercons(loss) == false
end

@testset "L2HingeLoss" begin
loss = L2HingeLoss()

@test isminimizable(loss) == true

@test isdifferentiable(loss) == true
@test isdifferentiable(loss, 0) == true
@test isdifferentiable(loss, 1) == true
@test istwicedifferentiable(loss) == false
@test istwicedifferentiable(loss, 0) == true
@test istwicedifferentiable(loss, 1) == false

@test isstronglyconvex(loss) == false
@test isstrictlyconvex(loss) == false
@test isconvex(loss) == true

@test isnemitski(loss) == true
@test islipschitzcont(loss) == false
@test islocallylipschitzcont(loss) == true
@test isclipable(loss) == true
@test ismarginbased(loss) == true
@test isdistancebased(loss) == false
@test issymmetric(loss) == false
@test isclasscalibrated(loss) == true
@test isfishercons(loss) == true
@test isunivfishercons(loss) == true
end

@testset "SmoothedL1HingeLoss" begin
loss = SmoothedL1HingeLoss(2)

@test isminimizable(loss) == true

@test isdifferentiable(loss) == true
@test isdifferentiable(loss, 0) == true
@test isdifferentiable(loss, 1) == true
@test istwicedifferentiable(loss) == false
@test istwicedifferentiable(loss, -1) == false
@test istwicedifferentiable(loss, 0) == true
@test istwicedifferentiable(loss, 1) == false
@test istwicedifferentiable(loss, 2) == true

@test isstronglyconvex(loss) == false
@test isstrictlyconvex(loss) == false
@test isconvex(loss) == true

@test isnemitski(loss) == true
@test islipschitzcont(loss) == true
@test islocallylipschitzcont(loss) == true
@test isclipable(loss) == true
@test ismarginbased(loss) == true
@test isdistancebased(loss) == false
@test issymmetric(loss) == false
@test isclasscalibrated(loss) == true
end

@testset "ModifiedHuberLoss" begin
loss = ModifiedHuberLoss()

@test isminimizable(loss) == true

@test isdifferentiable(loss) == true
@test isdifferentiable(loss, 0) == true
@test isdifferentiable(loss, 1) == true
@test istwicedifferentiable(loss) == false
@test istwicedifferentiable(loss, -1) == false
@test istwicedifferentiable(loss, 0) == true
@test istwicedifferentiable(loss, 1) == false
@test istwicedifferentiable(loss, 2) == true

@test isstronglyconvex(loss) == false
@test isstrictlyconvex(loss) == false
@test isconvex(loss) == true

@test isnemitski(loss) == true
@test islipschitzcont(loss) == true
@test islocallylipschitzcont(loss) == true
@test isclipable(loss) == true
@test ismarginbased(loss) == true
@test isdistancebased(loss) == false
@test issymmetric(loss) == false
@test isclasscalibrated(loss) == true
end

0 comments on commit 0b13dbf

Please sign in to comment.