PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
csvm.hpp
Go to the documentation of this file.
1 
12 #ifndef PLSSVM_BACKENDS_OPENMP_CSVM_HPP_
13 #define PLSSVM_BACKENDS_OPENMP_CSVM_HPP_
14 #pragma once
15 
16 #include "plssvm/csvm.hpp" // plssvm::csvm
17 #include "plssvm/detail/type_traits.hpp" // PLSSVM_REQUIRES
18 #include "plssvm/parameter.hpp" // plssvm::parameter, plssvm::detail::{parameter, has_only_parameter_named_args_v}
19 #include "plssvm/target_platforms.hpp" // plssvm::target_platform
20 
21 #include <type_traits> // std::true_type
22 #include <utility> // std::forward, std::pair
23 #include <vector> // std::vector
24 
25 namespace plssvm {
26 
27 namespace openmp {
28 
32 class csvm : public ::plssvm::csvm {
33  public:
41  explicit csvm(parameter params = {});
50  explicit csvm(target_platform target, parameter params = {});
51 
59  template <typename... Args, PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v<Args...>)>
60  explicit csvm(Args &&...named_args) :
61  ::plssvm::csvm{ std::forward<Args>(named_args)... } {
62  // the default target is the automatic one
64  }
73  template <typename... Args, PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v<Args...>)>
74  explicit csvm(const target_platform target, Args &&...named_args) :
75  ::plssvm::csvm{ std::forward<Args>(named_args)... } {
76  this->init(target);
77  }
78 
82  csvm(const csvm &) = delete;
86  csvm(csvm &&) noexcept = default;
90  csvm &operator=(const csvm &) = delete;
94  csvm &operator=(csvm &&) noexcept = default;
98  ~csvm() override = default;
99 
100  protected:
104  [[nodiscard]] 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 { return this->solve_system_of_linear_equations_impl(params, A, b, eps, max_iter); }
108  [[nodiscard]] 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 { return this->solve_system_of_linear_equations_impl(params, A, b, eps, max_iter); }
112  template <typename real_type>
113  [[nodiscard]] 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;
114 
118  [[nodiscard]] 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 { return this->predict_values_impl(params, support_vectors, alpha, rho, w, predict_points); }
122  [[nodiscard]] 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 { return this->predict_values_impl(params, support_vectors, alpha, rho, w, predict_points); }
126  template <typename real_type>
127  [[nodiscard]] 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;
128 
136  template <typename real_type>
137  [[nodiscard]] std::vector<real_type> generate_q(const detail::parameter<real_type> &params, const std::vector<std::vector<real_type>> &data) const;
145  template <typename real_type>
146  [[nodiscard]] std::vector<real_type> calculate_w(const std::vector<std::vector<real_type>> &support_vectors, const std::vector<real_type> &alpha) const;
147 
159  template <typename real_type>
160  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;
161 
162  private:
169  void init(target_platform target);
170 };
171 
172 } // namespace openmp
173 
174 namespace detail {
175 
179 template <>
180 struct csvm_backend_exists<openmp::csvm> : std::true_type {};
181 
182 } // namespace detail
183 
184 } // namespace plssvm
185 
186 #endif // PLSSVM_BACKENDS_OPENMP_CSVM_HPP_
Base class for all C-SVM backends.
Definition: csvm.hpp:50
A C-SVM implementation using OpenMP as backend.
Definition: csvm.hpp:32
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 ...
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.
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.
Definition: csvm.hpp:118
void init(target_platform target)
Initializes the OpenMP backend and performs some sanity checks.
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 using the Conjugated Gradients algorithm.
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.
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 using the Conjugated Gradients algorithm.
Definition: csvm.hpp:108
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.
csvm(Args &&...named_args)
Construct a new C-SVM using the OpenMP backend and the optionally provided named_args.
Definition: csvm.hpp:60
csvm(csvm &&) noexcept=default
Default move-constructor since a virtual destructor has been declared. noexcept
csvm(const csvm &)=delete
Delete copy-constructor since a CSVM is a move-only type.
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 nam...
Definition: csvm.hpp:74
csvm(target_platform target, parameter params={})
Construct a new C-SVM using the OpenMP backend on the target platform with the parameters given throu...
csvm(parameter params={})
Construct a new C-SVM using the OpenMP backend with the parameters given through params.
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 using the Conjugated Gradients algorithm.
Definition: csvm.hpp:104
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.
Definition: csvm.hpp:122
Defines the base class for all C-SVM backends and implements the functionality shared by all of them.
The main namespace containing all public API functions.
Definition: backend_types.hpp:24
target_platform
Enum class for all possible targets.
Definition: target_platforms.hpp:25
Implements the parameter class encapsulating all important C-SVM parameters.
Sets the value of the value member to true if T is a C-SVM using an available backend....
Definition: csvm.hpp:410
Defines an enumeration holding all possible target platforms. Can also include targets not available ...
Defines some generic type traits used in the PLSSVM library.
#define PLSSVM_REQUIRES(...)
A shorthand macro for the std::enable_if_t type trait.
Definition: type_traits.hpp:33