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-2015 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://image.diku.dk/shark/>
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>
39 namespace shark {
40 
41 
42 /// \brief Loss function interface
43 ///
44 /// \par
45 /// In statistics and machine learning, a loss function encodes
46 /// the severity of getting a label wrong. This is am important
47 /// special case of a cost function (see AbstractCost), where
48 /// the cost is computed as the average loss over a set, also
49 /// known as (empirical) risk.
50 ///
51 /// \par
52 /// It is generally agreed that loss values are non-negative,
53 /// and that the loss of correct prediction is zero. This rule
54 /// is not formally checked, but instead left to the various
55 /// sub-classes.
56 ///
57 template<class LabelT, class OutputT = LabelT>
58 class AbstractLoss : public AbstractCost<LabelT, OutputT>
59 {
60 public:
62  typedef OutputT OutputType;
63  typedef LabelT LabelType;
65 
68 
71  }
72 
73  /// \brief evaluate the loss for a batch of targets and a prediction
74  ///
75  /// \param target target values
76  /// \param prediction predictions, typically made by a model
77  virtual double eval( BatchLabelType const& target, BatchOutputType const& prediction) const = 0;
78 
79  /// \brief evaluate the loss for a target and a prediction
80  ///
81  /// \param target target value
82  /// \param prediction prediction, typically made by a model
83  virtual double eval( LabelType const& target, OutputType const& prediction)const{
84  BatchLabelType labelBatch = Batch<LabelType>::createBatch(target,1);
85  get(labelBatch,0)=target;
86  BatchOutputType predictionBatch = Batch<OutputType>::createBatch(prediction,1);
87  get(predictionBatch,0)=prediction;
88  return eval(labelBatch,predictionBatch);
89  }
90 
91  /// \brief evaluate the loss and its derivative for a target and a prediction
92  ///
93  /// \param target target value
94  /// \param prediction prediction, typically made by a model
95  /// \param gradient the gradient of the loss function with respect to the prediction
96  virtual double evalDerivative(LabelType const& target, OutputType const& prediction, OutputType& gradient) const {
97  BatchLabelType labelBatch = Batch<LabelType>::createBatch(target,1);
98  get(labelBatch, 0) = target;
99  BatchOutputType predictionBatch = Batch<OutputType>::createBatch(prediction, 1);
100  get(predictionBatch, 0) = prediction;
101  BatchOutputType gradientBatch = Batch<OutputType>::createBatch(gradient, 1);
102  double ret = evalDerivative(labelBatch, predictionBatch, gradientBatch);
103  gradient = get(gradientBatch, 0);
104  return ret;
105  }
106 
107  /// \brief evaluate the loss and its first and second derivative for a target and a prediction
108  ///
109  /// \param target target value
110  /// \param prediction prediction, typically made by a model
111  /// \param gradient the gradient of the loss function with respect to the prediction
112  /// \param hessian the hessian of the loss function with respect to the prediction
113  virtual double evalDerivative(
114  LabelType const& target, OutputType const& prediction,
115  OutputType& gradient,MatrixType & hessian
116  ) const {
118  return 0.0; // dead code, prevent warning
119  }
120 
121  /// \brief evaluate the loss and the derivative w.r.t. the prediction
122  ///
123  /// \par
124  /// The default implementations throws an exception.
125  /// If you overwrite this method, don't forget to set
126  /// the flag HAS_FIRST_DERIVATIVE.
127  /// \param target target value
128  /// \param prediction prediction, typically made by a model
129  /// \param gradient the gradient of the loss function with respect to the prediction
130  virtual double evalDerivative(BatchLabelType const& target, BatchOutputType const& prediction, BatchOutputType& gradient) const
131  {
133  return 0.0; // dead code, prevent warning
134  }
135 
136  //~ /// \brief evaluate the loss and fist and second derivative w.r.t. the prediction
137  //~ ///
138  //~ /// \par
139  //~ /// The default implementations throws an exception.
140  //~ /// If you overwrite this method, don't forget to set
141  //~ /// the flag HAS_FIRST_DERIVATIVE.
142  //~ /// \param target target value
143  //~ /// \param prediction prediction, typically made by a model
144  //~ /// \param gradient the gradient of the loss function with respect to the prediction
145  //~ /// \param hessian the hessian matrix of the loss function with respect to the prediction
146  //~ virtual double evalDerivative(
147  //~ LabelType const& target,
148  //~ OutputType const& prediction,
149  //~ OutputType& gradient,
150  //~ MatrixType& hessian) const
151  //~ {
152  //~ SHARK_FEATURE_EXCEPTION_DERIVED(HAS_SECOND_DERIVATIVE);
153  //~ return 0.0; // dead code, prevent warning
154  //~ }
155 
156  /// from AbstractCost
157  ///
158  /// \param targets target values
159  /// \param predictions predictions, typically made by a model
160  double eval(Data<LabelType> const& targets, Data<OutputType> const& predictions) const{
161  SIZE_CHECK(predictions.numberOfElements() == targets.numberOfElements());
162  SIZE_CHECK(predictions.numberOfBatches() == targets.numberOfBatches());
163  int numBatches = (int) targets.numberOfBatches();
164  double error = 0;
165  SHARK_PARALLEL_FOR(int i = 0; i < numBatches; ++i){
166  double batchError= eval(targets.batch(i),predictions.batch(i));
168  error+=batchError;
169  }
170  }
171  return error / targets.numberOfElements();
172  }
173 
174  /// \brief evaluate the loss for a target and a prediction
175  ///
176  /// \par
177  /// convenience operator
178  ///
179  /// \param target target value
180  /// \param prediction prediction, typically made by a model
181  double operator () (LabelType const& target, OutputType const& prediction) const
182  { return eval(target, prediction); }
183 
184  double operator () (BatchLabelType const& target, BatchOutputType const& prediction) const
185  { return eval(target, prediction); }
186 
187  using base_type::operator();
188 };
189 
190 
191 }
192 #endif