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... | |
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:
An example on how to use the interface is shown below:
MatType | Type of data matrix. |
Definition at line 80 of file linear_svm.hpp.
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.
OptimizerType | Desired differentiable separable optimizer |
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. |
delta | Margin of difference between correct class and other classes. |
fitIntercept | add intercept term or not. |
optimizer | Desired optimizer. |
callbacks | Callback functions. See https://www.ensmallen.org/docs.html#callback-documentation. |
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.
OptimizerType | Desired differentiable separable optimizer |
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. |
delta | Margin of difference between correct class and other classes. |
fitIntercept | add intercept term or not. |
optimizer | Desired optimizer. |
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.
inputSize | Size of the input feature vector. |
numClasses | Number of classes for classification. |
lambda | L2-regularization constant. |
delta | Margin of difference between correct class and other classes. |
fitIntercept | add intercept term or not. |
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.
numClasses | Number of classes for classification. |
lambda | L2-regularization constant. |
delta | Margin of difference between correct class and other classes. |
fitIntercept | add intercept term or not. |
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.
data | Set of points to classify. |
labels | Predicted labels for each point. |
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.
data | Matrix of data points to be classified. |
labels | Predicted labels for each point. |
scores | Class probabilities for each point. |
void Classify | ( | const MatType & | data, |
arma::mat & | scores | ||
) | const |
Classify the given points, returning class scores for each point.
data | Matrix of data points to be classified. |
scores | Class scores for each point. |
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.
point | Point to be classified. |
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.
testData | Matrix of data points using which predictions are made. |
testLabels | Vector of labels associated with the data. |
|
inline |
Sets the margin between the correct class and all other classes.
Definition at line 273 of file linear_svm.hpp.
|
inline |
Gets the margin between the correct class and all other classes.
Definition at line 275 of file linear_svm.hpp.
|
inline |
Gets the features size of the training data.
Definition at line 286 of file linear_svm.hpp.
|
inline |
Sets the intercept term flag.
Definition at line 278 of file linear_svm.hpp.
|
inline |
Sets the regularization parameter.
Definition at line 268 of file linear_svm.hpp.
|
inline |
Gets the regularization parameter.
Definition at line 270 of file linear_svm.hpp.
|
inline |
Sets the number of classes.
Definition at line 263 of file linear_svm.hpp.
|
inline |
Gets the number of classes.
Definition at line 265 of file linear_svm.hpp.
|
inline |
Set the model parameters.
Definition at line 281 of file linear_svm.hpp.
|
inline |
Get the model parameters.
Definition at line 283 of file linear_svm.hpp.
|
inline |
Serialize the LinearSVM model.
Definition at line 294 of file linear_svm.hpp.
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.
OptimizerType | Desired optimizer. |
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. |
optimizer | Desired optimizer. |
callbacks | Callback Functions. See https://www.ensmallen.org/docs.html#callback-documentation. |
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.
OptimizerType | Desired optimizer. |
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. |