AbstractWeightedTrainer.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Abstract Trainer Interface for trainers that support weighting
6  *
7  *
8  *
9  * \author O. Krause
10  * \date 2014
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_ALGORITHMS_TRAINERS_ABSTRACT_WEIGHTED_TRAINER_H
35 #define SHARK_ALGORITHMS_TRAINERS_ABSTRACT_WEIGHTED_TRAINER_H
36 
39 
40 namespace shark {
41 
42 
43 /// \brief Superclass of weighted supervised learning algorithms
44 ///
45 /// \par
46 /// AbstractWeightedTrainer is the super class of all trainers
47 /// that support weighted datasets. Weights are interpreted here
48 /// as the importance of a sample. unweighted training assumes
49 /// that all samples have the same importance, or weight.
50 /// The higher the weight, the more important a point. Weight
51 /// 0 is the same as if the point would not be part of the dataset.
52 /// Negative weights are not allowed.
53 ///
54 /// When all weights are integral values there is a simple interpretation
55 /// of the weights as the multiplicity of a point. Thus training
56 /// with a dataset with duplicate points is the same as counting the duplicates
57 /// and run the algorithm with a weighted dataset where all points are unique and
58 /// have their weight is the multiplicity.
59 template <class Model, class LabelTypeT = typename Model::OutputType>
60 class AbstractWeightedTrainer : public AbstractTrainer<Model,LabelTypeT>
61 {
62 private:
64 public:
65  typedef typename base_type::ModelType ModelType;
66  typedef typename base_type::InputType InputType;
67  typedef typename base_type::LabelType LabelType;
70 
71  /// \brief Executes the algorithm and trains a model on the given weighted data.
72  virtual void train(ModelType& model, WeightedDatasetType const& dataset) = 0;
73 
74  /// \brief Executes the algorithm and trains a model on the given unweighted data.
75  ///
76  /// This method behaves as using train with a weighted dataset where all weights are equal.
77  /// The default implementation just creates such a dataset and executes the weighted
78  /// version of the algorithm.
79  virtual void train(ModelType& model, DatasetType const& dataset){
80  train(model,WeightedDatasetType(dataset, 1.0));
81  }
82 };
83 
84 
85 /// \brief Superclass of weighted unsupervised learning algorithms
86 ///
87 /// \par
88 /// AbstractWeightedUnsupervisedTrainer is the super class of all trainers
89 /// that support weighted datasets. See AbstractWeightedTrainer for more information on
90 /// the weights.
91 /// \see AbstractWeightedTrainer
92 template <class Model>
94 {
95 private:
97 public:
98  typedef typename base_type::ModelType ModelType;
99  typedef typename base_type::InputType InputType;
102 
103  /// \brief Excecutes the algorithm and trains a model on the given weighted data.
104  virtual void train(ModelType& model, WeightedDatasetType const& dataset) = 0;
105 
106  /// \brief Excecutes the algorithm and trains a model on the given undata.
107  ///
108  /// This method behaves as using train with a weighted dataset where all weights are equal.
109  /// The default implementation just creates such a dataset and executes the weighted
110  /// version of the algorithm.
111  virtual void train(ModelType& model, DatasetType const& dataset){
112  train(model, WeightedDatasetType(dataset, 1.0));
113  }
114 };
115 
116 
117 }
118 #endif