mlpack
Public Member Functions | List of all members
mlpack::regression::SoftmaxRegression Class Reference

Softmax Regression is a classifier which can be used for classification when the data available can take two or more class values. More...

#include <softmax_regression.hpp>

Public Member Functions

 SoftmaxRegression (const size_t inputSize=0, const size_t numClasses=0, const bool fitIntercept=false)
 Initialize the SoftmaxRegression without performing training. More...
 
template<typename OptimizerType = ens::L_BFGS>
 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. More...
 
template<typename OptimizerType , typename... CallbackTypes>
 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. More...
 
void Classify (const arma::mat &dataset, arma::Row< size_t > &labels) const
 Classify the given points, returning the predicted labels for each point. More...
 
template<typename VecType >
size_t Classify (const VecType &point) const
 Classify the given point. More...
 
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. More...
 
void Classify (const arma::mat &dataset, arma::mat &probabilities) const
 Classify the given points, returning class probabilities for each point. More...
 
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 data point. More...
 
template<typename OptimizerType = ens::L_BFGS>
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. More...
 
template<typename OptimizerType = ens::L_BFGS, typename... CallbackTypes>
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. More...
 
size_t & NumClasses ()
 Sets the number of classes.
 
size_t NumClasses () const
 Gets the number of classes.
 
double & Lambda ()
 Sets the regularization parameter.
 
double Lambda () const
 Gets the regularization parameter.
 
bool FitIntercept () const
 Gets the intercept term flag. We can't change this after training.
 
arma::mat & Parameters ()
 Get the model parameters.
 
const arma::mat & Parameters () const
 Get the model parameters.
 
size_t FeatureSize () const
 Gets the features size of the training data.
 
template<typename Archive >
void serialize (Archive &ar, const uint32_t)
 Serialize the SoftmaxRegression model.
 

Detailed Description

Softmax Regression is a classifier which can be used for classification when the data available can take two or more class values.

It is a generalization of Logistic Regression (which is used only for binary classification). The model has a different set of parameters for each class, but can be easily converted into a vectorized implementation as has been done in this module. The model can be used for direct classification of feature data or in conjunction with unsupervised learning methods. More technical details about the model can be found on the following webpage:

http://ufldl.stanford.edu/wiki/index.php/Softmax_Regression

An example on how to use the interface is shown below:

arma::mat trainData; // Training data matrix.
arma::Row<size_t> labels; // Labels associated with the data.
const size_t inputSize = 1000; // Size of input feature vector.
const size_t numClasses = 10; // Number of classes.
const double lambda = 0.0001; // L2-Regularization parameter.
const size_t numBasis = 5; // Parameter required for L-BFGS algorithm.
const size_t numIterations = 100; // Maximum number of iterations.
// Train the model using an instantiated optimizer for the training.
SoftmaxRegression regressor(trainData.n_rows, numClasses);
ens::L_BFGS optimizer(numBasis, numIterations);
regressor.Train(trainData, labels, numClasses, std::move(optimizer));
arma::mat testData; // Test data matrix.
arma::Row<size_t> predictions; // Vectors to store predictions in.
// Obtain predictions from both the learned models.
regressor.Classify(testData, predictions);

Constructor & Destructor Documentation

◆ SoftmaxRegression() [1/3]

mlpack::regression::SoftmaxRegression::SoftmaxRegression ( const size_t  inputSize = 0,
const size_t  numClasses = 0,
const bool  fitIntercept = false 
)

Initialize the SoftmaxRegression without performing training.

Default value of lambda is 0.0001. Be sure to use Train() before calling Classify() or ComputeAccuracy(), otherwise the results may be meaningless.

Parameters
inputSizeSize of the input feature vector.
numClassesNumber of classes for classification.
fitInterceptadd intercept term or not.

◆ SoftmaxRegression() [2/3]

template<typename OptimizerType >
mlpack::regression::SoftmaxRegression::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.

This will train the model. Optionally, the parameter 'lambda' can be passed, which controls the amount of L2-regularization in the objective function. By default, the model takes a small value.

Template Parameters
OptimizerTypeDesired optimizer type.
Parameters
dataInput training features. Each column associate with one sample
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
optimizerDesired optimizer.
lambdaL2-regularization constant.
fitInterceptadd intercept term or not.

◆ SoftmaxRegression() [3/3]

template<typename OptimizerType , typename... CallbackTypes>
mlpack::regression::SoftmaxRegression::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.

This will train the model. Optionally, the parameter 'lambda' can be passed, which controls the amount of L2-regularization in the objective function. By default, the model takes a small value.

Template Parameters
OptimizerTypeDesired optimizer type.
CallbackTypesTypes of Callback Functions.
Parameters
dataInput training features. Each column associate with one sample
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
lambdaL2-regularization constant.
fitInterceptadd intercept term or not.
optimizerDesired optimizer.
callbacksCallback function for ensmallen optimizer OptimizerType. See https://www.ensmallen.org/docs.html#callback-documentation.

Member Function Documentation

◆ Classify() [1/4]

void mlpack::regression::SoftmaxRegression::Classify ( const arma::mat &  dataset,
arma::Row< size_t > &  labels 
) const

Classify the given points, returning the predicted labels for each point.

The function calculates the probabilities for every class, given a data point. It then chooses the class which has the highest probability among all.

Parameters
datasetSet of points to classify.
labelsPredicted labels for each point.

◆ Classify() [2/4]

template<typename VecType >
size_t mlpack::regression::SoftmaxRegression::Classify ( const VecType &  point) const

Classify the given point.

The predicted class label is returned. The function calculates the probabilites for every class, given the point. It then chooses the class which has the highest probability among all.

Parameters
pointPoint to be classified.
Returns
Predicted class label of the point.

◆ Classify() [3/4]

void mlpack::regression::SoftmaxRegression::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.

The function calculates the probabilities for every class, given a data point. It then chooses the class which has the highest probability among all.

Parameters
datasetMatrix of data points to be classified.
labelsPredicted labels for each point.
probabilitiesClass probabilities for each point.

◆ Classify() [4/4]

void mlpack::regression::SoftmaxRegression::Classify ( const arma::mat &  dataset,
arma::mat &  probabilities 
) const

Classify the given points, returning class probabilities for each point.

Parameters
datasetMatrix of data points to be classified.
probabilitiesClass probabilities for each point.

◆ ComputeAccuracy()

double mlpack::regression::SoftmaxRegression::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 data point.

Predictions are made using the provided data and are compared with the actual labels.

Parameters
testDataMatrix of data points using which predictions are made.
labelsVector of labels associated with the data.

◆ Train() [1/2]

template<typename OptimizerType >
double mlpack::regression::SoftmaxRegression::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.

Template Parameters
OptimizerTypeDesired optimizer type.
Parameters
dataInput data with each column as one example.
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
optimizerDesired optimizer.
Returns
Objective value of the final point.

◆ Train() [2/2]

template<typename OptimizerType , typename... CallbackTypes>
double mlpack::regression::SoftmaxRegression::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.

Template Parameters
OptimizerTypeDesired optimizer type.
CallbackTypesTypes of Callback Functions.
Parameters
dataInput data with each column as one example.
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
optimizerDesired optimizer.
callbacksCallback function for ensmallen optimizer OptimizerType. See https://www.ensmallen.org/docs.html#callback-documentation.
Returns
Objective value of the final point.

The documentation for this class was generated from the following files: