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

Public Member Functions

 SoftmaxRegressionFunction (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 Softmax Regression objective function with the given parameters. More...
 
const arma::mat InitializeWeights ()
 Initializes the parameters of the model to suitable values. More...
 
void Shuffle ()
 Shuffle the dataset. More...
 
void GetGroundTruthMatrix (const arma::Row< size_t > &labels, arma::sp_mat &groundTruth)
 Constructs the ground truth label matrix with the passed labels. More...
 
void GetProbabilitiesMatrix (const arma::mat &parameters, arma::mat &probabilities, const size_t start, const size_t batchSize) const
 Evaluate the probabilities matrix with the passed parameters. More...
 
double Evaluate (const arma::mat &parameters) const
 Evaluates the objective function of the softmax regression model using the given parameters. More...
 
double Evaluate (const arma::mat &parameters, const size_t start, const size_t batchSize=1) const
 Evaluate the objective function of the softmax regression model for a subset of the data points using the given parameters. More...
 
void Gradient (const arma::mat &parameters, arma::mat &gradient) const
 Evaluates the gradient values of the objective function given the current set of parameters. More...
 
void Gradient (const arma::mat &parameters, const size_t start, arma::mat &gradient, const size_t batchSize=1) const
 Evaluate the gradient of the objective function given the current set of parameters, on a subset of the data. More...
 
void PartialGradient (const arma::mat &parameters, size_t j, arma::sp_mat &gradient) const
 Evaluates the gradient values of the objective function given the current set of parameters for a single feature indexed by j. More...
 
const arma::mat & GetInitialPoint () const
 Return the initial point for the optimization.
 
size_t NumClasses () const
 Gets the number of classes.
 
size_t NumFeatures () const
 Gets the features size of the training data.
 
size_t NumFunctions () const
 Return the number of separable functions (the number of predictor points).
 
double & Lambda ()
 Sets the regularization parameter.
 
double Lambda () const
 Gets the regularization parameter.
 
bool FitIntercept () const
 Gets the intercept flag.
 

Static Public Member Functions

static const arma::mat InitializeWeights (const size_t featureSize, const size_t numClasses, const bool fitIntercept=false)
 Initialize Softmax Regression weights (trainable parameters) with the given parameters. More...
 
static void InitializeWeights (arma::mat &weights, const size_t featureSize, const size_t numClasses, const bool fitIntercept=false)
 Initialize Softmax Regression weights (trainable parameters) with the given parameters. More...
 

Constructor & Destructor Documentation

◆ SoftmaxRegressionFunction()

SoftmaxRegressionFunction::SoftmaxRegressionFunction ( 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 Softmax Regression objective function with the given parameters.

Parameters
dataInput training data, each column associate with one sample
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
lambdaL2-regularization constant.
fitInterceptIntercept term flag.

Member Function Documentation

◆ Evaluate() [1/2]

double SoftmaxRegressionFunction::Evaluate ( const arma::mat &  parameters) const

Evaluates the objective function of the softmax regression model using the given parameters.

Evaluates the objective function given the parameters.

The cost function has terms for the log likelihood error and the regularization cost. The objective function takes a low value when the model generalizes well for the given training data, while having small parameter values.

Parameters
parametersCurrent values of the model parameters.

◆ Evaluate() [2/2]

double SoftmaxRegressionFunction::Evaluate ( const arma::mat &  parameters,
const size_t  start,
const size_t  batchSize = 1 
) const

Evaluate the objective function of the softmax regression model for a subset of the data points using the given parameters.

Evaluate the objective function for the given points given the parameters.

The cost function has terms for the log likelihood error and the regularization cost. The objective function takes a low value when the model generalizes well for the given training data, while having small parameter values.

Parameters
parametersCurrent values of the model parameters.
startFirst index of the data points to use.
batchSizeNumber of data points to evaluate objective for.

◆ GetGroundTruthMatrix()

void SoftmaxRegressionFunction::GetGroundTruthMatrix ( const arma::Row< size_t > &  labels,
arma::sp_mat &  groundTruth 
)

Constructs the ground truth label matrix with the passed labels.

This is equivalent to applying the indicator function to the training labels.

Parameters
labelsLabels associated with the training data.
groundTruthPointer to arma::mat which stores the computed matrix.

The output is in the form of a matrix, which leads to simpler calculations in the Evaluate() and Gradient() methods.

◆ GetProbabilitiesMatrix()

void SoftmaxRegressionFunction::GetProbabilitiesMatrix ( const arma::mat &  parameters,
arma::mat &  probabilities,
const size_t  start,
const size_t  batchSize 
) const

Evaluate the probabilities matrix with the passed parameters.

Evaluate the probabilities matrix.

probabilities(i, j) = \( exp(\theta_i * data_j) / sum_k(exp(\theta_k * data_j)) \). It represents the probability of data_j belongs to class i.

Parameters
parametersCurrent values of the model parameters.
probabilitiesPointer to arma::mat which stores the probabilities.
startIndex of point to start at.
batchSizeNumber of points to calculate probabilities for.

If fitIntercept flag is true, it should consider the parameters.cols(0) intercept term.

◆ Gradient() [1/2]

void SoftmaxRegressionFunction::Gradient ( const arma::mat &  parameters,
arma::mat &  gradient 
) const

Evaluates the gradient values of the objective function given the current set of parameters.

Calculates and stores the gradient values given a set of parameters.

The function calculates the probabilities for each class given the parameters, and computes the gradients based on the difference from the ground truth.

Parameters
parametersCurrent values of the model parameters.
gradientMatrix where gradient values will be stored.

◆ Gradient() [2/2]

void SoftmaxRegressionFunction::Gradient ( const arma::mat &  parameters,
const size_t  start,
arma::mat &  gradient,
const size_t  batchSize = 1 
) const

Evaluate the gradient of the objective function given the current set of parameters, on a subset of the data.

The function calculates the probabilities for each class given the parameters, and computes the gradients based on the difference from the ground truth.

Parameters
parametersCurrent values of the model parameters.
startFirst index of the data points to use.
gradientMatrix to store gradient into.
batchSizeNumber of data points to evaluate gradient for.

◆ InitializeWeights() [1/3]

const arma::mat SoftmaxRegressionFunction::InitializeWeights ( )

Initializes the parameters of the model to suitable values.

Initializes parameter weights to random values taken from a scaled standard normal distribution.

The weights cannot be initialized to zero, as that will lead to each class output being the same.

◆ InitializeWeights() [2/3]

const arma::mat SoftmaxRegressionFunction::InitializeWeights ( const size_t  featureSize,
const size_t  numClasses,
const bool  fitIntercept = false 
)
static

Initialize Softmax Regression weights (trainable parameters) with the given parameters.

Parameters
featureSizeThe number of features in the training set.
numClassesNumber of classes for classification.
fitInterceptIf true, an intercept is fitted.
Returns
Initialized model weights.

◆ InitializeWeights() [3/3]

void SoftmaxRegressionFunction::InitializeWeights ( arma::mat &  weights,
const size_t  featureSize,
const size_t  numClasses,
const bool  fitIntercept = false 
)
static

Initialize Softmax Regression weights (trainable parameters) with the given parameters.

Parameters
weightsThis will be filled with the initialized model weights.
featureSizeThe number of features in the training set.
numClassesNumber of classes for classification.
fitInterceptIntercept term flag.

◆ PartialGradient()

void SoftmaxRegressionFunction::PartialGradient ( const arma::mat &  parameters,
size_t  j,
arma::sp_mat &  gradient 
) const

Evaluates the gradient values of the objective function given the current set of parameters for a single feature indexed by j.

Parameters
parametersCurrent values of the model parameters.
jThe index of the feature with respect to which the partial gradient is to be computed.
gradientOut param for the gradient value.

◆ Shuffle()

void SoftmaxRegressionFunction::Shuffle ( )

Shuffle the dataset.

Shuffle the data.


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