Skip to content

Commit

Permalink
Merge pull request #8 from Evizero/master
Browse files Browse the repository at this point in the history
Add squared hinge loss for L2-SVM
  • Loading branch information
lindahua committed Sep 11, 2015
2 parents f815dbc + d8d64c1 commit 2408ae9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This package provides basic components for implementing regularized empirical ri
- [x] quantile loss
- [x] huber loss
- [x] hinge loss
- [x] squared hinge loss
- [x] smoothed hinge loss
- [x] logistic loss
- [x] sum squared loss (for multivariate prediction)
Expand Down
1 change: 1 addition & 0 deletions src/EmpiricalRisks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export
QuantileLoss,
HuberLoss,
HingeLoss,
SqrHingeLoss,
SmoothedHingeLoss,
LogisticLoss,
SumLoss,
Expand Down
22 changes: 21 additions & 1 deletion src/loss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ end



## Hinge loss (for SVM)
## Hinge loss (for L1-SVM)
#
# loss(p, y) := max(1 - y * p, 0)
#
Expand All @@ -118,6 +118,26 @@ function value_and_deriv{T<:BlasReal}(::HingeLoss, p::T, y::T)
end


## Squared Hinge loss (for L2-SVM)
#
# loss(p, y) := max(1 - y * p, 0)^2
#
immutable SqrHingeLoss <: UnivariateLoss
end

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

deriv{T<:BlasReal}(::SqrHingeLoss, p::T, y::T) = y * p < one(T) ? (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))
end


## Smoothed HingeLoss
#
# loss(p, y) := 0 ... y * p > 1 + h
Expand Down
6 changes: 6 additions & 0 deletions test/uniloss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ _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)
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 2408ae9

Please sign in to comment.