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::openmp::csvm Class Reference

A C-SVM implementation using OpenMP as backend. More...

#include <csvm.hpp>

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

Public Member Functions

 csvm (parameter params={})
 Construct a new C-SVM using the OpenMP backend with the parameters given through params. More...
 
 csvm (target_platform target, parameter params={})
 Construct a new C-SVM using the OpenMP backend on the target platform with the parameters given through params. More...
 
template<typename... Args, PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v< Args... >) >
 csvm (Args &&...named_args)
 Construct a new C-SVM using the OpenMP backend and the optionally provided named_args. More...
 
template<typename... Args, PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v< Args... >) >
 csvm (const target_platform target, Args &&...named_args)
 Construct a new C-SVM using the OpenMP backend on the target platform and the optionally provided named_args. More...
 
 csvm (const csvm &)=delete
 Delete copy-constructor since a CSVM is a move-only type. More...
 
 csvm (csvm &&) noexcept=default
 Default move-constructor since a virtual destructor has been declared. noexcept More...
 
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. noexcept More...
 
 ~csvm () override=default
 Default destructor since the copy and move constructors and copy- and move-assignment operators are defined.
 
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

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 override
 Solves the equation \(Ax = b\) using the Conjugated Gradients algorithm. More...
 
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 override
 Solves the equation \(Ax = b\) using the Conjugated Gradients algorithm. More...
 
template<typename real_type >
std::pair< std::vector< real_type >, real_type > solve_system_of_linear_equations_impl (const detail::parameter< real_type > &params, const std::vector< std::vector< real_type >> &A, std::vector< real_type > b, real_type eps, unsigned long long max_iter) const
 Solves the equation \(Ax = b\) using the Conjugated Gradients algorithm. More...
 
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 override
 Uses the already learned model to predict the class of multiple (new) data points. More...
 
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 override
 Uses the already learned model to predict the class of multiple (new) data points. More...
 
template<typename real_type >
std::vector< real_type > predict_values_impl (const detail::parameter< real_type > &params, const std::vector< std::vector< real_type >> &support_vectors, const std::vector< real_type > &alpha, real_type rho, std::vector< real_type > &w, const std::vector< std::vector< real_type >> &predict_points) const
 Uses the already learned model to predict the class of multiple (new) data points. More...
 
template<typename real_type >
std::vector< real_type > generate_q (const detail::parameter< real_type > &params, const std::vector< std::vector< real_type >> &data) const
 Calculate the q vector used in the dimensional reduction. More...
 
template<typename real_type >
std::vector< real_type > calculate_w (const std::vector< std::vector< real_type >> &support_vectors, const std::vector< real_type > &alpha) const
 Precalculate the w vector to speedup up the prediction using the linear kernel function. More...
 
template<typename real_type >
void run_device_kernel (const detail::parameter< real_type > &params, const std::vector< real_type > &q, std::vector< real_type > &ret, const std::vector< real_type > &d, const std::vector< std::vector< real_type >> &data, real_type QA_cost, real_type add) const
 Select the correct kernel based on the value of plssvm::parameter::kernel_type and run it on the CPU using OpenMP. More...
 

Protected Attributes

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

Private Member Functions

void init (target_platform target)
 Initializes the OpenMP backend and performs some sanity checks. More...
 
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

A C-SVM implementation using OpenMP as backend.

Constructor & Destructor Documentation

◆ csvm() [1/6]

plssvm::openmp::csvm::csvm ( parameter  params = {})
explicit

Construct a new C-SVM using the OpenMP backend with the parameters given through params.

Parameters
[in]paramsstruct encapsulating all possible SVM parameters
Exceptions
plssvm::exceptionall exceptions thrown in the base class constructor
plssvm::openmp::backend_exceptionif the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu
plssvm::openmp::backend_exceptionif the plssvm::target_platform::cpu target isn't available

◆ csvm() [2/6]

plssvm::openmp::csvm::csvm ( target_platform  target,
parameter  params = {} 
)
explicit

Construct a new C-SVM using the OpenMP backend on the target platform with the parameters given through params.

Parameters
[in]targetthe target platform used for this C-SVM
[in]paramsstruct encapsulating all possible SVM parameters
Exceptions
plssvm::exceptionall exceptions thrown in the base class constructor
plssvm::openmp::backend_exceptionif the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu
plssvm::openmp::backend_exceptionif the plssvm::target_platform::cpu target isn't available

◆ csvm() [3/6]

template<typename... Args, PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v< Args... >) >
plssvm::openmp::csvm::csvm ( Args &&...  named_args)
inlineexplicit

Construct a new C-SVM using the OpenMP backend and the optionally provided named_args.

Parameters
[in]named_argsthe additional optional named-parameters
Exceptions
plssvm::exceptionall exceptions thrown in the base class constructor
plssvm::openmp::backend_exceptionif the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu
plssvm::openmp::backend_exceptionif the plssvm::target_platform::cpu target isn't available

◆ csvm() [4/6]

template<typename... Args, PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v< Args... >) >
plssvm::openmp::csvm::csvm ( const target_platform  target,
Args &&...  named_args 
)
inlineexplicit

Construct a new C-SVM using the OpenMP backend on the target platform and the optionally provided named_args.

Parameters
[in]targetthe target platform used for this C-SVM
[in]named_argsthe additional optional named-parameters
Exceptions
plssvm::exceptionall exceptions thrown in the base class constructor
plssvm::openmp::backend_exceptionif the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu
plssvm::openmp::backend_exceptionif the plssvm::target_platform::cpu target isn't available

◆ csvm() [5/6]

plssvm::openmp::csvm::csvm ( const csvm )
delete

Delete copy-constructor since a CSVM is a move-only type.

◆ csvm() [6/6]

plssvm::openmp::csvm::csvm ( csvm &&  )
defaultnoexcept

Default move-constructor since a virtual destructor has been declared. noexcept

noexcept

Member Function Documentation

◆ operator=() [1/2]

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

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

Returns
*this

◆ operator=() [2/2]

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

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

Returns
*this noexcept

◆ solve_system_of_linear_equations() [1/2]

std::pair<std::vector<float>, float> plssvm::openmp::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
inlineoverrideprotectedvirtual

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

Implements plssvm::csvm.

◆ solve_system_of_linear_equations() [2/2]

std::pair<std::vector<double>, double> plssvm::openmp::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
inlineoverrideprotectedvirtual

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

Implements plssvm::csvm.

◆ solve_system_of_linear_equations_impl()

template<typename real_type >
std::pair<std::vector<real_type>, real_type> plssvm::openmp::csvm::solve_system_of_linear_equations_impl ( const detail::parameter< real_type > &  params,
const std::vector< std::vector< real_type >> &  A,
std::vector< real_type >  b,
real_type  eps,
unsigned long long  max_iter 
) const
protected

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

◆ predict_values() [1/2]

std::vector<float> plssvm::openmp::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
inlineoverrideprotectedvirtual

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

Implements plssvm::csvm.

◆ predict_values() [2/2]

std::vector<double> plssvm::openmp::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
inlineoverrideprotectedvirtual

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

Implements plssvm::csvm.

◆ predict_values_impl()

template<typename real_type >
std::vector<real_type> plssvm::openmp::csvm::predict_values_impl ( const detail::parameter< real_type > &  params,
const std::vector< std::vector< real_type >> &  support_vectors,
const std::vector< real_type > &  alpha,
real_type  rho,
std::vector< real_type > &  w,
const std::vector< std::vector< real_type >> &  predict_points 
) const
protected

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

◆ generate_q()

template<typename real_type >
std::vector<real_type> plssvm::openmp::csvm::generate_q ( const detail::parameter< real_type > &  params,
const std::vector< std::vector< real_type >> &  data 
) const
protected

Calculate the q vector used in the dimensional reduction.

The template parameter real_type represents the type of the data points (either float or double).

Parameters
[in]paramsthe SVM parameter used to calculate q (e.g., kernel_type)
[in]datathe data points used in the dimensional reduction.
Returns
the q vector ([[nodiscard]])

◆ calculate_w()

template<typename real_type >
std::vector<real_type> plssvm::openmp::csvm::calculate_w ( const std::vector< std::vector< real_type >> &  support_vectors,
const std::vector< real_type > &  alpha 
) const
protected

Precalculate the w vector to speedup up the prediction using the linear kernel function.

The template parameter real_type represents the type of the data points (either float or double).

Parameters
[in]support_vectorsthe previously learned support vectors
[in]alphathe previously learned weights
Returns
the w vector ([[nodiscard]])

◆ run_device_kernel()

template<typename real_type >
void plssvm::openmp::csvm::run_device_kernel ( const detail::parameter< real_type > &  params,
const std::vector< real_type > &  q,
std::vector< real_type > &  ret,
const std::vector< real_type > &  d,
const std::vector< std::vector< real_type >> &  data,
real_type  QA_cost,
real_type  add 
) const
protected

Select the correct kernel based on the value of plssvm::parameter::kernel_type and run it on the CPU using OpenMP.

The template parameter real_type represents the type of the data points (either float or double).

Parameters
[in]paramsthe SVM parameter used to calculate q (e.g., kernel_type)
[in]qthe q vector used in the dimensional reduction
[out]retthe result vector
[in]dthe right-hand side of the equation
[in]datathe data points
[in]QA_costa value used in the dimensional reduction
[in]adddenotes whether the values are added or subtracted from the result vector

◆ init()

void plssvm::openmp::csvm::init ( target_platform  target)
private

Initializes the OpenMP backend and performs some sanity checks.

Parameters
[in]targetthe target platform to use
Exceptions
plssvm::openmp::backend_exceptionif the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu
plssvm::openmp::backend_exceptionif the plssvm::target_platform::cpu target isn't available

◆ get_target_platform()

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

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
inlinenoexceptinherited

Return the currently used SVM parameter.

Returns
the SVM parameter ([[nodiscard]])

◆ set_params() [1/2]

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

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

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
inherited

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
inherited

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
inherited

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
inherited

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

◆ sanity_check_parameter()

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

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: