mlpack
Public Member Functions | Static Public Member Functions | List of all members
mlpack::svm::LinearSVMFunction< MatType > Class Template Reference

The hinge loss function for the linear SVM objective function. More...

#include <linear_svm_function.hpp>

Public Member Functions

 LinearSVMFunction (const MatType &dataset, const arma::Row< size_t > &labels, const size_t numClasses, const double lambda=0.0001, const double delta=1.0, const bool fitIntercept=false)
 Construct the Linear SVM objective function with given parameters. 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...
 
double Evaluate (const arma::mat &parameters)
 Evaluate the hinge loss function for all the datapoints. More...
 
double Evaluate (const arma::mat &parameters, const size_t firstId, const size_t batchSize=1)
 Evaluate the hinge loss function on the specified datapoints. More...
 
template<typename GradType >
void Gradient (const arma::mat &parameters, GradType &gradient)
 Evaluate the gradient of the hinge loss function following the LinearFunctionType requirements on the Gradient function. More...
 
template<typename GradType >
void Gradient (const arma::mat &parameters, const size_t firstId, GradType &gradient, const size_t batchSize=1)
 Evaluate the gradient of the hinge loss function, following the LinearFunctionType requirements on the Gradient function. More...
 
template<typename GradType >
double EvaluateWithGradient (const arma::mat &parameters, GradType &gradient) const
 Evaluate the gradient of the hinge loss function, following the LinearFunctionType requirements on the Gradient function followed by evaluation of the hinge loss function on all the datapoints. More...
 
template<typename GradType >
double EvaluateWithGradient (const arma::mat &parameters, const size_t firstId, GradType &gradient, const size_t batchSize=1) const
 Evaluate the gradient of the hinge loss function, following the LinearFunctionType requirements on the Gradient function followed by evaluation of the hinge loss function on the specified datapoints. More...
 
const arma::mat & InitialPoint () const
 Return the initial point for the optimization.
 
arma::mat & InitialPoint ()
 Modify the initial point for the optimization.
 
const arma::sp_mat & Dataset () const
 Get the dataset.
 
arma::sp_mat & Dataset ()
 Modify the dataset.
 
double & Lambda ()
 Sets the regularization parameter.
 
double Lambda () const
 Gets the regularization parameter.
 
bool FitIntercept () const
 Gets the intercept flag.
 
size_t NumFunctions () const
 Return the number of functions.
 

Static Public Member Functions

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

Detailed Description

template<typename MatType = arma::mat>
class mlpack::svm::LinearSVMFunction< MatType >

The hinge loss function for the linear SVM objective function.

This is used by various ensmallen optimizers to train the linear SVM model.

Constructor & Destructor Documentation

◆ LinearSVMFunction()

template<typename MatType >
mlpack::svm::LinearSVMFunction< MatType >::LinearSVMFunction ( const MatType &  dataset,
const arma::Row< size_t > &  labels,
const size_t  numClasses,
const double  lambda = 0.0001,
const double  delta = 1.0,
const bool  fitIntercept = false 
)

Construct the Linear SVM objective function with given parameters.

Parameters
datasetInput training data, each column associate with one sample
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
lambdaL2-regularization constant.
deltaMargin of difference between correct class and other classes.
fitInterceptIntercept term flag.

Member Function Documentation

◆ Evaluate() [1/2]

template<typename MatType >
double mlpack::svm::LinearSVMFunction< MatType >::Evaluate ( const arma::mat &  parameters)

Evaluate the hinge loss function for all the datapoints.

Parameters
parametersThe parameters of the SVM.
Returns
The value of the loss function for the entire dataset.

◆ Evaluate() [2/2]

template<typename MatType >
double mlpack::svm::LinearSVMFunction< MatType >::Evaluate ( const arma::mat &  parameters,
const size_t  firstId,
const size_t  batchSize = 1 
)

Evaluate the hinge loss function on the specified datapoints.

Parameters
parametersThe parameters of the SVM.
firstIdIndex of the datapoints to use for function evaluation.
batchSizeSize of batch to process.
Returns
The value of the loss function for the given parameters.

◆ EvaluateWithGradient() [1/2]

template<typename MatType >
template<typename GradType >
double mlpack::svm::LinearSVMFunction< MatType >::EvaluateWithGradient ( const arma::mat &  parameters,
GradType &  gradient 
) const

Evaluate the gradient of the hinge loss function, following the LinearFunctionType requirements on the Gradient function followed by evaluation of the hinge loss function on all the datapoints.

Template Parameters
GradTypeType of the gradient matrix.
Parameters
parametersThe parameters of the SVM.
gradientLinear matrix to output the gradient into.
Returns
The value of the loss function at the given parameters.

◆ EvaluateWithGradient() [2/2]

template<typename MatType >
template<typename GradType >
double mlpack::svm::LinearSVMFunction< MatType >::EvaluateWithGradient ( const arma::mat &  parameters,
const size_t  firstId,
GradType &  gradient,
const size_t  batchSize = 1 
) const

Evaluate the gradient of the hinge loss function, following the LinearFunctionType requirements on the Gradient function followed by evaluation of the hinge loss function on the specified datapoints.

Template Parameters
GradTypeType of the gradient matrix.
Parameters
parametersThe parameters of the SVM.
firstIdIndex of the datapoint to use for the gradient and function evaluation.
gradientLinear matrix to output the gradient into.
batchSizeSize of the batch to process.
Returns
The value of the loss function at the given parameters.

◆ GetGroundTruthMatrix()

template<typename MatType >
void mlpack::svm::LinearSVMFunction< MatType >::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.

◆ Gradient() [1/2]

template<typename MatType >
template<typename GradType >
void mlpack::svm::LinearSVMFunction< MatType >::Gradient ( const arma::mat &  parameters,
GradType &  gradient 
)

Evaluate the gradient of the hinge loss function following the LinearFunctionType requirements on the Gradient function.

Template Parameters
GradTypeType of the gradient matrix.
Parameters
parametersThe parameters of the SVM.
gradientLinear matrix to output the gradient into.

◆ Gradient() [2/2]

template<typename MatType >
template<typename GradType >
void mlpack::svm::LinearSVMFunction< MatType >::Gradient ( const arma::mat &  parameters,
const size_t  firstId,
GradType &  gradient,
const size_t  batchSize = 1 
)

Evaluate the gradient of the hinge loss function, following the LinearFunctionType requirements on the Gradient function.

Template Parameters
GradTypeType of the gradient matrix.
Parameters
parametersThe parameters of the SVM.
firstIdIndex of the datapoint to use for the gradient evaluation.
gradientLinear matrix to output the gradient into.
batchSizeSize of the batch to process.

◆ InitializeWeights()

template<typename MatType >
void mlpack::svm::LinearSVMFunction< MatType >::InitializeWeights ( arma::mat &  weights,
const size_t  featureSize,
const size_t  numClasses,
const bool  fitIntercept = false 
)
static

Initialize Linear SVM weights (trainable parameters) with the given parameters.

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

Parameters
weightsThis will be filled with the initialized model weights.
featureSizeThe number of features in the training set.
numClassesNumber of classes for classification.
fitInterceptIf true, an intercept is fitted.

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

◆ Shuffle()

template<typename MatType >
void mlpack::svm::LinearSVMFunction< MatType >::Shuffle ( )

Shuffle the dataset.

Shuffle the data.


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