softmax_regression.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_SOFTMAX_REGRESSION_SOFTMAX_REGRESSION_HPP
13 #define MLPACK_METHODS_SOFTMAX_REGRESSION_SOFTMAX_REGRESSION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 #include <ensmallen.hpp>
17 
19 
20 namespace mlpack {
21 namespace regression {
22 
60 {
61  public:
71  SoftmaxRegression(const size_t inputSize = 0,
72  const size_t numClasses = 0,
73  const bool fitIntercept = false);
74 
90  template<typename OptimizerType = ens::L_BFGS>
91  SoftmaxRegression(const arma::mat& data,
92  const arma::Row<size_t>& labels,
93  const size_t numClasses,
94  const double lambda = 0.0001,
95  const bool fitIntercept = false,
96  OptimizerType optimizer = OptimizerType());
97 
107  void Classify(const arma::mat& dataset, arma::Row<size_t>& labels) const;
108 
117  template<typename VecType>
118  size_t Classify(const VecType& point) const;
119 
131  void Classify(const arma::mat& dataset,
132  arma::Row<size_t>& labels,
133  arma::mat& probabilites) const;
134 
141  void Classify(const arma::mat& dataset,
142  arma::mat& probabilities) const;
143 
152  double ComputeAccuracy(const arma::mat& testData,
153  const arma::Row<size_t>& labels) const;
154 
165  template<typename OptimizerType = ens::L_BFGS>
166  double Train(const arma::mat& data,
167  const arma::Row<size_t>& labels,
168  const size_t numClasses,
169  OptimizerType optimizer = OptimizerType());
170 
172  size_t& NumClasses() { return numClasses; }
174  size_t NumClasses() const { return numClasses; }
175 
177  double& Lambda() { return lambda; }
179  double Lambda() const { return lambda; }
180 
182  bool FitIntercept() const { return fitIntercept; }
183 
185  arma::mat& Parameters() { return parameters; }
187  const arma::mat& Parameters() const { return parameters; }
188 
190  size_t FeatureSize() const
191  { return fitIntercept ? parameters.n_cols - 1 :
192  parameters.n_cols; }
193 
197  template<typename Archive>
198  void serialize(Archive& ar, const unsigned int /* version */)
199  {
200  ar & BOOST_SERIALIZATION_NVP(parameters);
201  ar & BOOST_SERIALIZATION_NVP(numClasses);
202  ar & BOOST_SERIALIZATION_NVP(lambda);
203  ar & BOOST_SERIALIZATION_NVP(fitIntercept);
204  }
205 
206  private:
208  arma::mat parameters;
210  size_t numClasses;
212  double lambda;
214  bool fitIntercept;
215 };
216 
217 } // namespace regression
218 } // namespace mlpack
219 
220 // Include implementation.
221 #include "softmax_regression_impl.hpp"
222 
223 #endif
SoftmaxRegression(const size_t inputSize=0, const size_t numClasses=0, const bool fitIntercept=false)
Initialize the SoftmaxRegression without performing training.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Lambda() const
Gets the regularization parameter.
bool FitIntercept() const
Gets the intercept term flag. We can&#39;t change this after training.
size_t NumClasses() const
Gets the number of classes.
double Train(const arma::mat &data, const arma::Row< size_t > &labels, const size_t numClasses, OptimizerType optimizer=OptimizerType())
Train the softmax regression with the given training data.
Softmax Regression is a classifier which can be used for classification when the data available can t...
arma::mat & Parameters()
Get the model parameters.
void serialize(Archive &ar, const unsigned int)
Serialize the SoftmaxRegression model.
double ComputeAccuracy(const arma::mat &testData, const arma::Row< size_t > &labels) const
Computes accuracy of the learned model given the feature data and the labels associated with each dat...
double & Lambda()
Sets the regularization parameter.
size_t FeatureSize() const
Gets the features size of the training data.
size_t & NumClasses()
Sets the number of classes.
void Classify(const arma::mat &dataset, arma::Row< size_t > &labels) const
Classify the given points, returning the predicted labels for each point.
const arma::mat & Parameters() const
Get the model parameters.