DiscreteLoss.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Flexible error measure for classication tasks
6  *
7  *
8  *
9  * \author T. 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 
34 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_DISCRETELOSS_H
35 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_DISCRETELOSS_H
36 
37 #include <shark/Core/DLLSupport.h>
39 
40 
41 namespace shark {
42 
43 
44 ///
45 /// \brief flexible loss for classification
46 ///
47 /// \par
48 /// The DiscreteLoss class allows for the definition of
49 /// a cost matrix applied to a finite number of classes.
50 /// The cost of correct classification must be zero, all
51 /// other costs must be non-negative.
52 ///
53 /// \par
54 /// Note: As a special case, this loss can be used to provide
55 /// a balanced error signal for unbalanced data sets.
56 ///
57 class DiscreteLoss : public AbstractLoss<unsigned int, unsigned int>
58 {
59 public:
60 
61  /// Constructor
62  /// \param cost cost matrix in the format (target, prediction).
63  SHARK_EXPORT_SYMBOL DiscreteLoss(RealMatrix const& cost);
64 
65 
66  /// \brief From INameable: return the class name.
67  std::string name() const
68  { return "DiscreteLoss"; }
69 
70  /// inherited from AbstractLoss, evaluation of the loss function
71  SHARK_EXPORT_SYMBOL double eval(BatchLabelType const& target, BatchOutputType const& prediction) const;
72 
73  /// Define a new cost structure given by an explicit cost matrix.
74  /// \param cost cost matrix in the format (target, prediction).
75  SHARK_EXPORT_SYMBOL void defineCostMatrix(RealMatrix const& cost);
76 
77  /// Define a new cost structure so that the cost of misclassifying
78  /// a pattern is anti-proportional to the frequency of its class.
79  /// This amounts to balancing the class-wise cost in unbalanced
80  /// data sets (i.e., where one class is far more frequent than
81  /// another).
82  ///
83  /// \param labels label set to which the balanced loss should be adapted
85 
86 protected:
87  /// cost matrix
88  RealMatrix m_cost;
89 };
90 
91 
92 }
93 #endif