LinearSVMFunction< MatType > Class Template Reference

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

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...

 
const arma::sp_mat & Dataset () const
 Get the dataset. More...

 
arma::sp_mat & Dataset ()
 Modify the dataset. 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
>
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...

 
bool FitIntercept () const
 Gets the intercept flag. More...

 
void GetGroundTruthMatrix (const arma::Row< size_t > &labels, arma::sp_mat &groundTruth)
 Constructs the ground truth label matrix with the passed labels. 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...

 
const arma::mat & InitialPoint () const
 Return the initial point for the optimization. More...

 
arma::mat & InitialPoint ()
 Modify the initial point for the optimization. More...

 
double & Lambda ()
 Sets the regularization parameter. More...

 
double Lambda () const
 Gets the regularization parameter. More...

 
size_t NumFunctions () const
 Return the number of functions. More...

 
void Shuffle ()
 Shuffle the dataset. More...

 

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.

Definition at line 28 of file linear_svm_function.hpp.

Constructor & Destructor Documentation

◆ LinearSVMFunction()

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

◆ Dataset() [1/2]

const arma::sp_mat& Dataset ( ) const
inline

Get the dataset.

Definition at line 166 of file linear_svm_function.hpp.

◆ Dataset() [2/2]

arma::sp_mat& Dataset ( )
inline

Modify the dataset.

Definition at line 168 of file linear_svm_function.hpp.

◆ Evaluate() [1/2]

double 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]

double 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]

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.

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]

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.

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.

◆ FitIntercept()

bool FitIntercept ( ) const
inline

Gets the intercept flag.

Definition at line 176 of file linear_svm_function.hpp.

References LinearSVMFunction< MatType >::NumFunctions().

◆ GetGroundTruthMatrix()

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

Constructs the ground truth label matrix with the passed labels.

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

◆ Gradient() [1/2]

void 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]

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.

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()

static void 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.

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.

◆ InitialPoint() [1/2]

const arma::mat& InitialPoint ( ) const
inline

Return the initial point for the optimization.

Definition at line 161 of file linear_svm_function.hpp.

◆ InitialPoint() [2/2]

arma::mat& InitialPoint ( )
inline

Modify the initial point for the optimization.

Definition at line 163 of file linear_svm_function.hpp.

◆ Lambda() [1/2]

double& Lambda ( )
inline

Sets the regularization parameter.

Definition at line 171 of file linear_svm_function.hpp.

◆ Lambda() [2/2]

double Lambda ( ) const
inline

Gets the regularization parameter.

Definition at line 173 of file linear_svm_function.hpp.

◆ NumFunctions()

size_t NumFunctions ( ) const

Return the number of functions.

Referenced by LinearSVMFunction< MatType >::FitIntercept().

◆ Shuffle()

void Shuffle ( )

Shuffle the dataset.


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