mlpack: a scalable c++ machine learning library
mlpack  2.0.2
softmax_regression.hpp
Go to the documentation of this file.
1 
14 #ifndef mlpack_METHODS_SOFTMAX_REGRESSION_SOFTMAX_REGRESSION_HPP
15 #define mlpack_METHODS_SOFTMAX_REGRESSION_SOFTMAX_REGRESSION_HPP
16 
17 #include <mlpack/core.hpp>
19 
21 
22 namespace mlpack {
23 namespace regression {
24 
64 template<
65  template<typename> class OptimizerType = mlpack::optimization::L_BFGS
66 >
68 {
69  public:
79  SoftmaxRegression(const size_t inputSize,
80  const size_t numClasses,
81  const bool fitIntercept = false);
82 
96  SoftmaxRegression(const arma::mat& data,
97  const arma::Row<size_t>& labels,
98  const size_t numClasses,
99  const double lambda = 0.0001,
100  const bool fitIntercept = false);
101 
111  SoftmaxRegression(OptimizerType<SoftmaxRegressionFunction>& optimizer);
112 
121  void Predict(const arma::mat& testData, arma::Row<size_t>& predictions) const;
122 
131  double ComputeAccuracy(const arma::mat& testData,
132  const arma::Row<size_t>& labels) const;
133 
142  double Train(OptimizerType<SoftmaxRegressionFunction>& optimizer);
143 
151  double Train(const arma::mat &data, const arma::Row<size_t>& labels,
152  const size_t numClasses);
153 
155  size_t& NumClasses() { return numClasses; }
157  size_t NumClasses() const { return numClasses; }
158 
160  double& Lambda() { return lambda; }
162  double Lambda() const { return lambda; }
163 
165  bool FitIntercept() const { return fitIntercept; }
166 
168  arma::mat& Parameters() { return parameters; }
170  const arma::mat& Parameters() const { return parameters; }
171 
173  size_t FeatureSize() const
174  { return fitIntercept ? parameters.n_cols - 1 :
175  parameters.n_cols; }
176 
180  template<typename Archive>
181  void Serialize(Archive& ar, const unsigned int /* version */)
182  {
184 
185  ar & CreateNVP(parameters, "parameters");
186  ar & CreateNVP(numClasses, "numClasses");
187  ar & CreateNVP(lambda, "lambda");
188  ar & CreateNVP(fitIntercept, "fitIntercept");
189  }
190 
191  private:
193  arma::mat parameters;
195  size_t numClasses;
197  double lambda;
200 };
201 
202 } // namespace regression
203 } // namespace mlpack
204 
205 // Include implementation.
206 #include "softmax_regression_impl.hpp"
207 
208 #endif
size_t NumClasses() const
Gets the number of classes.
double & Lambda()
Sets the regularization parameter.
Linear algebra utility functions, generally performed on matrices or vectors.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename boost::enable_if< HasSerialize< T >>::type *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
const arma::mat & Parameters() const
Get the model parameters.
void Predict(const arma::mat &testData, arma::Row< size_t > &predictions) const
Predict the class labels for the provided feature points.
arma::mat & Parameters()
Get the model parameters.
double Train(OptimizerType< SoftmaxRegressionFunction > &optimizer)
Train the softmax regression model with the given optimizer.
Softmax Regression is a classifier which can be used for classification when the data available can t...
size_t & NumClasses()
Sets the number of classes.
double Lambda() const
Gets the regularization parameter.
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
size_t FeatureSize() const
Gets the features size of the training data.
SoftmaxRegression(const size_t inputSize, const size_t numClasses, const bool fitIntercept=false)
Initialize the SoftmaxRegression without performing training.
void Serialize(Archive &ar, const unsigned int)
Serialize the SoftmaxRegression model.
double lambda
L2-regularization constant.
arma::mat parameters
Parameters after optimization.
bool FitIntercept() const
Gets the intercept term flag. We can&#39;t change this after training.
The generic L-BFGS optimizer, which uses a back-tracking line search algorithm to minimize a function...
Definition: lbfgs.hpp:36
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...