PLSSVM - Parallel Least Squares Support Vector Machine
2.0.0
A Least Squares Support Vector Machine implementation using different backends.
|
A C-SVM implementation using OpenMP as backend. More...
#include <csvm.hpp>
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... | |
csvm & | operator= (const csvm &)=delete |
Delete copy-assignment operator since a CSVM is a move-only type. More... | |
csvm & | operator= (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 > ¶ms, 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 > ¶ms, 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 > ¶ms, 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 > ¶ms, 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 > ¶ms, 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 > ¶ms, 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 > ¶ms, 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 > ¶ms, 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. | |
A C-SVM implementation using OpenMP as backend.
|
explicit |
Construct a new C-SVM using the OpenMP backend with the parameters given through params
.
[in] | params | struct encapsulating all possible SVM parameters |
plssvm::exception | all exceptions thrown in the base class constructor |
plssvm::openmp::backend_exception | if the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu |
plssvm::openmp::backend_exception | if the plssvm::target_platform::cpu target isn't available |
|
explicit |
Construct a new C-SVM using the OpenMP backend on the target
platform with the parameters given through params
.
[in] | target | the target platform used for this C-SVM |
[in] | params | struct encapsulating all possible SVM parameters |
plssvm::exception | all exceptions thrown in the base class constructor |
plssvm::openmp::backend_exception | if the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu |
plssvm::openmp::backend_exception | if the plssvm::target_platform::cpu target isn't available |
|
inlineexplicit |
Construct a new C-SVM using the OpenMP backend and the optionally provided named_args
.
[in] | named_args | the additional optional named-parameters |
plssvm::exception | all exceptions thrown in the base class constructor |
plssvm::openmp::backend_exception | if the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu |
plssvm::openmp::backend_exception | if the plssvm::target_platform::cpu target isn't available |
|
inlineexplicit |
Construct a new C-SVM using the OpenMP backend on the target
platform and the optionally provided named_args
.
[in] | target | the target platform used for this C-SVM |
[in] | named_args | the additional optional named-parameters |
plssvm::exception | all exceptions thrown in the base class constructor |
plssvm::openmp::backend_exception | if the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu |
plssvm::openmp::backend_exception | if the plssvm::target_platform::cpu target isn't available |
|
delete |
Delete copy-constructor since a CSVM is a move-only type.
|
defaultnoexcept |
Default move-constructor since a virtual destructor has been declared. noexcept
noexcept
Delete copy-assignment operator since a CSVM is a move-only type.
*this
Default move-assignment operator since a virtual destructor has been declared. noexcept
*this
noexcept
|
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:
[in] | params | the SVM parameters used in the respective kernel functions |
[in] | A | the matrix of the equation \(Ax = b\) (symmetric positive definite) |
[in] | b | the right-hand side of the equation \(Ax = b\) |
[in] | eps | the error tolerance |
[in] | max_iter | the maximum number of CG iterations |
plssvm::exception | any exception thrown by the backend's implementation |
[[nodiscard]]
) Implements plssvm::csvm.
|
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:
[in] | params | the SVM parameters used in the respective kernel functions |
[in] | A | the matrix of the equation \(Ax = b\) (symmetric positive definite) |
[in] | b | the right-hand side of the equation \(Ax = b\) |
[in] | eps | the error tolerance |
[in] | max_iter | the maximum number of CG iterations |
plssvm::exception | any exception thrown by the backend's implementation |
[[nodiscard]]
) Implements plssvm::csvm.
|
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:
[in] | params | the SVM parameters used in the respective kernel functions |
[in] | A | the matrix of the equation \(Ax = b\) (symmetric positive definite) |
[in] | b | the right-hand side of the equation \(Ax = b\) |
[in] | eps | the error tolerance |
[in] | max_iter | the maximum number of CG iterations |
plssvm::exception | any exception thrown by the backend's implementation |
[[nodiscard]]
)
|
inlineoverrideprotectedvirtual |
Uses the already learned model to predict the class of multiple (new) data points.
[in] | params | the SVM parameters used in the respective kernel functions |
[in] | support_vectors | the previously learned support vectors |
[in] | alpha | the alpha values (weights) associated with the support vectors |
[in] | rho | the rho value determined after training the model |
[in,out] | w | the 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_points | the points to predict |
plssvm::exception | any exception thrown by the backend's implementation |
[[nodiscard]]
) Implements plssvm::csvm.
|
inlineoverrideprotectedvirtual |
Uses the already learned model to predict the class of multiple (new) data points.
[in] | params | the SVM parameters used in the respective kernel functions |
[in] | support_vectors | the previously learned support vectors |
[in] | alpha | the alpha values (weights) associated with the support vectors |
[in] | rho | the rho value determined after training the model |
[in,out] | w | the 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_points | the points to predict |
plssvm::exception | any exception thrown by the backend's implementation |
[[nodiscard]]
) Implements plssvm::csvm.
|
protected |
Uses the already learned model to predict the class of multiple (new) data points.
[in] | params | the SVM parameters used in the respective kernel functions |
[in] | support_vectors | the previously learned support vectors |
[in] | alpha | the alpha values (weights) associated with the support vectors |
[in] | rho | the rho value determined after training the model |
[in,out] | w | the 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_points | the points to predict |
plssvm::exception | any exception thrown by the backend's implementation |
[[nodiscard]]
)
|
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
).
[in] | params | the SVM parameter used to calculate q (e.g., kernel_type) |
[in] | data | the data points used in the dimensional reduction. |
q
vector ([[nodiscard]]
)
|
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
).
[in] | support_vectors | the previously learned support vectors |
[in] | alpha | the previously learned weights |
w
vector ([[nodiscard]]
)
|
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
).
[in] | params | the SVM parameter used to calculate q (e.g., kernel_type) |
[in] | q | the q vector used in the dimensional reduction |
[out] | ret | the result vector |
[in] | d | the right-hand side of the equation |
[in] | data | the data points |
[in] | QA_cost | a value used in the dimensional reduction |
[in] | add | denotes whether the values are added or subtracted from the result vector |
|
private |
Initializes the OpenMP backend and performs some sanity checks.
[in] | target | the target platform to use |
plssvm::openmp::backend_exception | if the target platform isn't plssvm::target_platform::automatic or plssvm::target_platform::cpu |
plssvm::openmp::backend_exception | if the plssvm::target_platform::cpu target isn't available |
|
inlinenoexceptinherited |
Return the target platform (i.e, CPU or GPU including the vendor) this SVM runs on.
[[nodiscard]]
)
|
inlinenoexceptinherited |
Return the currently used SVM parameter.
[[nodiscard]]
)
|
inlinenoexceptinherited |
Override the old SVM parameter with the new plssvm::parameter params
.
[in] | params | the new SVM parameter to use |
|
inherited |
Override the old SVM parameter with the new ones given as named parameters in named_args
.
Args | the type of the named-parameters |
[in] | named_args | the potential named-parameters |
|
inherited |
Fit a model using the current SVM on the data
.
real_type | the type of the data (float or double ) |
label_type | the type of the label (an arithmetic type or std::string ) |
Args | the type of the potential additional parameters |
[in] | data | the data used to train the SVM model |
[in] | named_args | the potential additional parameters (epsilon and/or max_iter ) |
plssvm::invalid_parameter_exception | if the provided value for epsilon is greater or equal than zero |
plssvm::invlaid_parameter_exception | if the provided maximum number of iterations is less or equal than zero |
plssvm::invalid_parameter_exception | if the training data does not include labels |
plssvm::exception | any exception thrown in the respective backend's implementation of plssvm::csvm::solve_system_of_linear_equations |
[[nodiscard]]
)
|
inherited |
Predict the labels for the data
set using the model
.
real_type | the type of the data (float or double ) |
label_type | the type of the label (an arithmetic type or std::string ) |
[in] | model | a previously learned model |
[in] | data | the data to predict the labels for |
plssvm::invalid_parameter_exception | if the number of features in the model's support vectors don't match the number of features in the data set |
plssvm::exception | any exception thrown in the respective backend's implementation of plssvm::csvm::predict_values |
[[nodiscard]]
)
|
inherited |
Calculate the accuracy of the model
.
real_type | the type of the data (float or double ) |
label_type | the type of the label (an arithmetic type or std::string ) |
[in] | model | a previously learned model |
plssvm::exception | any exception thrown in the respective backend's implementation of plssvm::csvm::predict_values |
[[nodiscard]]
)
|
inherited |
Calculate the accuracy of the labeled data
set using the model
.
real_type | the type of the data (float or double ) |
label_type | the type of the label (an arithmetic type or std::string ) |
[in] | model | a previously learned model |
[in] | data | the labeled data set to score |
plssvm::invalid_parameter_exception | if the data to score has no labels |
plssvm::invalid_parameter_exception | if the number of features in the model's support vectors don't match the number of features in the data set |
plssvm::exception | any exception thrown in the respective backend's implementation of plssvm::csvm::predict_values |
data
([[nodiscard]]
)
|
inlineprivateinherited |
Perform some sanity checks on the passed SVM parameters.
plssvm::invalid_parameter_exception | if the kernel function is invalid |
plssvm::invalid_parameter_exception | if the gamma value for the polynomial or radial basis function kernel is not greater than zero |