LinearSVM< MatType > Class Template Reference

The LinearSVM class implements an L2-regularized support vector machine model, and supports training with multiple optimizers and classification. More...

Public Member Functions

template<typename OptimizerType , typename... CallbackTypes>
 LinearSVM (const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses, const double lambda, const double delta, const bool fitIntercept, OptimizerType optimizer, CallbackTypes &&... callbacks)
 Construct the LinearSVM class with the provided data and labels. More...

 
template
<
typename
OptimizerType
=
ens::L_BFGS
>
 LinearSVM (const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses=2, const double lambda=0.0001, const double delta=1.0, const bool fitIntercept=false, OptimizerType optimizer=OptimizerType())
 Construct the LinearSVM class with the provided data and labels. More...

 
 LinearSVM (const size_t inputSize, const size_t numClasses=0, const double lambda=0.0001, const double delta=1.0, const bool fitIntercept=false)
 Initialize the Linear SVM without performing training. More...

 
 LinearSVM (const size_t numClasses=0, const double lambda=0.0001, const double delta=1.0, const bool fitIntercept=false)
 Initialize the Linear SVM without performing training. More...

 
void Classify (const MatType &data, arma::Row< size_t > &labels) const
 Classify the given points, returning the predicted labels for each point. More...

 
void Classify (const MatType &data, arma::Row< size_t > &labels, arma::mat &scores) const
 Classify the given points, returning class scores and predicted class label for each point. More...

 
void Classify (const MatType &data, arma::mat &scores) const
 Classify the given points, returning class scores for each point. More...

 
template
<
typename
VecType
>
size_t Classify (const VecType &point) const
 Classify the given point. More...

 
double ComputeAccuracy (const MatType &testData, const arma::Row< size_t > &testLabels) const
 Computes accuracy of the learned model given the feature data and the labels associated with each data point. More...

 
double & Delta ()
 Sets the margin between the correct class and all other classes. More...

 
double Delta () const
 Gets the margin between the correct class and all other classes. More...

 
size_t FeatureSize () const
 Gets the features size of the training data. More...

 
bool & FitIntercept ()
 Sets the intercept term flag. 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 ()
 Set 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 LinearSVM model. More...

 
template<typename OptimizerType , typename... CallbackTypes>
double Train (const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses, OptimizerType optimizer, CallbackTypes &&... callbacks)
 Train the Linear SVM with the given training data. More...

 
template
<
typename
OptimizerType
=
ens::L_BFGS
>
double Train (const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses=2, OptimizerType optimizer=OptimizerType())
 Train the Linear SVM with the given training data. More...

 

Detailed Description


template
<
typename
MatType
=
arma::mat
>

class mlpack::svm::LinearSVM< MatType >

The LinearSVM class implements an L2-regularized support vector machine model, and supports training with multiple optimizers and classification.

The class supports different observation types via the MatType template parameter; for instance, support vector classification can be performed on sparse datasets by specifying arma::sp_mat as the MatType parameter.

Linear SVM can be used for general classification tasks which will work on multiclass classification. More technical details about the model can be found from the following:

@inproceedings{weston1999support,
title = {Support vector machines for multi-class pattern
recognition.},
author = {Weston, Jason and Watkins, Chris},
booktitle = {Proceedings of the 7th European Symposium on Artifical Neural
Networks (ESANN '99)},
volume = {99},
pages = {219--224},
year = {1999}
}
@article{cortes1995support,
title = {Support-vector networks},
author = {Cortes, Corinna and Vapnik, Vladimir},
journal = {Machine Learning},
volume = {20},
number = {3},
pages = {273--297},
year = {1995},
publisher = {Springer}
}

An example on how to use the interface is shown below:

arma::mat train_data; // Training data matrix.
arma::Row<size_t> labels; // Labels associated with the data.
const size_t inputSize = 1000; // Size of input feature vector.
const size_t numClasses = 5; // Number of classes.
// Train the model using default options.
LinearSVM<> lsvm(train_data, labels, inputSize, numClasses, lambda,
delta, L_BFGS());
arma::mat test_data;
arma::Row<size_t> predictions;
lsvm.Classify(test_data, predictions);
Template Parameters
MatTypeType of data matrix.

Definition at line 80 of file linear_svm.hpp.

Constructor & Destructor Documentation

◆ LinearSVM() [1/4]

LinearSVM ( const MatType &  data,
const arma::Row< size_t > &  labels,
const size_t  numClasses,
const double  lambda,
const double  delta,
const bool  fitIntercept,
OptimizerType  optimizer,
CallbackTypes &&...  callbacks 
)

Construct the LinearSVM 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.

Template Parameters
OptimizerTypeDesired differentiable separable optimizer
CallbackTypesTypes of callback functions.
Parameters
dataInput training features. 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.
fitInterceptadd intercept term or not.
optimizerDesired optimizer.
callbacksCallback functions. See https://www.ensmallen.org/docs.html#callback-documentation.

◆ LinearSVM() [2/4]

LinearSVM ( const MatType &  data,
const arma::Row< size_t > &  labels,
const size_t  numClasses = 2,
const double  lambda = 0.0001,
const double  delta = 1.0,
const bool  fitIntercept = false,
OptimizerType  optimizer = OptimizerType() 
)

Construct the LinearSVM 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.

Template Parameters
OptimizerTypeDesired differentiable separable optimizer
Parameters
dataInput training features. 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.
fitInterceptadd intercept term or not.
optimizerDesired optimizer.

◆ LinearSVM() [3/4]

LinearSVM ( const size_t  inputSize,
const size_t  numClasses = 0,
const double  lambda = 0.0001,
const double  delta = 1.0,
const bool  fitIntercept = false 
)

Initialize the Linear SVM 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.

Parameters
inputSizeSize of the input feature vector.
numClassesNumber of classes for classification.
lambdaL2-regularization constant.
deltaMargin of difference between correct class and other classes.
fitInterceptadd intercept term or not.

◆ LinearSVM() [4/4]

LinearSVM ( const size_t  numClasses = 0,
const double  lambda = 0.0001,
const double  delta = 1.0,
const bool  fitIntercept = false 
)

Initialize the Linear SVM 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.

Parameters
numClassesNumber of classes for classification.
lambdaL2-regularization constant.
deltaMargin of difference between correct class and other classes.
fitInterceptadd intercept term or not.

Member Function Documentation

◆ Classify() [1/4]

void Classify ( const MatType &  data,
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.

Parameters
dataSet of points to classify.
labelsPredicted labels for each point.

◆ Classify() [2/4]

void Classify ( const MatType &  data,
arma::Row< size_t > &  labels,
arma::mat &  scores 
) const

Classify the given points, returning class scores and predicted class label for each point.

The function calculates the scores for every class, given a data point. It then chooses the class which has the highest probability among all.

Parameters
dataMatrix of data points to be classified.
labelsPredicted labels for each point.
scoresClass probabilities for each point.

◆ Classify() [3/4]

void Classify ( const MatType &  data,
arma::mat &  scores 
) const

Classify the given points, returning class scores for each point.

Parameters
dataMatrix of data points to be classified.
scoresClass scores for each point.

◆ Classify() [4/4]

size_t Classify ( const VecType &  point) const

Classify the given point.

The predicted class label is returned. The function calculates the scores for every class, given the point. It then chooses the class which has the highest probability among all.

Parameters
pointPoint to be classified.
Returns
Predicted class label of the point.

◆ ComputeAccuracy()

double ComputeAccuracy ( const MatType &  testData,
const arma::Row< size_t > &  testLabels 
) 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.
testLabelsVector of labels associated with the data.
Returns
Accuracy of the model.

◆ Delta() [1/2]

double& Delta ( )
inline

Sets the margin between the correct class and all other classes.

Definition at line 273 of file linear_svm.hpp.

◆ Delta() [2/2]

double Delta ( ) const
inline

Gets the margin between the correct class and all other classes.

Definition at line 275 of file linear_svm.hpp.

◆ FeatureSize()

size_t FeatureSize ( ) const
inline

Gets the features size of the training data.

Definition at line 286 of file linear_svm.hpp.

◆ FitIntercept()

bool& FitIntercept ( )
inline

Sets the intercept term flag.

Definition at line 278 of file linear_svm.hpp.

◆ Lambda() [1/2]

double& Lambda ( )
inline

Sets the regularization parameter.

Definition at line 268 of file linear_svm.hpp.

◆ Lambda() [2/2]

double Lambda ( ) const
inline

Gets the regularization parameter.

Definition at line 270 of file linear_svm.hpp.

◆ NumClasses() [1/2]

size_t& NumClasses ( )
inline

Sets the number of classes.

Definition at line 263 of file linear_svm.hpp.

◆ NumClasses() [2/2]

size_t NumClasses ( ) const
inline

Gets the number of classes.

Definition at line 265 of file linear_svm.hpp.

◆ Parameters() [1/2]

arma::mat& Parameters ( )
inline

Set the model parameters.

Definition at line 281 of file linear_svm.hpp.

◆ Parameters() [2/2]

const arma::mat& Parameters ( ) const
inline

Get the model parameters.

Definition at line 283 of file linear_svm.hpp.

◆ serialize()

void serialize ( Archive &  ar,
const uint32_t   
)
inline

Serialize the LinearSVM model.

Definition at line 294 of file linear_svm.hpp.

◆ Train() [1/2]

double Train ( const MatType &  data,
const arma::Row< size_t > &  labels,
const size_t  numClasses,
OptimizerType  optimizer,
CallbackTypes &&...  callbacks 
)

Train the Linear SVM with the given training data.

Template Parameters
OptimizerTypeDesired optimizer.
CallbackTypesTypes of Callback Functions.
Parameters
dataInput training features. Each column associate with one sample.
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
optimizerDesired optimizer.
callbacksCallback Functions. See https://www.ensmallen.org/docs.html#callback-documentation.
Returns
Objective value of the final point.

◆ Train() [2/2]

double Train ( const MatType &  data,
const arma::Row< size_t > &  labels,
const size_t  numClasses = 2,
OptimizerType  optimizer = OptimizerType() 
)

Train the Linear SVM with the given training data.

Template Parameters
OptimizerTypeDesired optimizer.
Parameters
dataInput training features. Each column associate with one sample.
labelsLabels associated with the feature data.
numClassesNumber of classes for classification.
optimizerDesired optimizer.
Returns
Objective value of the final point.

The documentation for this class was generated from the following file:
  • /home/jenkins-mlpack/mlpack.org/_src/mlpack-git/src/mlpack/methods/linear_svm/linear_svm.hpp