12 #ifndef PLSSVM_MODEL_HPP_
13 #define PLSSVM_MODEL_HPP_
25 #include "fmt/chrono.h"
49 template <
typename T,
typename U =
int>
52 static_assert(detail::type_list_contains_v<T, detail::real_type_list>,
"Illegal real type provided! See the 'real_type_list' in the type_list.hpp header for a list of the allowed types.");
53 static_assert(detail::type_list_contains_v<U, detail::label_type_list>,
"Illegal label type provided! See the 'label_type_list' in the type_list.hpp header for a list of the allowed types.");
71 explicit model(
const std::string &filename);
77 void save(
const std::string &filename)
const;
107 [[nodiscard]]
const std::vector<label_type> &
labels() const noexcept {
return data_.
labels()->get(); }
127 [[nodiscard]]
const std::vector<real_type> &
weights() const noexcept {
157 std::shared_ptr<std::vector<real_type>>
alpha_ptr_{
nullptr };
166 std::shared_ptr<std::vector<real_type>>
w_{ std::make_shared<std::vector<real_type>>() };
169 template <
typename T,
typename U>
171 const std::chrono::time_point start_time = std::chrono::steady_clock::now();
178 std::vector<label_type> labels{};
179 std::size_t num_header_lines{};
180 std::tie(params_, rho_, labels, num_header_lines) = detail::io::parse_libsvm_model_header<real_type, label_type, size_type>(reader.lines());
183 std::vector<std::vector<real_type>> support_vectors;
184 std::vector<real_type> alphas;
187 std::tie(num_support_vectors_, num_features_, support_vectors, alphas) = detail::io::parse_libsvm_data<real_type, real_type>(reader, num_header_lines);
191 alpha_ptr_ = std::make_shared<decltype(alphas)>(std::move(alphas));
193 const std::chrono::time_point end_time = std::chrono::steady_clock::now();
195 "Read {} support vectors with {} features in {} using the libsvm model parser from file '{}'.\n\n",
198 detail::tracking_entry{
"model_read",
"time", std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time) },
203 template <
typename T,
typename U>
205 params_{ std::move(params) }, data_{ std::move(data) }, num_support_vectors_{ data_.num_data_points() }, num_features_{ data_.num_features() }, alpha_ptr_{ std::make_shared<std::vector<
real_type>>(data_.num_data_points()) } {}
207 template <
typename T,
typename U>
209 const std::chrono::time_point start_time = std::chrono::steady_clock::now();
214 const std::chrono::time_point end_time = std::chrono::steady_clock::now();
216 "Write {} support vectors with {} features in {} to the libsvm model file '{}'.\n",
219 detail::tracking_entry{
"model_write",
"time", std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time) },
Implements a custom assert macro PLSSVM_ASSERT.
#define PLSSVM_ASSERT(cond, msg,...)
Defines the PLSSVM_ASSERT macro if PLSSVM_ASSERT_ENABLED is defined.
Definition: assert.hpp:74
Base class for all C-SVM backends.
Definition: csvm.hpp:50
size_type num_different_labels() const noexcept
Returns the number of different labels in this data set.
Definition: data_set.hpp:225
const std::vector< std::vector< real_type > > & data() const noexcept
Return the data points in this data set.
Definition: data_set.hpp:189
std::optional< std::vector< label_type > > different_labels() const
Returns an optional to the different labels in this data set.
Definition: data_set.hpp:633
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
The plssvm::detail::file_reader class is responsible for reading a file and splitting it into its lin...
Definition: file_reader.hpp:42
const std::vector< std::string_view > & read_lines(std::string_view comment={ "\n" })
Read the content of the associated file and split it into lines, ignoring empty lines and lines start...
Implements a class encapsulating the result of a call to the SVM fit function. A model is used to pre...
Definition: model.hpp:50
const std::vector< std::vector< real_type > > & support_vectors() const noexcept
The support vectors representing the learned model.
Definition: model.hpp:100
size_type num_features_
The number of features per support vector.
Definition: model.hpp:154
T real_type
The type of the data points: either float or double.
Definition: model.hpp:60
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
const std::vector< label_type > & labels() const noexcept
Returns the labels of the support vectors.
Definition: model.hpp:107
void save(const std::string &filename) const
Save the model to a LIBSVM model file for later usage.
Definition: model.hpp:208
size_type num_features() const noexcept
The number of features of the support vectors used in this model.
Definition: model.hpp:88
const std::vector< real_type > & weights() const noexcept
The learned weights for the support vectors.
Definition: model.hpp:127
U label_type
The type of the labels: any arithmetic type or std::string.
Definition: model.hpp:62
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
const parameter & get_params() const noexcept
Return the SVM parameter that were used to learn this model.
Definition: model.hpp:94
std::vector< label_type > different_labels() const
Returns the different labels of the support vectors.
Definition: model.hpp:120
std::size_t size_type
The unsigned size type.
Definition: model.hpp:64
size_type num_different_labels() const noexcept
Returns the number of different labels in this data set.
Definition: model.hpp:114
real_type rho() const noexcept
The bias value after learning.
Definition: model.hpp:135
size_type num_support_vectors_
The number of support vectors representing this model.
Definition: model.hpp:152
size_type num_support_vectors() const noexcept
The number of support vectors used in this model.
Definition: model.hpp:83
std::shared_ptr< std::vector< real_type > > alpha_ptr_
The learned weights for each support vector.
Definition: model.hpp:157
model(const std::string &filename)
Read a previously learned model from the LIBSVM model file filename.
Definition: model.hpp:170
Implements a data set class encapsulating all data points, features, and potential labels.
Implements parsing functions for the LIBSVM model file.
Implements parsing functions for the LIBSVM file format.
Defines a simple logging function.
void write_libsvm_model_data(const std::string &filename, const plssvm::parameter ¶ms, const real_type rho, const std::vector< real_type > &alpha, const data_set< real_type, label_type > &data)
Write the LIBSVM model to the file filename.
Definition: libsvm_model_parsing.hpp:371
void log(const verbosity_level verb, const std::string_view msg, Args &&...args)
Definition: logger.hpp:109
The main namespace containing all public API functions.
Definition: backend_types.hpp:24
Implements the parameter class encapsulating all important C-SVM parameters.
A single tracking entry containing a specific category, a unique name, and the actual value to be tra...
Definition: performance_tracker.hpp:40
All possible real_type and label_type combinations for a plssvm::model and plssvm::data_set.