Skip to content

Commit

Permalink
Merge pull request #11 from Evizero/sqrhinge
Browse files Browse the repository at this point in the history
Bugfix SqrHingeLoss concerning consistency
  • Loading branch information
lindahua committed Oct 6, 2015
2 parents b7b66d6 + 60e3697 commit edc49b0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/loss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ function value_and_deriv{T<:BlasReal}(loss::HuberLoss, p::T, y::T)
end



## Hinge loss (for L1-SVM)
#
# loss(p, y) := max(1 - y * p, 0)
Expand All @@ -160,14 +159,14 @@ end

function value{T<:BlasReal}(::SqrHingeLoss, p::T, y::T)
yp = y * p
yp >= one(T) ? zero(T) : half(abs2(nonneg(one(T) - yp)))
yp >= one(T) ? zero(T) : abs2(nonneg(one(T) - yp))
end

deriv{T<:BlasReal}(::SqrHingeLoss, p::T, y::T) = y * p < one(T) ? (p - y) : zero(T)
deriv{T<:BlasReal}(::SqrHingeLoss, p::T, y::T) = y * p < one(T) ? 2(p - y) : zero(T)

function value_and_deriv{T<:BlasReal}(::SqrHingeLoss, p::T, y::T)
yp = y * p
yp >= one(T) ? (zero(T), zero(T)) : (half(abs2(one(T) - yp)), (p - y))
yp >= one(T) ? (zero(T), zero(T)) : (abs2(one(T) - yp), 2(p - y))
end


Expand Down
5 changes: 1 addition & 4 deletions test/uniloss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,16 @@ end
verify_uniloss(HuberLoss(0.3), (p, y) -> _huberf(0.3, p, y), -2.0:0.25:2.0, -1.0:0.5:1.0)
verify_uniloss(HuberLoss(0.5), (p, y) -> _huberf(0.5, p, y), -2.0:0.25:2.0, -1.0:0.5:1.0)


# HingeLoss

_hingef(u::Dual, y) = y * real(u) < 1.0 ? 1.0 - y * u : dual(0.0, 0.0)
verify_uniloss(HingeLoss(), _hingef, -2.0:0.5:2.0, [-1.0, 1.0])


# SquaredHingeLoss

_sqrhingef(u::Dual, y) = y * real(u) < 1.0 ? .5(1.0 - y * u).^2 : dual(0.0, 0.0)
_sqrhingef(u::Dual, y) = y * real(u) < 1.0 ? (1.0 - y * u).^2 : dual(0.0, 0.0)
verify_uniloss(SqrHingeLoss(), _sqrhingef, -2.0:0.5:2.0, [-1.0, 1.0])


# SmoothedHingeLoss

function _sm_hingef(h::Float64, u::Dual, y)
Expand Down

0 comments on commit edc49b0

Please sign in to comment.