12 #ifndef PLSSVM_CSVM_HPP_
13 #define PLSSVM_CSVM_HPP_
30 #include "igor/igor.hpp"
35 #include <type_traits>
63 template <
typename... Args>
64 explicit csvm(Args &&...args);
110 template <
typename... Args,
PLSSVM_REQUIRES(detail::has_only_parameter_named_args_v<Args...>)>
129 template <
typename real_type,
typename label_type,
typename... Args>
145 template <
typename real_type,
typename label_type>
156 template <
typename real_type,
typename label_type>
169 template <
typename real_type,
typename label_type>
204 [[nodiscard]]
virtual 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 = 0;
208 [[nodiscard]]
virtual 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 = 0;
229 template <
typename... Args>
231 params_{ std::forward<Args>(named_args)... } {
237 static_assert(
sizeof...(Args) > 0,
"At least one named parameter mus be given when calling set_params()!");
240 parameter provided_params{ std::forward<Args>(named_args)... };
243 if (!provided_params.kernel_type.is_default()) {
246 if (!provided_params.gamma.is_default()) {
249 if (!provided_params.degree.is_default()) {
252 if (!provided_params.coef0.is_default()) {
255 if (!provided_params.cost.is_default()) {
263 template <
typename real_type,
typename label_type,
typename... Args>
265 igor::parser parser{ std::forward<Args>(named_args)... };
272 static_assert(!parser.has_unnamed_arguments(),
"Can only use named parameter!");
274 static_assert(!parser.has_duplicates(),
"Can only use each named parameter once!");
276 static_assert(!parser.has_other_than(epsilon, max_iter),
"An illegal named parameter has been passed!");
279 if constexpr (parser.has(epsilon)) {
281 epsilon_val = detail::get_value_from_named_parameter<typename decltype(epsilon_val)::value_type>(parser, epsilon);
283 if (epsilon_val <=
static_cast<typename decltype(epsilon_val)::value_type
>(0)) {
287 if constexpr (parser.has(max_iter)) {
289 max_iter_val = detail::get_value_from_named_parameter<typename decltype(max_iter_val)::value_type>(parser, max_iter);
291 if (max_iter_val ==
static_cast<typename decltype(max_iter_val)::value_type
>(0)) {
304 if (params.gamma.is_default()) {
309 const std::chrono::time_point start_time = std::chrono::steady_clock::now();
317 const std::chrono::time_point end_time = std::chrono::steady_clock::now();
319 "Solved minimization problem (r = b - Ax) using the Conjugate Gradient (CG) methode in {}.\n\n",
320 detail::tracking_entry{
"cg",
"total_runtime", std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time) });
325 template <
typename real_type,
typename label_type>
335 std::vector<label_type> predicted_labels(predicted_values.size());
337 #pragma omp parallel for default(none) shared(predicted_labels, predicted_values, model) if (!std::is_same_v<label_type, bool>)
338 for (
typename std::vector<label_type>::size_type i = 0; i < predicted_labels.size(); ++i) {
342 return predicted_labels;
345 template <
typename real_type,
typename label_type>
350 template <
typename real_type,
typename label_type>
362 const std::vector<label_type> predicted_labels =
predict(
model, data);
364 const std::vector<label_type> &correct_labels = data.
labels().value();
367 typename std::vector<label_type>::size_type correct{ 0 };
368 #pragma omp parallel for reduction(+ : correct) default(none) shared(predicted_labels, correct_labels)
369 for (
typename std::vector<label_type>::size_type i = 0; i < predicted_labels.size(); ++i) {
370 if (predicted_labels[i] == correct_labels[i]) {
374 return static_cast<real_type
>(correct) /
static_cast<real_type
>(predicted_labels.size());
399 template <
typename T>
409 template <
typename T>
415 template <
typename T>
Base class for all C-SVM backends.
Definition: csvm.hpp:50
parameter params_
The SVM parameter (e.g., cost, degree, gamma, coef0) currently in use.
Definition: csvm.hpp:221
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.
Definition: csvm.hpp:326
csvm(const csvm &)=delete
Delete copy-constructor since a CSVM is a move-only type.
virtual 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 =0
Uses the already learned model to predict the class of multiple (new) data points.
virtual 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 =0
Solves the equation using the Conjugated Gradients algorithm.
virtual 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 =0
Uses the already learned model to predict the class of multiple (new) data points.
target_platform get_target_platform() const noexcept
Return the target platform (i.e, CPU or GPU including the vendor) this SVM runs on.
Definition: csvm.hpp:93
csvm(parameter params={})
Construct a C-SVM using the SVM parameter params.
Definition: csvm.hpp:224
parameter get_params() const noexcept
Return the currently used SVM parameter.
Definition: csvm.hpp:98
csvm(csvm &&) noexcept=default
Default move-constructor since a virtual destructor has been declared.
virtual 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 =0
Solves the equation using the Conjugated Gradients algorithm.
real_type score(const model< real_type, label_type > &model) const
Calculate the accuracy of the model.
Definition: csvm.hpp:346
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.
Definition: csvm.hpp:264
void set_params(parameter params) noexcept
Override the old SVM parameter with the new plssvm::parameter params.
Definition: csvm.hpp:104
target_platform target_
The target platform of this SVM.
Definition: csvm.hpp:211
void sanity_check_parameter() const
Perform some sanity checks on the passed SVM parameters.
Definition: csvm.hpp:377
bool has_labels() const noexcept
Returns whether this data set contains labels or not.
Definition: data_set.hpp:194
std::shared_ptr< const label_mapper > mapping_
The mapping used to convert the original label to its mapped value and vice versa; may be nullptr if ...
Definition: data_set.hpp:285
const std::vector< std::vector< real_type > > & data() const noexcept
Return the data points in this data set.
Definition: data_set.hpp:189
std::shared_ptr< std::vector< real_type > > y_ptr_
A pointer to the mapped values of the labels of this data set; may be nullptr if no labels have been ...
Definition: data_set.hpp:277
size_type num_features() const noexcept
Returns the number of features in this data set.
Definition: data_set.hpp:218
optional_ref< const std::vector< label_type > > labels() const noexcept
Returns an optional reference to the labels in this data set.
Definition: data_set.hpp:625
size_type num_data_points() const noexcept
Returns the number of data points in this data set.
Definition: data_set.hpp:213
This class encapsulates a value that may be a default value or not.
Definition: default_value.hpp:62
constexpr const value_type & value() const noexcept
Get the currently active value: the user provided value if provided, otherwise the default value is r...
Definition: default_value.hpp:150
constexpr bool is_default() const noexcept
Check whether the currently active value is the default value.
Definition: default_value.hpp:170
Exception type thrown if the provided parameter is invalid.
Definition: exceptions.hpp:62
Implements a class encapsulating the result of a call to the SVM fit function. A model is used to pre...
Definition: model.hpp:50
data_set< real_type, label_type > data_
The data (support vectors + respective label) used to learn this model.
Definition: model.hpp:150
real_type rho_
The bias after learning this model.
Definition: model.hpp:159
parameter params_
The SVM parameter used to learn this model.
Definition: model.hpp:148
size_type num_features() const noexcept
The number of features of the support vectors used in this model.
Definition: model.hpp:88
std::shared_ptr< std::vector< real_type > > w_
A vector used to speedup the prediction in case of the linear kernel function.
Definition: model.hpp:166
std::shared_ptr< std::vector< real_type > > alpha_ptr_
The learned weights for each support vector.
Definition: model.hpp:157
Implements a data set class encapsulating all data points, features, and potential labels.
Implements a class used to be able to distinguish between the default value of a variable and an assi...
Defines universal utility functions.
Implements custom exception classes derived from std::runtime_error including source location informa...
Defines an enumeration holding all possible kernel function types.
Defines a simple logging function.
Implements a model class encapsulating the results of a SVM fit call.
void log(const verbosity_level verb, const std::string_view msg, Args &&...args)
Definition: logger.hpp:109
constexpr bool has_only_parameter_named_args_v
Trait to check whether Args only contains named-parameter that can be used to initialize a plssvm::pa...
Definition: parameter.hpp:66
constexpr std::underlying_type_t< Enum > to_underlying(const Enum e) noexcept
Converts an enumeration to its underlying type.
Definition: utility.hpp:63
constexpr T sign(const T x)
Returns +1 if x is positive and -1 if x is negative or 0.
Definition: operators.hpp:179
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
constexpr bool csvm_backend_exists_v
Sets the value of the value member to true if T is a C-SVM using an available backend....
Definition: csvm.hpp:416
Defines (arithmetic) functions on std::vector and scalars.
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
This class denotes an explicit default value initialization used to distinguish between the default v...
Definition: default_value.hpp:37
default_value< real_type > cost
The cost parameter in the C-SVM.
Definition: parameter.hpp:165
default_value< real_type > coef0
The coef0 parameter used in the polynomial kernel function.
Definition: parameter.hpp:163
default_value< int > degree
The degree parameter used in the polynomial kernel function.
Definition: parameter.hpp:159
default_value< kernel_function_type > kernel_type
The used kernel function: linear, polynomial, or radial basis functions (rbf).
Definition: parameter.hpp:157
default_value< real_type > gamma
The gamma parameter used in the polynomial and rbf kernel functions.
Definition: parameter.hpp:161
A single tracking entry containing a specific category, a unique name, and the actual value to be tra...
Definition: performance_tracker.hpp:40
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