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>
17 
19 
20 namespace mlpack {
21 namespace regression {
22 
63 {
64  public:
74  SoftmaxRegression(const size_t inputSize = 0,
75  const size_t numClasses = 0,
76  const bool fitIntercept = false);
77 
93  template<typename OptimizerType = mlpack::optimization::L_BFGS>
94  SoftmaxRegression(const arma::mat& data,
95  const arma::Row<size_t>& labels,
96  const size_t numClasses,
97  const double lambda = 0.0001,
98  const bool fitIntercept = false,
99  OptimizerType optimizer = OptimizerType());
100 
110  void Classify(const arma::mat& dataset, arma::Row<size_t>& labels) const;
111 
120  template<typename VecType>
121  size_t Classify(const VecType& point) const;
122 
134  void Classify(const arma::mat& dataset,
135  arma::Row<size_t>& labels,
136  arma::mat& probabilites) const;
137 
144  void Classify(const arma::mat& dataset,
145  arma::mat& probabilities) const;
146 
155  double ComputeAccuracy(const arma::mat& testData,
156  const arma::Row<size_t>& labels) const;
157 
168  template<typename OptimizerType = mlpack::optimization::L_BFGS>
169  double Train(const arma::mat& data,
170  const arma::Row<size_t>& labels,
171  const size_t numClasses,
172  OptimizerType optimizer = OptimizerType());
173 
175  size_t& NumClasses() { return numClasses; }
177  size_t NumClasses() const { return numClasses; }
178 
180  double& Lambda() { return lambda; }
182  double Lambda() const { return lambda; }
183 
185  bool FitIntercept() const { return fitIntercept; }
186 
188  arma::mat& Parameters() { return parameters; }
190  const arma::mat& Parameters() const { return parameters; }
191 
193  size_t FeatureSize() const
194  { return fitIntercept ? parameters.n_cols - 1 :
195  parameters.n_cols; }
196 
200  template<typename Archive>
201  void serialize(Archive& ar, const unsigned int /* version */)
202  {
203  ar & BOOST_SERIALIZATION_NVP(parameters);
204  ar & BOOST_SERIALIZATION_NVP(numClasses);
205  ar & BOOST_SERIALIZATION_NVP(lambda);
206  ar & BOOST_SERIALIZATION_NVP(fitIntercept);
207  }
208 
209  private:
211  arma::mat parameters;
213  size_t numClasses;
215  double lambda;
217  bool fitIntercept;
218 };
219 
220 } // namespace regression
221 } // namespace mlpack
222 
223 // Include implementation.
224 #include "softmax_regression_impl.hpp"
225 
226 #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.