SquaredEpsilonHingeLoss.h
Go to the documentation of this file.
1 /*!
2  *
3  * \brief Implements the squard 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_SQUAREDEPSILONHINGELOSS_H
32 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_SQUAREDEPSILONHINGELOSS_H
33 
35 
36 namespace shark {
37 
38 ///
39 /// \brief Hinge-loss for large margin regression using th squared two-norm
40 ///
41 /// The loss is defined as \f$L_i = 1/2 \max\{0.0, ||f(x_i)-y{i,j}||^2- \epsilon^2\} \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.
46 ///
47 /// epsilon describes the distance from the label to the margin that is allowed until the point leaves
48 /// the margin.
49 ///
50 /// Contrary to th EpsilonHingeLoss, this loss is differentiable.
51 class SquaredEpsilonHingeLoss : public AbstractLoss<RealVector, RealVector>
52 {
53 public:
54  /// constructor
55  SquaredEpsilonHingeLoss(double epsilon):m_sqrEpsilon(sqr(epsilon)){
56  m_features |= base_type::HAS_FIRST_DERIVATIVE;
57  }
58 
59  /// \brief Returns class name "HingeLoss"
60  std::string name() const
61  { return "SquaredEpsilonHingeLoss"; }
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 
69  return 0.5*sum(max(0.0,sum_columns(sqr(labels-predictions)) - m_sqrEpsilon));
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 
77  gradient.resize(numInputs,predictions.size2());
78  double error = 0;
79  for(std::size_t i = 0; i != numInputs;++i){
80  double sampleLoss = 0.5*std::max(0.0,norm_sqr(row(predictions,i)-row(labels,i))-m_sqrEpsilon);
81  error+=sampleLoss;
82  if(sampleLoss > 0){
83  noalias(row(gradient,i)) = row(predictions,i)-row(labels,i);
84  }
85  else{
86  row(gradient,i).clear();
87  }
88  }
89  return error;
90  }
91 private:
92  double m_sqrEpsilon;
93 };
94 
95 }
96 #endif