mlpack  3.4.2
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);
88  template<typename OptimizerType = ens::L_BFGS>
89  SoftmaxRegression(const arma::mat& data,
90  const arma::Row<size_t>& labels,
91  const size_t numClasses,
92  const double lambda = 0.0001,
93  const bool fitIntercept = false,
94  OptimizerType optimizer = OptimizerType());
112  template<typename OptimizerType, typename... CallbackTypes>
113  SoftmaxRegression(const arma::mat& data,
114  const arma::Row<size_t>& labels,
115  const size_t numClasses,
116  const double lambda,
117  const bool fitIntercept,
118  OptimizerType optimizer,
119  CallbackTypes&&... callbacks);
128  void Classify(const arma::mat& dataset, arma::Row<size_t>& labels) const;
136  template<typename VecType>
137  size_t Classify(const VecType& point) const;
138 
150  void Classify(const arma::mat& dataset,
151  arma::Row<size_t>& labels,
152  arma::mat& probabilities) const;
153 
160  void Classify(const arma::mat& dataset,
161  arma::mat& probabilities) const;
162 
171  double ComputeAccuracy(const arma::mat& testData,
172  const arma::Row<size_t>& labels) const;
183  template<typename OptimizerType = ens::L_BFGS>
184  double Train(const arma::mat& data,
185  const arma::Row<size_t>& labels,
186  const size_t numClasses,
187  OptimizerType optimizer = OptimizerType());
201  template<typename OptimizerType = ens::L_BFGS, typename... CallbackTypes>
202  double Train(const arma::mat& data,
203  const arma::Row<size_t>& labels,
204  const size_t numClasses,
205  OptimizerType optimizer,
206  CallbackTypes&&... callbacks);
207 
209  size_t& NumClasses() { return numClasses; }
211  size_t NumClasses() const { return numClasses; }
212 
214  double& Lambda() { return lambda; }
216  double Lambda() const { return lambda; }
217 
219  bool FitIntercept() const { return fitIntercept; }
220 
222  arma::mat& Parameters() { return parameters; }
224  const arma::mat& Parameters() const { return parameters; }
225 
227  size_t FeatureSize() const
228  { return fitIntercept ? parameters.n_cols - 1:
229  parameters.n_cols; }
230 
234  template<typename Archive>
235  void serialize(Archive& ar, const unsigned int /* version */)
236  {
237  ar & BOOST_SERIALIZATION_NVP(parameters);
238  ar & BOOST_SERIALIZATION_NVP(numClasses);
239  ar & BOOST_SERIALIZATION_NVP(lambda);
240  ar & BOOST_SERIALIZATION_NVP(fitIntercept);
241  }
242 
243  private:
245  arma::mat parameters;
247  size_t numClasses;
249  double lambda;
251  bool fitIntercept;
252 };
253 
254 } // namespace regression
255 } // namespace mlpack
256 
257 // Include implementation.
258 #include "softmax_regression_impl.hpp"
259 
260 #endif
Softmax Regression is a classifier which can be used for classification when the data available can t...
arma::mat & Parameters()
Get the model parameters.
size_t NumClasses() const
Gets 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.
bool FitIntercept() const
Gets the intercept term flag. We can't change this after training.
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.
double Lambda() const
Gets the regularization parameter.
void Classify(const arma::mat &dataset, arma::Row< size_t > &labels, arma::mat &probabilities) const
Classify the given points, returning class probabilities and predicted class label for each point.
void Classify(const arma::mat &dataset, arma::mat &probabilities) const
Classify the given points, returning class probabilities for each point.
size_t FeatureSize() const
Gets the features size of the training data.
SoftmaxRegression(const arma::mat &data, const arma::Row< size_t > &labels, const size_t numClasses, const double lambda, const bool fitIntercept, OptimizerType optimizer, CallbackTypes &&... callbacks)
Construct the SoftmaxRegression class with the provided data and labels.
SoftmaxRegression(const arma::mat &data, const arma::Row< size_t > &labels, const size_t numClasses, const double lambda=0.0001, const bool fitIntercept=false, OptimizerType optimizer=OptimizerType())
Construct the SoftmaxRegression class with the provided data and labels.
size_t Classify(const VecType &point) const
Classify the given point.
const arma::mat & Parameters() const
Get the model parameters.
double & Lambda()
Sets the regularization parameter.
SoftmaxRegression(const size_t inputSize=0, const size_t numClasses=0, const bool fitIntercept=false)
Initialize the SoftmaxRegression without performing training.
size_t & NumClasses()
Sets the number of classes.
double Train(const arma::mat &data, const arma::Row< size_t > &labels, const size_t numClasses, OptimizerType optimizer, CallbackTypes &&... callbacks)
Train the softmax regression with the given training data.
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...
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.