PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
plssvm::csvm Class Referenceabstract

Base class for all C-SVM backends. More...

#include <csvm.hpp>

Inheritance diagram for plssvm::csvm:
[legend]
Collaboration diagram for plssvm::csvm:
[legend]

Public Member Functions

 csvm (parameter params={})
 Construct a C-SVM using the SVM parameter params. More...
 
template<typename... Args>
 csvm (Args &&...args)
 Construct a C-SVM forwarding all parameters args to the plssvm::parameter constructor. More...
 
 csvm (const csvm &)=delete
 Delete copy-constructor since a CSVM is a move-only type.
 
 csvm (csvm &&) noexcept=default
 Default move-constructor since a virtual destructor has been declared.
 
csvmoperator= (const csvm &)=delete
 Delete copy-assignment operator since a CSVM is a move-only type. More...
 
csvmoperator= (csvm &&) noexcept=default
 Default move-assignment operator since a virtual destructor has been declared. More...
 
virtual ~csvm ()=default
 Virtual destructor to enable safe inheritance.
 
target_platform get_target_platform () const noexcept
 Return the target platform (i.e, CPU or GPU including the vendor) this SVM runs on. More...
 
parameter get_params () const noexcept
 Return the currently used SVM parameter. More...
 
void set_params (parameter params) noexcept
 Override the old SVM parameter with the new plssvm::parameter params. More...
 
template<typename... Args, PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v< Args... >) >
void set_params (Args &&...named_args)
 Override the old SVM parameter with the new ones given as named parameters in named_args. More...
 
template<typename real_type , typename label_type , typename... Args>
model< real_type, label_type > fit (const data_set< real_type, label_type > &data, Args &&...named_args) const
 Fit a model using the current SVM on the data. More...
 
template<typename real_type , typename label_type >
std::vector< label_type > predict (const model< real_type, label_type > &model, const data_set< real_type, label_type > &data) const
 Predict the labels for the data set using the model. More...
 
template<typename real_type , typename label_type >
real_type score (const model< real_type, label_type > &model) const
 Calculate the accuracy of the model. More...
 
template<typename real_type , typename label_type >
real_type score (const model< real_type, label_type > &model, const data_set< real_type, label_type > &data) const
 Calculate the accuracy of the labeled data set using the model. More...
 

Protected Member Functions

virtual std::pair< std::vector< float >, float > solve_system_of_linear_equations (const detail::parameter< float > &params, const std::vector< std::vector< float >> &A, std::vector< float > b, float eps, unsigned long long max_iter) const =0
 Solves the equation \(Ax = b\) using the Conjugated Gradients algorithm. More...
 
virtual std::pair< std::vector< double >, double > solve_system_of_linear_equations (const detail::parameter< double > &params, const std::vector< std::vector< double >> &A, std::vector< double > b, double eps, unsigned long long max_iter) const =0
 Solves the equation \(Ax = b\) using the Conjugated Gradients algorithm. More...
 
virtual std::vector< float > predict_values (const detail::parameter< float > &params, const std::vector< std::vector< float >> &support_vectors, const std::vector< float > &alpha, float rho, std::vector< float > &w, const std::vector< std::vector< float >> &predict_points) const =0
 Uses the already learned model to predict the class of multiple (new) data points. More...
 
virtual std::vector< double > predict_values (const detail::parameter< double > &params, const std::vector< std::vector< double >> &support_vectors, const std::vector< double > &alpha, double rho, std::vector< double > &w, const std::vector< std::vector< double >> &predict_points) const =0
 Uses the already learned model to predict the class of multiple (new) data points. More...
 

Protected Attributes

target_platform target_ { plssvm::target_platform::automatic }
 The target platform of this SVM.
 

Private Member Functions

void sanity_check_parameter () const
 Perform some sanity checks on the passed SVM parameters. More...
 

Private Attributes

parameter params_ {}
 The SVM parameter (e.g., cost, degree, gamma, coef0) currently in use.
 

Detailed Description

Base class for all C-SVM backends.

This class implements all features shared between all C-SVM backends. It defines the whole public API of a C-SVM.

Constructor & Destructor Documentation

◆ csvm() [1/2]

plssvm::csvm::csvm ( parameter  params = {})
inlineexplicit

Construct a C-SVM using the SVM parameter params.

Uses the default SVM parameter if none are provided.

Parameters
[in]paramsthe SVM parameter

◆ csvm() [2/2]

template<typename... Args>
plssvm::csvm::csvm ( Args &&...  args)
explicit

Construct a C-SVM forwarding all parameters args to the plssvm::parameter constructor.

Template Parameters
Argsthe type of the (named-)parameters
Parameters
[in]argsthe parameters used to construct a plssvm::parameter

Member Function Documentation

◆ operator=() [1/2]

csvm& plssvm::csvm::operator= ( const csvm )
delete

Delete copy-assignment operator since a CSVM is a move-only type.

Returns
*this

◆ operator=() [2/2]

csvm& plssvm::csvm::operator= ( csvm &&  )
defaultnoexcept

Default move-assignment operator since a virtual destructor has been declared.

Returns
*this

◆ get_target_platform()

target_platform plssvm::csvm::get_target_platform ( ) const
inlinenoexcept

Return the target platform (i.e, CPU or GPU including the vendor) this SVM runs on.

Returns
the target platform ([[nodiscard]])

◆ get_params()

parameter plssvm::csvm::get_params ( ) const
inlinenoexcept

Return the currently used SVM parameter.

Returns
the SVM parameter ([[nodiscard]])

◆ set_params() [1/2]

void plssvm::csvm::set_params ( parameter  params)
inlinenoexcept

Override the old SVM parameter with the new plssvm::parameter params.

Parameters
[in]paramsthe new SVM parameter to use

◆ set_params() [2/2]

template<typename... Args, std::enable_if_t< detail::has_only_parameter_named_args_v< Args... >, bool > >
void plssvm::csvm::set_params ( Args &&...  named_args)

Override the old SVM parameter with the new ones given as named parameters in named_args.

Template Parameters
Argsthe type of the named-parameters
Parameters
[in]named_argsthe potential named-parameters

◆ fit()

template<typename real_type , typename label_type , typename... Args>
model< real_type, label_type > plssvm::csvm::fit ( const data_set< real_type, label_type > &  data,
Args &&...  named_args 
) const

Fit a model using the current SVM on the data.

Template Parameters
real_typethe type of the data (float or double)
label_typethe type of the label (an arithmetic type or std::string)
Argsthe type of the potential additional parameters
Parameters
[in]datathe data used to train the SVM model
[in]named_argsthe potential additional parameters (epsilon and/or max_iter)
Exceptions
plssvm::invalid_parameter_exceptionif the provided value for epsilon is greater or equal than zero
plssvm::invlaid_parameter_exceptionif the provided maximum number of iterations is less or equal than zero
plssvm::invalid_parameter_exceptionif the training data does not include labels
plssvm::exceptionany exception thrown in the respective backend's implementation of plssvm::csvm::solve_system_of_linear_equations
Returns
the learned model ([[nodiscard]])

◆ predict()

template<typename real_type , typename label_type >
std::vector< label_type > plssvm::csvm::predict ( const model< real_type, label_type > &  model,
const data_set< real_type, label_type > &  data 
) const

Predict the labels for the data set using the model.

Template Parameters
real_typethe type of the data (float or double)
label_typethe type of the label (an arithmetic type or std::string)
Parameters
[in]modela previously learned model
[in]datathe data to predict the labels for
Exceptions
plssvm::invalid_parameter_exceptionif the number of features in the model's support vectors don't match the number of features in the data set
plssvm::exceptionany exception thrown in the respective backend's implementation of plssvm::csvm::predict_values
Returns
the predicted labels ([[nodiscard]])
Examples
csvm_examples.cpp.

◆ score() [1/2]

template<typename real_type , typename label_type >
real_type plssvm::csvm::score ( const model< real_type, label_type > &  model) const

Calculate the accuracy of the model.

Template Parameters
real_typethe type of the data (float or double)
label_typethe type of the label (an arithmetic type or std::string)
Parameters
[in]modela previously learned model
Exceptions
plssvm::exceptionany exception thrown in the respective backend's implementation of plssvm::csvm::predict_values
Returns
the accuracy of the model ([[nodiscard]])

◆ score() [2/2]

template<typename real_type , typename label_type >
real_type plssvm::csvm::score ( const model< real_type, label_type > &  model,
const data_set< real_type, label_type > &  data 
) const

Calculate the accuracy of the labeled data set using the model.

Template Parameters
real_typethe type of the data (float or double)
label_typethe type of the label (an arithmetic type or std::string)
Parameters
[in]modela previously learned model
[in]datathe labeled data set to score
Exceptions
plssvm::invalid_parameter_exceptionif the data to score has no labels
plssvm::invalid_parameter_exceptionif the number of features in the model's support vectors don't match the number of features in the data set
plssvm::exceptionany exception thrown in the respective backend's implementation of plssvm::csvm::predict_values
Returns
the accuracy of the labeled data ([[nodiscard]])

◆ solve_system_of_linear_equations() [1/2]

virtual std::pair<std::vector<float>, float> plssvm::csvm::solve_system_of_linear_equations ( const detail::parameter< float > &  params,
const std::vector< std::vector< float >> &  A,
std::vector< float >  b,
float  eps,
unsigned long long  max_iter 
) const
protectedpure virtual

Solves the equation \(Ax = b\) using the Conjugated Gradients algorithm.

Uses a slightly modified version of the CG algorithm described by Jonathan Richard Shewchuk:

Parameters
[in]paramsthe SVM parameters used in the respective kernel functions
[in]Athe matrix of the equation \(Ax = b\) (symmetric positive definite)
[in]bthe right-hand side of the equation \(Ax = b\)
[in]epsthe error tolerance
[in]max_iterthe maximum number of CG iterations
Exceptions
plssvm::exceptionany exception thrown by the backend's implementation
Returns
a pair of [the result vector x, the resulting bias] ([[nodiscard]])

Implemented in plssvm::detail::gpu_csvm< device_ptr_t, queue_t >, plssvm::detail::gpu_csvm< detail::device_ptr, int >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::command_queue >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::queue >, and plssvm::openmp::csvm.

◆ solve_system_of_linear_equations() [2/2]

virtual std::pair<std::vector<double>, double> plssvm::csvm::solve_system_of_linear_equations ( const detail::parameter< double > &  params,
const std::vector< std::vector< double >> &  A,
std::vector< double >  b,
double  eps,
unsigned long long  max_iter 
) const
protectedpure virtual

Solves the equation \(Ax = b\) using the Conjugated Gradients algorithm.

Uses a slightly modified version of the CG algorithm described by Jonathan Richard Shewchuk:

Parameters
[in]paramsthe SVM parameters used in the respective kernel functions
[in]Athe matrix of the equation \(Ax = b\) (symmetric positive definite)
[in]bthe right-hand side of the equation \(Ax = b\)
[in]epsthe error tolerance
[in]max_iterthe maximum number of CG iterations
Exceptions
plssvm::exceptionany exception thrown by the backend's implementation
Returns
a pair of [the result vector x, the resulting bias] ([[nodiscard]])

Implemented in plssvm::detail::gpu_csvm< device_ptr_t, queue_t >, plssvm::detail::gpu_csvm< detail::device_ptr, int >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::command_queue >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::queue >, and plssvm::openmp::csvm.

◆ predict_values() [1/2]

virtual std::vector<float> plssvm::csvm::predict_values ( const detail::parameter< float > &  params,
const std::vector< std::vector< float >> &  support_vectors,
const std::vector< float > &  alpha,
float  rho,
std::vector< float > &  w,
const std::vector< std::vector< float >> &  predict_points 
) const
protectedpure virtual

Uses the already learned model to predict the class of multiple (new) data points.

Parameters
[in]paramsthe SVM parameters used in the respective kernel functions
[in]support_vectorsthe previously learned support vectors
[in]alphathe alpha values (weights) associated with the support vectors
[in]rhothe rho value determined after training the model
[in,out]wthe normal vector to speedup prediction in case of the linear kernel function, an empty vector in case of the polynomial or rbf kernel
[in]predict_pointsthe points to predict
Exceptions
plssvm::exceptionany exception thrown by the backend's implementation
Returns
a vector filled with the predictions (not the actual labels!) ([[nodiscard]])

Implemented in plssvm::detail::gpu_csvm< device_ptr_t, queue_t >, plssvm::detail::gpu_csvm< detail::device_ptr, int >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::command_queue >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::queue >, and plssvm::openmp::csvm.

◆ predict_values() [2/2]

virtual std::vector<double> plssvm::csvm::predict_values ( const detail::parameter< double > &  params,
const std::vector< std::vector< double >> &  support_vectors,
const std::vector< double > &  alpha,
double  rho,
std::vector< double > &  w,
const std::vector< std::vector< double >> &  predict_points 
) const
protectedpure virtual

Uses the already learned model to predict the class of multiple (new) data points.

Parameters
[in]paramsthe SVM parameters used in the respective kernel functions
[in]support_vectorsthe previously learned support vectors
[in]alphathe alpha values (weights) associated with the support vectors
[in]rhothe rho value determined after training the model
[in,out]wthe normal vector to speedup prediction in case of the linear kernel function, an empty vector in case of the polynomial or rbf kernel
[in]predict_pointsthe points to predict
Exceptions
plssvm::exceptionany exception thrown by the backend's implementation
Returns
a vector filled with the predictions (not the actual labels!) ([[nodiscard]])

Implemented in plssvm::detail::gpu_csvm< device_ptr_t, queue_t >, plssvm::detail::gpu_csvm< detail::device_ptr, int >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::command_queue >, plssvm::detail::gpu_csvm< detail::device_ptr, detail::queue >, and plssvm::openmp::csvm.

◆ sanity_check_parameter()

void plssvm::csvm::sanity_check_parameter ( ) const
inlineprivate

Perform some sanity checks on the passed SVM parameters.

Exceptions
plssvm::invalid_parameter_exceptionif the kernel function is invalid
plssvm::invalid_parameter_exceptionif the gamma value for the polynomial or radial basis function kernel is not greater than zero

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