mlpack  master
Public Member Functions | Private Attributes | List of all members
mlpack::regression::SoftmaxRegression< OptimizerType > Class Template Reference

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

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...
 
 SoftmaxRegression (const arma::mat &data, const arma::Row< size_t > &labels, const size_t numClasses, const double lambda=0.0001, const bool fitIntercept=false)
 Construct the SoftmaxRegression class with the provided data and labels. More...
 
 SoftmaxRegression (OptimizerType< SoftmaxRegressionFunction > &optimizer)
 Construct the softmax regression model with the given training data. 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...
 
size_t FeatureSize () const
 Gets the features size of the training data. More...
 
bool FitIntercept () const
 Gets the intercept term flag. We can't change this after training. More...
 
double & Lambda ()
 Sets the regularization parameter. More...
 
double Lambda () const
 Gets the regularization parameter. More...
 
size_t & NumClasses ()
 Sets the number of classes. More...
 
size_t NumClasses () const
 Gets the number of classes. More...
 
arma::mat & Parameters ()
 Get the model parameters. More...
 
const arma::mat & Parameters () const
 Get the model parameters. More...
 
void Predict (const arma::mat &testData, arma::Row< size_t > &predictions) const
 Predict the class labels for the provided feature points. More...
 
template<typename Archive >
void Serialize (Archive &ar, const unsigned int)
 Serialize the SoftmaxRegression model. More...
 
double Train (OptimizerType< SoftmaxRegressionFunction > &optimizer)
 Train the softmax regression model with the given optimizer. More...
 
double Train (const arma::mat &data, const arma::Row< size_t > &labels, const size_t numClasses)
 Train the softmax regression with the given training data. More...
 

Private Attributes

bool fitIntercept
 Intercept term flag. More...
 
double lambda
 L2-regularization constant. More...
 
size_t numClasses
 Number of classes. More...
 
arma::mat parameters
 Parameters after optimization. More...
 

Detailed Description

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
class mlpack::regression::SoftmaxRegression< OptimizerType >

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 train_data; // Training data matrix.
arma::vec labels; // Labels associated with the data.
const size_t inputSize = 784; // Size of input feature vector.
const size_t numClasses = 10; // Number of classes.
// Train the model using default options.
SoftmaxRegression<> regressor1(train_data, labels, inputSize, numClasses);
const size_t numBasis = 5; // Parameter required for L-BFGS algorithm.
const size_t numIterations = 100; // Maximum number of iterations.
// Use an instantiated optimizer for the training.
SoftmaxRegressionFunction srf(train_data, labels, inputSize, numClasses);
L_BFGS<SoftmaxRegressionFunction> optimizer(srf, numBasis, numIterations);
SoftmaxRegression<L_BFGS> regressor2(optimizer);
arma::mat test_data; // Test data matrix.
arma::vec predictions1, predictions2; // Vectors to store predictions in.
// Obtain predictions from both the learned models.
regressor1.Predict(test_data, predictions1);
regressor2.Predict(test_data, predictions2);

Definition at line 65 of file softmax_regression.hpp.

Constructor & Destructor Documentation

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
mlpack::regression::SoftmaxRegression< OptimizerType >::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 Predict() 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.
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
mlpack::regression::SoftmaxRegression< OptimizerType >::SoftmaxRegression ( const arma::mat &  data,
const arma::Row< size_t > &  labels,
const size_t  numClasses,
const double  lambda = 0.0001,
const bool  fitIntercept = false 
)

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.

Parameters
dataInput training features. Each column associate with one sample
labelsLabels associated with the feature data.
inputSizeSize of the input feature vector.
numClassesNumber of classes for classification.
lambdaL2-regularization constant.
fitInterceptadd intercept term or not.
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
mlpack::regression::SoftmaxRegression< OptimizerType >::SoftmaxRegression ( OptimizerType< SoftmaxRegressionFunction > &  optimizer)

Construct the softmax regression model with the given training data.

This will train the model. This overload takes an already instantiated optimizer and uses it to train the model. The optimizer should hold an instantiated SoftmaxRegressionFunction object for the function to operate upon. This option should be preferred when the optimizer options are to be changed.

Parameters
optimizerInstantiated optimizer with instantiated error function.

Member Function Documentation

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
double mlpack::regression::SoftmaxRegression< OptimizerType >::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.
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
size_t mlpack::regression::SoftmaxRegression< OptimizerType >::FeatureSize ( ) const
inline
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
bool mlpack::regression::SoftmaxRegression< OptimizerType >::FitIntercept ( ) const
inline

Gets the intercept term flag. We can't change this after training.

Definition at line 163 of file softmax_regression.hpp.

References mlpack::regression::SoftmaxRegression< OptimizerType >::fitIntercept.

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
double& mlpack::regression::SoftmaxRegression< OptimizerType >::Lambda ( )
inline

Sets the regularization parameter.

Definition at line 158 of file softmax_regression.hpp.

References mlpack::regression::SoftmaxRegression< OptimizerType >::lambda.

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
double mlpack::regression::SoftmaxRegression< OptimizerType >::Lambda ( ) const
inline

Gets the regularization parameter.

Definition at line 160 of file softmax_regression.hpp.

References mlpack::regression::SoftmaxRegression< OptimizerType >::lambda.

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
size_t& mlpack::regression::SoftmaxRegression< OptimizerType >::NumClasses ( )
inline

Sets the number of classes.

Definition at line 153 of file softmax_regression.hpp.

References mlpack::regression::SoftmaxRegression< OptimizerType >::numClasses.

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
size_t mlpack::regression::SoftmaxRegression< OptimizerType >::NumClasses ( ) const
inline

Gets the number of classes.

Definition at line 155 of file softmax_regression.hpp.

References mlpack::regression::SoftmaxRegression< OptimizerType >::numClasses.

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
arma::mat& mlpack::regression::SoftmaxRegression< OptimizerType >::Parameters ( )
inline

Get the model parameters.

Definition at line 166 of file softmax_regression.hpp.

References mlpack::regression::SoftmaxRegression< OptimizerType >::parameters.

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
const arma::mat& mlpack::regression::SoftmaxRegression< OptimizerType >::Parameters ( ) const
inline

Get the model parameters.

Definition at line 168 of file softmax_regression.hpp.

References mlpack::regression::SoftmaxRegression< OptimizerType >::parameters.

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
void mlpack::regression::SoftmaxRegression< OptimizerType >::Predict ( const arma::mat &  testData,
arma::Row< size_t > &  predictions 
) const

Predict the class labels for the provided feature points.

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
testDataMatrix of data points for which predictions are to be made.
predictionsVector to store the predictions in.
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
template<typename Archive >
void mlpack::regression::SoftmaxRegression< OptimizerType >::Serialize ( Archive &  ar,
const unsigned  int 
)
inline
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
double mlpack::regression::SoftmaxRegression< OptimizerType >::Train ( OptimizerType< SoftmaxRegressionFunction > &  optimizer)

Train the softmax regression model with the given optimizer.

The optimizer should hold an instantiated SoftmaxRegressionFunction object for the function to operate upon. This option should be preferred when the optimizer options are to be changed.

Parameters
optimizerInstantiated optimizer with instantiated error function.
Returns
Objective value of the final point.
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
double mlpack::regression::SoftmaxRegression< OptimizerType >::Train ( const arma::mat &  data,
const arma::Row< size_t > &  labels,
const size_t  numClasses 
)

Train the softmax regression with the given training data.

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

Member Data Documentation

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
bool mlpack::regression::SoftmaxRegression< OptimizerType >::fitIntercept
private
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
double mlpack::regression::SoftmaxRegression< OptimizerType >::lambda
private
template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
size_t mlpack::regression::SoftmaxRegression< OptimizerType >::numClasses
private

Number of classes.

Definition at line 193 of file softmax_regression.hpp.

Referenced by mlpack::regression::SoftmaxRegression< OptimizerType >::NumClasses().

template<template< typename > class OptimizerType = mlpack::optimization::L_BFGS>
arma::mat mlpack::regression::SoftmaxRegression< OptimizerType >::parameters
private

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