AbstractLoss.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief super class of all loss functions
6  *
7  *
8  *
9  * \author T. Glasmachers
10  * \date 2010-2011
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 
34 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_ABSTRACTLOSS_H
35 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_ABSTRACTLOSS_H
36 
38 #include <shark/LinAlg/Base.h>
40 namespace shark {
41 
42 
43 /// \brief Loss function interface
44 ///
45 /// \par
46 /// In statistics and machine learning, a loss function encodes
47 /// the severity of getting a label wrong. This is am important
48 /// special case of a cost function (see AbstractCost), where
49 /// the cost is computed as the average loss over a set, also
50 /// known as (empirical) risk.
51 ///
52 /// \par
53 /// It is generally agreed that loss values are non-negative,
54 /// and that the loss of correct prediction is zero. This rule
55 /// is not formally checked, but instead left to the various
56 /// sub-classes.
57 ///
58 template<class LabelT, class OutputT = LabelT>
59 class AbstractLoss : public AbstractCost<LabelT, OutputT>
60 {
61 public:
63  typedef OutputT OutputType;
64  typedef LabelT LabelType;
65  typedef RealMatrix MatrixType;
66 
69 
70  /// \brief Const references to LabelType
71  typedef typename ConstProxyReference<LabelType const>::type ConstLabelReference;
72  /// \brief Const references to OutputType
73  typedef typename ConstProxyReference<OutputType const>::type ConstOutputReference;
74 
77  }
78 
79  /// \brief evaluate the loss for a batch of targets and a prediction
80  ///
81  /// \param target target values
82  /// \param prediction predictions, typically made by a model
83  virtual double eval( BatchLabelType const& target, BatchOutputType const& prediction) const = 0;
84 
85  /// \brief evaluate the loss for a target and a prediction
86  ///
87  /// \param target target value
88  /// \param prediction prediction, typically made by a model
89  virtual double eval( ConstLabelReference target, ConstOutputReference prediction)const{
90  BatchLabelType labelBatch = Batch<LabelType>::createBatch(target,1);
91  getBatchElement(labelBatch,0)=target;
92  BatchOutputType predictionBatch = Batch<OutputType>::createBatch(prediction,1);
93  getBatchElement(predictionBatch,0)=prediction;
94  return eval(labelBatch,predictionBatch);
95  }
96 
97  /// \brief evaluate the loss and its derivative for a target and a prediction
98  ///
99  /// \param target target value
100  /// \param prediction prediction, typically made by a model
101  /// \param gradient the gradient of the loss function with respect to the prediction
102  virtual double evalDerivative(ConstLabelReference target, ConstOutputReference prediction, OutputType& gradient) const {
103  BatchLabelType labelBatch = Batch<LabelType>::createBatch(target,1);
104  getBatchElement(labelBatch, 0) = target;
105  BatchOutputType predictionBatch = Batch<OutputType>::createBatch(prediction, 1);
106  getBatchElement(predictionBatch, 0) = prediction;
107  BatchOutputType gradientBatch = Batch<OutputType>::createBatch(gradient, 1);
108  double ret = evalDerivative(labelBatch, predictionBatch, gradientBatch);
109  gradient = getBatchElement(gradientBatch, 0);
110  return ret;
111  }
112 
113  /// \brief evaluate the loss and its first and second derivative for a target and a prediction
114  ///
115  /// \param target target value
116  /// \param prediction prediction, typically made by a model
117  /// \param gradient the gradient of the loss function with respect to the prediction
118  /// \param hessian the hessian of the loss function with respect to the prediction
119  virtual double evalDerivative(
120  ConstLabelReference target, ConstOutputReference prediction,
121  OutputType& gradient,MatrixType & hessian
122  ) const {
124  return 0.0; // dead code, prevent warning
125  }
126 
127  /// \brief evaluate the loss and the derivative w.r.t. the prediction
128  ///
129  /// \par
130  /// The default implementations throws an exception.
131  /// If you overwrite this method, don't forget to set
132  /// the flag HAS_FIRST_DERIVATIVE.
133  /// \param target target value
134  /// \param prediction prediction, typically made by a model
135  /// \param gradient the gradient of the loss function with respect to the prediction
136  virtual double evalDerivative(BatchLabelType const& target, BatchOutputType const& prediction, BatchOutputType& gradient) const
137  {
139  return 0.0; // dead code, prevent warning
140  }
141 
142  //~ /// \brief evaluate the loss and fist and second derivative w.r.t. the prediction
143  //~ ///
144  //~ /// \par
145  //~ /// The default implementations throws an exception.
146  //~ /// If you overwrite this method, don't forget to set
147  //~ /// the flag HAS_FIRST_DERIVATIVE.
148  //~ /// \param target target value
149  //~ /// \param prediction prediction, typically made by a model
150  //~ /// \param gradient the gradient of the loss function with respect to the prediction
151  //~ /// \param hessian the hessian matrix of the loss function with respect to the prediction
152  //~ virtual double evalDerivative(
153  //~ LabelType const& target,
154  //~ OutputType const& prediction,
155  //~ OutputType& gradient,
156  //~ MatrixType& hessian) const
157  //~ {
158  //~ SHARK_FEATURE_EXCEPTION_DERIVED(HAS_SECOND_DERIVATIVE);
159  //~ return 0.0; // dead code, prevent warning
160  //~ }
161 
162  /// from AbstractCost
163  ///
164  /// \param targets target values
165  /// \param predictions predictions, typically made by a model
166  double eval(Data<LabelType> const& targets, Data<OutputType> const& predictions) const{
167  SIZE_CHECK(predictions.numberOfElements() == targets.numberOfElements());
168  SIZE_CHECK(predictions.numberOfBatches() == targets.numberOfBatches());
169  int numBatches = (int) targets.numberOfBatches();
170  double error = 0;
171  SHARK_PARALLEL_FOR(int i = 0; i < numBatches; ++i){
172  double batchError= eval(targets.batch(i),predictions.batch(i));
174  error+=batchError;
175  }
176  }
177  return error / targets.numberOfElements();
178  }
179 
180  /// \brief evaluate the loss for a target and a prediction
181  ///
182  /// \par
183  /// convenience operator
184  ///
185  /// \param target target value
186  /// \param prediction prediction, typically made by a model
187  double operator () (LabelType const& target, OutputType const& prediction) const
188  { return eval(target, prediction); }
189 
190  double operator () (BatchLabelType const& target, BatchOutputType const& prediction) const
191  { return eval(target, prediction); }
192 
193  using base_type::operator();
194 };
195 
196 
197 }
198 #endif