AbsoluteLoss.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief implements the absolute loss, which is the distance between labels and predictions
5  *
6  *
7  *
8  *
9  * \author Tobias Glasmachers
10  * \date 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 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_ABSOLUTELOSS_H
34 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_ABSOLUTELOSS_H
35 
36 
38 namespace shark{
39 
40 
41 ///
42 /// \brief absolute loss
43 ///
44 /// The absolute loss is usually defined in a single dimension
45 /// as the absolute value of the difference between labels and
46 /// predictions. Here we generalize to multiple dimensions by
47 /// returning the norm.
48 ///
49 template<class VectorType = RealVector>
50 class AbsoluteLoss : public AbstractLoss<VectorType, VectorType>
51 {
52 public:
56 
57  /// constructor
59  { }
60 
61 
62  /// \brief From INameable: return the class name.
63  std::string name() const
64  { return "AbsoluteLoss"; }
65 
66  // annoyingness of C++ templates
67  using base_type::eval;
68 
69  /// evaluate the loss \f$ \| labels - predictions \| \f$, which
70  /// is a slight generalization of the absolute value of the difference.
71  double eval(BatchLabelType const& labels, BatchOutputType const& predictions) const{
72  SIZE_CHECK(labels.size1() == predictions.size1());
73  SIZE_CHECK(labels.size2() == predictions.size2());
74 
75  double error = 0;
76  for(std::size_t i = 0; i != labels.size1(); ++i){
77  error+=blas::distance(row(predictions,i),row(labels,i));
78  }
79  return error;
80  }
81 };
82 
83 
84 }
85 #endif