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  * <http://shark-ml.org/>
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 
36 namespace 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 class EpsilonHingeLoss : public AbstractLoss<RealVector, RealVector>
52 {
53 public:
54  /// constructor
55  EpsilonHingeLoss(double epsilon):m_epsilon(epsilon){
56  m_features |= base_type::HAS_FIRST_DERIVATIVE;
57  }
58 
59  /// \brief Returns class name "HingeLoss"
60  std::string name() const
61  { return "EpsilonHingeLoss"; }
62 
63 
64  ///\brief calculates the sum of all
65  double eval(BatchLabelType const& labels, BatchOutputType const& predictions) const{
66  SIZE_CHECK(predictions.size1() == labels.size1());
67  SIZE_CHECK(predictions.size2() == labels.size2());
68  return sum(max(0.0,abs(labels-predictions)- m_epsilon));
69  }
70 
71  double evalDerivative(BatchLabelType const& labels, BatchOutputType const& predictions, BatchOutputType& gradient)const{
72  SIZE_CHECK(predictions.size1() == labels.size1());
73  SIZE_CHECK(predictions.size2() == labels.size2());
74  std::size_t numInputs = predictions.size1();
75  std::size_t outputDim = predictions.size2();
76 
77  gradient.resize(numInputs,outputDim);
78  double error = 0;
79  for(std::size_t i = 0; i != numInputs;++i){
80  for(std::size_t o = 0; o != outputDim;++o){
81  double sampleLoss = std::max(0.0,std::abs(predictions(i,o)-labels(i,o))-m_epsilon);
82  error+=sampleLoss;
83  gradient(i,o) = 0;
84  if(sampleLoss > 0){
85  if(predictions(i,o) > labels(i,o))
86  gradient(i,o) = 1;
87  else
88  gradient(i,o) = -1;
89  }
90 
91  }
92  }
93  return error;
94  }
95 private:
96  double m_epsilon;
97 };
98 
99 }
100 #endif