Converter.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Format conversion models
6  *
7  * \par
8  * This file provides a number of parameter-free models
9  * performing format conversions. The classes are intended
10  * to be used in two ways: First, they can be used to convert
11  * data stored in Set or Datasets objects to different formats.
12  * Second, they can be appended to another model by means of
13  * the ConcatenatedModel class.
14  *
15  * \par
16  * The need for converter models arises in particular for
17  * classification problems. There are at least three encodings
18  * of class labels in common use. Assume there are d classes in
19  * the problem, then it is natural to use integers
20  * \f$ 0, \dots, d-1 \f$. Neural networks usually use a one-hot
21  * encoding, with a unit vector representing the class label.
22  * This encoding has the advantage that it naturally generalizes
23  * to encoding probabilities over class labels, and thus allows
24  * for objective functions like cross-entropy for model training.
25  * The third encoding in common use, both in support vector
26  * machines and neural networks, is a thresholded real value
27  * representing one out of d=2 classes. Within Shark we
28  * consistently use the data types unsigned int for the first
29  * and RealVector for the latter two cases, such that format
30  * conversions can focus on essential differences in encoding
31  * only. The models in this file allow for the most important
32  * conversions between these three encodings.
33  *
34  * \author T. Glasmachers
35  * \date 2010
36  *
37  *
38  * \par Copyright 1995-2017 Shark Development Team
39  *
40  * <BR><HR>
41  * This file is part of Shark.
42  * <http://shark-ml.org/>
43  *
44  * Shark is free software: you can redistribute it and/or modify
45  * it under the terms of the GNU Lesser General Public License as published
46  * by the Free Software Foundation, either version 3 of the License, or
47  * (at your option) any later version.
48  *
49  * Shark is distributed in the hope that it will be useful,
50  * but WITHOUT ANY WARRANTY; without even the implied warranty of
51  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52  * GNU Lesser General Public License for more details.
53  *
54  * You should have received a copy of the GNU Lesser General Public License
55  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
56  *
57  */
58 //===========================================================================
59 
60 #ifndef SHARK_ML_MODEL_CONVERTER_H
61 #define SHARK_ML_MODEL_CONVERTER_H
62 
63 #include <shark/Core/DLLSupport.h>
65 namespace shark {
66 
67 
68 ///
69 /// \brief Convertion of real-valued outputs to classes 0 or 1
70 ///
71 /// \par
72 /// The ThresholdConverter is a parameter-free model converting its
73 /// real-valued input to a binary class label 0 or 1 by means of a
74 /// threshold operation. Values above the threshold result in class 1,
75 /// values equal to or below the threshold are converted to class 0.
76 /// Ths threshold takes a default value of zero, which is adjusted to
77 /// the case of a (linear or tanh) output neuron of a neural network,
78 /// and to a binary support vector machine.
79 ///
80 class ThresholdConverter : public AbstractModel<RealVector, unsigned int>
81 {
82 public:
83  SHARK_EXPORT_SYMBOL ThresholdConverter(double threshold = 0.0);
84 
85  /// \brief From INameable: return the class name.
86  std::string name() const
87  { return "ThresholdConverter"; }
88 
89  SHARK_EXPORT_SYMBOL RealVector parameterVector() const;
90  SHARK_EXPORT_SYMBOL void setParameterVector(RealVector const& newParameters);
91  SHARK_EXPORT_SYMBOL std::size_t numberOfParameters() const;
92 
93  boost::shared_ptr<State> createState()const{
94  return boost::shared_ptr<State>(new EmptyState());
95  }
96  SHARK_EXPORT_SYMBOL void eval(BatchInputType const& patterns, BatchOutputType& outputs)const;
97  void eval(BatchInputType const& patterns, BatchOutputType& outputs, State& state)const{
98  eval(patterns,outputs);
99  }
101 
102 protected:
103  double m_threshold;
104 };
105 
106 ///
107 /// \brief Convertion of real-vector outputs to vectors of class labels 0 or 1
108 ///
109 /// \par
110 /// The ThresholdVectorConverter is a parameter-free model converting its
111 /// real-valued inputs to binary class labels 0 or 1 by means of a
112 /// threshold operation. Values above the threshold result in class 1,
113 /// values equal to or below the threshold are converted to class 0.
114 /// Ths threshold takes a default value of zero, which is adjusted to
115 /// the case of a (linear or tanh) output neuron of a neural network,
116 /// and to a binary support vector machine.
117 ///
118 class ThresholdVectorConverter : public AbstractModel<RealVector, RealVector>
119 {
120 public:
121  SHARK_EXPORT_SYMBOL ThresholdVectorConverter(double threshold = 0.0);
122 
123  /// \brief From INameable: return the class name.
124  std::string name() const
125  { return "ThresholdVectorConverter"; }
126 
127  SHARK_EXPORT_SYMBOL RealVector parameterVector() const;
128  SHARK_EXPORT_SYMBOL void setParameterVector(RealVector const& newParameters);
129  SHARK_EXPORT_SYMBOL std::size_t numberOfParameters() const;
130 
131  boost::shared_ptr<State> createState()const{
132  return boost::shared_ptr<State>(new EmptyState());
133  }
134 
135  SHARK_EXPORT_SYMBOL void eval(BatchInputType const& patterns, BatchOutputType& outputs)const;
136  void eval(BatchInputType const& patterns, BatchOutputType& outputs, State& state)const{
137  eval(patterns,outputs);
138  }
140 
141 protected:
142  double m_threshold;
143 };
144 
145 
146 ///
147 /// \brief Conversion of real-valued outputs to classes
148 ///
149 /// \par
150 /// The ArgMaxConverter is a model converting the
151 /// real-valued vector output of an underlying decision function to a
152 /// class label 0, ..., d-1 by means of an arg-max operation.
153 /// The class returns the argument of the maximal
154 /// input component as its output. This convertion is adjusted to
155 /// interpret the output of a neural network or a support vector
156 /// machine for multi-category classification.
157 ///
158 /// In the special case that d is 1, it is assumed that the model can be represented as
159 /// a 2 d vector with both components having the same value but opposite sign.
160 /// In this case the behavior is equivalent to the threshold converter with threshold 0.
161 ///
162 /// The underlying decision function is an arbitrary model. It should
163 /// be default constructable and it can be accessed using decisionFunction().
164 /// The parameters of the ArgMaxConverter are the ones of the decision function.
165 template<class Model>
166 class ArgMaxConverter : public AbstractModel<typename Model::InputType, unsigned int>
167 {
168 private:
169  typedef typename Model::BatchOutputType ModelBatchOutputType;
170 public:
171  typedef typename Model::InputType InputType;
172  typedef unsigned int OutputType;
175 
177  { }
178  ArgMaxConverter(Model const& decisionFunction)
179  : m_decisionFunction(decisionFunction)
180  { }
181 
182  std::string name() const
183  { return "ArgMaxConverter<"+m_decisionFunction.name()+">"; }
184 
185  RealVector parameterVector() const{
186  return m_decisionFunction.parameterVector();
187  }
188 
189  void setParameterVector(RealVector const& newParameters){
190  m_decisionFunction.setParameterVector(newParameters);
191  }
192 
193  std::size_t numberOfParameters() const{
194  return m_decisionFunction.numberOfParameters();
195  }
196 
197  /// \brief Return the decision function
198  Model const& decisionFunction()const{
199  return m_decisionFunction;
200  }
201 
202  /// \brief Return the decision function
204  return m_decisionFunction;
205  }
206 
207  void eval(BatchInputType const& input, BatchOutputType& output)const{
208 
209  ModelBatchOutputType modelResult;
210  m_decisionFunction.eval(input,modelResult);
211  std::size_t batchSize = modelResult.size1();
212  output.resize(batchSize);
213  if(modelResult.size2()== 1)
214  {
215  for(std::size_t i = 0; i != batchSize; ++i){
216  output(i) = modelResult(i,0) > 0.0;
217  }
218  }
219  else{
220  for(std::size_t i = 0; i != batchSize; ++i){
221  output(i) = static_cast<unsigned int>(arg_max(row(modelResult,i)));
222  }
223  }
224  }
225  void eval(BatchInputType const& input, BatchOutputType& output, State& state)const{
226  eval(input,output);
227  }
228 
229  void eval(InputType const & pattern, OutputType& output)const{
230  typename Model::OutputType modelResult;
231  m_decisionFunction.eval(pattern,modelResult);
232  if(modelResult.size()== 1){
233  output = modelResult(0) > 0.0;
234  }
235  else{
236  output = static_cast<unsigned int>(arg_max(modelResult));
237  }
238  }
239 
240  /// From ISerializable
241  void read(InArchive& archive){
242  archive >> m_decisionFunction;
243  }
244  /// From ISerializable
245  void write(OutArchive& archive) const{
246  archive << m_decisionFunction;
247  }
248 
249 private:
250  Model m_decisionFunction;
251 };
252 
253 };
254 #endif