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... | |
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... | |
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... | |
template < typename Archive > | |
void | serialize (Archive &ar, const uint32_t) |
Serialize the SoftmaxRegression model. 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... | |
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:
Definition at line 59 of file softmax_regression.hpp.
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.
inputSize | Size of the input feature vector. |
numClasses | Number of classes for classification. |
fitIntercept | add intercept term or not. |
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.
OptimizerType | Desired optimizer type. |
data | Input training features. Each column associate with one sample |
labels | Labels associated with the feature data. |
numClasses | Number of classes for classification. |
optimizer | Desired optimizer. |
lambda | L2-regularization constant. |
fitIntercept | add intercept term or not. |
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.
OptimizerType | Desired optimizer type. |
CallbackTypes | Types of Callback Functions. |
data | Input training features. Each column associate with one sample |
labels | Labels associated with the feature data. |
numClasses | Number of classes for classification. |
lambda | L2-regularization constant. |
fitIntercept | add intercept term or not. |
optimizer | Desired optimizer. |
callbacks | Callback function for ensmallen optimizer OptimizerType . See https://www.ensmallen.org/docs.html#callback-documentation. |
void 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.
dataset | Set of points to classify. |
labels | Predicted labels for each point. |
size_t 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.
point | Point to be classified. |
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.
The function calculates the probabilities for every class, given a data point. It then chooses the class which has the highest probability among all.
dataset | Matrix of data points to be classified. |
labels | Predicted labels for each point. |
probabilities | Class probabilities for each point. |
void Classify | ( | const arma::mat & | dataset, |
arma::mat & | probabilities | ||
) | const |
Classify the given points, returning class probabilities for each point.
dataset | Matrix of data points to be classified. |
probabilities | Class probabilities for each point. |
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.
Predictions are made using the provided data and are compared with the actual labels.
testData | Matrix of data points using which predictions are made. |
labels | Vector of labels associated with the data. |
|
inline |
Gets the features size of the training data.
Definition at line 227 of file softmax_regression.hpp.
|
inline |
Gets the intercept term flag. We can't change this after training.
Definition at line 219 of file softmax_regression.hpp.
|
inline |
Sets the regularization parameter.
Definition at line 214 of file softmax_regression.hpp.
|
inline |
Gets the regularization parameter.
Definition at line 216 of file softmax_regression.hpp.
|
inline |
Sets the number of classes.
Definition at line 209 of file softmax_regression.hpp.
|
inline |
Gets the number of classes.
Definition at line 211 of file softmax_regression.hpp.
|
inline |
Get the model parameters.
Definition at line 222 of file softmax_regression.hpp.
|
inline |
Get the model parameters.
Definition at line 224 of file softmax_regression.hpp.
|
inline |
Serialize the SoftmaxRegression model.
Definition at line 235 of file softmax_regression.hpp.
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.
OptimizerType | Desired optimizer type. |
data | Input data with each column as one example. |
labels | Labels associated with the feature data. |
numClasses | Number of classes for classification. |
optimizer | Desired optimizer. |
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.
OptimizerType | Desired optimizer type. |
CallbackTypes | Types of Callback Functions. |
data | Input data with each column as one example. |
labels | Labels associated with the feature data. |
numClasses | Number of classes for classification. |
optimizer | Desired optimizer. |
callbacks | Callback function for ensmallen optimizer OptimizerType . See https://www.ensmallen.org/docs.html#callback-documentation. |