ThunderSVM
ThunderSVM: An Open-Source SVM Library on GPUs and CPUs
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SvmModel Class Referenceabstract

Abstract class for different SVM models. More...

#include <svmmodel.h>

Inheritance diagram for SvmModel:
OneClassSVC SVC SVR NuSVC NuSVR

Public Member Functions

virtual void train (const DataSet &dataset, SvmParam param)=0
 
virtual vector< float_type > predict (const DataSet::node2d &instances, int batch_size)
 
void predict_dec_values (const DataSet::node2d &instances, SyncArray< float_type > &dec_values, int batch_size) const
 
virtual vector< float_type > cross_validation (DataSet dataset, SvmParam param, int n_fold)
 
virtual void save_to_file (string path)
 
virtual void load_from_file (string path)
 

Protected Member Functions

virtual void model_setup (const DataSet &dataset, SvmParam &param)
 

Protected Attributes

SvmParam param
 
SyncArray< float_type > coef
 
DataSet::node2d sv
 
SyncArray< int > n_sv
 the number of support vectors for each class
 
int n_total_sv
 the number of support vectors for all classes
 
SyncArray< float_type > rho
 the bias term for each binary model
 
int n_classes = 2
 the number of classes
 
size_t n_binary_models
 the number of binary models, equal to \(k(k-1)/2\), where \(k\) is the number of classes
 
vector< float_type > probA
 be used to predict probability for each binary model
 
vector< float_type > probB
 be used to predict probability for each binary model
 
vector< int > label
 only for SVC, maps logical label (0,1,2,...) to real label in dataset (maybe 2,4,5,...)
 

Detailed Description

Abstract class for different SVM models.

Member Function Documentation

◆ cross_validation()

vector< float_type > SvmModel::cross_validation ( DataSet  dataset,
SvmParam  param,
int  n_fold 
)
virtual

performing cross-validation. In \(k\)-fold cross_validation, dataset is spilt into \(k\) equal size parts. Then each part is used as testing set, while the remaining \(k-1\) parts are used as training set. The whole dataset will be predicted after \(k\) training and testing.

Parameters
datasettraining dataset
paramparam for cross-validation
n_foldthe number of fold in cross-validation
Returns
the same structure as predict()

◆ load_from_file()

void SvmModel::load_from_file ( string  path)
virtual

load SvmModel from a file, the file format is the same as LIBSVM. before call this function, one must read the first line of the model file, which contains svm_type. Then construct right SvmModel (SVC, SVR, ...), since SvmModel is an abstract class.

Parameters
pathpath and filename of saved model file

◆ model_setup()

void SvmModel::model_setup ( const DataSet dataset,
SvmParam param 
)
protectedvirtual

called at the begining of train(), do initialization

Parameters
dataset
param

Reimplemented in SVC.

◆ predict()

vector< float_type > SvmModel::predict ( const DataSet::node2d &  instances,
int  batch_size 
)
virtual

predict label given instances.

Parameters
instancesinstances used
batch_sizethe number of instances to predict parallel, higher value needs more memory
Returns
label (SVC, NuSVC), real number (SVR, NuSVR), {-1,+1} (OneClassSVC)

Reimplemented in SVC, and OneClassSVC.

◆ predict_dec_values()

void SvmModel::predict_dec_values ( const DataSet::node2d &  instances,
SyncArray< float_type > &  dec_values,
int  batch_size 
) const

predict decision values.

Parameters
[in]instancesinstances used
[out]dec_valuesdecision values predicted, #instances \(times\) n_binary_models array
[in]batch_sizethe number of instances to predict parallel, higher value needs more memory

◆ save_to_file()

void SvmModel::save_to_file ( string  path)
virtual

save SvmModel to a file, the file format is the same as LIBSVM

Parameters
pathpath and filename of the model file to save

◆ train()

virtual void SvmModel::train ( const DataSet dataset,
SvmParam  param 
)
pure virtual

train model given dataset and param.

Parameters
datasettraining dataset
paramparam for training

Implemented in SVC, SVR, NuSVR, and OneClassSVC.

Member Data Documentation

◆ coef

SyncArray<float_type> SvmModel::coef
protected

coefficients for each support vector, the structure is the same as LIBSVM. The coefficient is equal to \(\alpha_iy_i\) in SVM dual optimization problem. For one-vs-one multi-class decomposition, \(k\) is the number of classes, and \(N_{sv}\) is the number of support vectors. The size of coef is \( (k-1)\times N_{sv}\). Each support vector has at most \(k-1\) coefficients. For each binary classifier \(SVM_{i,j}\), the coefficients locate in: (1) class i, coef[j-1][sv_start[i]...]; (2) class j, coef[i][sv_start[j]...], where sv_start[i] is the start position of support vectors of class i.

◆ sv

DataSet::node2d SvmModel::sv
protected

support vectors of this model. The support vectors is grouped in classes 0,1,2,.... The sequence of them in each group is as they appear in original dataset. A training instance is saved as a support vector IFF it is a support vector in at least one binary model.


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