EpsilonHingeLoss.h
Go to the documentation of this file.
1/*!
2 *
3 * \brief Implements the Hinge Loss function for maximum margin regression
4 *
5 *
6 * \author Oswin Krause
7 * \date 2014
8 *
9 *
10 * \par Copyright 1995-2017 Shark Development Team
11 *
12 * <BR><HR>
13 * This file is part of Shark.
14 * <https://shark-ml.github.io/Shark/>
15 *
16 * Shark is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as published
18 * by the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * Shark is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31#ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_EPSILONHINGELOSS_H
32#define SHARK_OBJECTIVEFUNCTIONS_LOSS_EPSILONHINGELOSS_H
33
35
36namespace shark {
37
38///
39/// \brief Hinge-loss for large margin regression
40///
41/// The loss is defined as \f$L_i = \sum_j^N \max\{0.0, |f_j(x_i)-y_{i,j}|-\epsilon\} \f$
42/// where \f$ y_i =(y_{i,1},\dots,y_{i_N} \f$ is the label of dimension N
43/// and \f$ f_j(x_i) \f$ is the j-th output of the prediction of the model for the ith input.
44/// The loss introduces the concept of a margin to regression, that is, points are not punished
45/// when they are sufficiently close to the function. Points which are outside of the
46/// margin are linearly punished, that is the loss is outlier resistant.
47///
48/// Epsilon describes the size of the margin.
49///
50/// The hinge-loss is not differentiable at the points y_{i,j}+epsilon and y_{i,j}-epsilon.
51/// \ingroup lossfunctions
52class EpsilonHingeLoss : public AbstractLoss<RealVector, RealVector>
53{
54public:
55 /// constructor
56 EpsilonHingeLoss(double epsilon):m_epsilon(epsilon){
57 m_features |= base_type::HAS_FIRST_DERIVATIVE;
58 }
59
60 /// \brief Returns class name "HingeLoss"
61 std::string name() const
62 { return "EpsilonHingeLoss"; }
63
64
65 ///\brief calculates the sum of all
66 double eval(BatchLabelType const& labels, BatchOutputType const& predictions) const{
67 SIZE_CHECK(predictions.size1() == labels.size1());
68 SIZE_CHECK(predictions.size2() == labels.size2());
69 return sum(max(0.0,abs(labels-predictions)- m_epsilon));
70 }
71
72 double evalDerivative(BatchLabelType const& labels, BatchOutputType const& predictions, BatchOutputType& gradient)const{
73 SIZE_CHECK(predictions.size1() == labels.size1());
74 SIZE_CHECK(predictions.size2() == labels.size2());
75 std::size_t numInputs = predictions.size1();
76 std::size_t outputDim = predictions.size2();
77
78 gradient.resize(numInputs,outputDim);
79 double error = 0;
80 for(std::size_t i = 0; i != numInputs;++i){
81 for(std::size_t o = 0; o != outputDim;++o){
82 double sampleLoss = std::max(0.0,std::abs(predictions(i,o)-labels(i,o))-m_epsilon);
83 error+=sampleLoss;
84 gradient(i,o) = 0;
85 if(sampleLoss > 0){
86 if(predictions(i,o) > labels(i,o))
87 gradient(i,o) = 1;
88 else
89 gradient(i,o) = -1;
90 }
91
92 }
93 }
94 return error;
95 }
96private:
97 double m_epsilon;
98};
99
100}
101#endif