HuberLoss.h
Go to the documentation of this file.
1/*!
2 *
3 * \brief Implements the Huber loss function for robust 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_HUBERLOSS_H
32#define SHARK_OBJECTIVEFUNCTIONS_LOSS_HUBERLOSS_H
33
35
36namespace shark {
37
38/// \brief Huber-loss for for robust regression
39///
40/// The Huber loss is a function that is quadratic if\f$ ||f(x)-y||_2 \leq \delta \f$.
41/// Outside this region, whn the error is larger, it is defined as a linear continuation. The function is once
42/// but not twice differentiable. This loss is important for regression as it weights outliers lower than
43/// ordinary least squares regression while still preserving a convex shape of the loss function.
44///
45/// Please not that, due to its nature, the error function is not scale invariant. thus rescaling the dataset
46/// changes the behaviour. This function has the hyper parameter delta which marks thee region where
47/// the function changes from quadratic to linear.
48/// \ingroup lossfunctions
49class HuberLoss : public AbstractLoss<RealVector, RealVector>
50{
51public:
52 /// constructor
53 HuberLoss(double delta = 1.0):m_delta(delta){
54 m_features |= base_type::HAS_FIRST_DERIVATIVE;
55 }
56
57 /// \brief Returns class name "HuberLoss"
58 std::string name() const
59 { return "HuberLoss"; }
60
61
62 ///\brief calculates the sum of all
63 double eval(BatchLabelType const& labels, BatchOutputType const& predictions) const{
64 SIZE_CHECK(labels.size1() == predictions.size1());
65 SIZE_CHECK(labels.size2() == predictions.size2());
66 std::size_t numInputs = labels.size1();
67
68 double error = 0;
69 for(std::size_t i = 0; i != numInputs;++i){
70 double norm2 = norm_sqr(row(predictions,i)-row(labels,i));
71
72 //check whether we are in the quadratic area
73 if(norm2 <= sqr(m_delta)){
74 error += 0.5*norm2;
75 }
76 else{
77 error += m_delta*std::sqrt(norm2)-0.5*sqr(m_delta);
78 }
79 }
80 return error;
81 }
82
83 double evalDerivative(BatchLabelType const& labels, BatchOutputType const& predictions, BatchOutputType& gradient)const{
84 SIZE_CHECK(labels.size1() == predictions.size1());
85 SIZE_CHECK(labels.size2() == predictions.size2());
86 std::size_t numInputs = predictions.size1();
87 std::size_t outputDim = predictions.size2();
88
89 gradient.resize(numInputs,outputDim);
90 double error = 0;
91 for(std::size_t i = 0; i != numInputs;++i){
92 double norm2 = norm_sqr(row(predictions,i)-row(labels,i));
93
94 //check whether we are in the quadratic area
95 if(norm2 <= sqr(m_delta)){
96 error += 0.5*norm2;
97 noalias(row(gradient,i)) = row(predictions,i)-row(labels,i);
98 }
99 else{
100 double norm = std::sqrt(norm2);
101 error += m_delta*norm-0.5*sqr(m_delta);
102 noalias(row(gradient,i)) = m_delta/norm*(row(predictions,i)-row(labels,i));
103 }
104 }
105 return error;
106 }
107
108private:
109 double m_delta;
110};
111
112}
113#endif