SVM Classifier C++ 1.0.0
High-performance Support Vector Machine classifier with scikit-learn compatible API
|
Data converter between libtorch tensors and SVM library formats. More...
#include <data_converter.hpp>
Public Member Functions | |
DataConverter () | |
Default constructor. | |
~DataConverter () | |
Destructor - cleans up allocated memory. | |
std::unique_ptr< svm_problem > | to_svm_problem (const torch::Tensor &X, const torch::Tensor &y=torch::Tensor()) |
Convert PyTorch tensors to libsvm format. | |
std::unique_ptr< problem > | to_linear_problem (const torch::Tensor &X, const torch::Tensor &y=torch::Tensor()) |
Convert PyTorch tensors to liblinear format. | |
svm_node * | to_svm_node (const torch::Tensor &sample) |
Convert single sample to libsvm format. | |
feature_node * | to_feature_node (const torch::Tensor &sample) |
Convert single sample to liblinear format. | |
torch::Tensor | from_predictions (const std::vector< double > &predictions) |
Convert predictions back to PyTorch tensor. | |
torch::Tensor | from_probabilities (const std::vector< std::vector< double > > &probabilities) |
Convert probabilities back to PyTorch tensor. | |
torch::Tensor | from_decision_values (const std::vector< std::vector< double > > &decision_values) |
Convert decision values back to PyTorch tensor. | |
void | validate_tensors (const torch::Tensor &X, const torch::Tensor &y=torch::Tensor()) |
Validate input tensors. | |
int | get_n_features () const |
Get number of features from last conversion. | |
int | get_n_samples () const |
Get number of samples from last conversion. | |
void | cleanup () |
Clean up all allocated memory. | |
void | set_sparse_threshold (double threshold) |
Set sparse threshold (features with absolute value below this are ignored) | |
double | get_sparse_threshold () const |
Get sparse threshold. | |
DataConverter () | |
Default constructor. | |
~DataConverter () | |
Destructor - cleans up allocated memory. | |
std::unique_ptr< svm_problem > | to_svm_problem (const torch::Tensor &X, const torch::Tensor &y=torch::Tensor()) |
Convert PyTorch tensors to libsvm format. | |
std::unique_ptr< problem > | to_linear_problem (const torch::Tensor &X, const torch::Tensor &y=torch::Tensor()) |
Convert PyTorch tensors to liblinear format. | |
svm_node * | to_svm_node (const torch::Tensor &sample) |
Convert single sample to libsvm format. | |
feature_node * | to_feature_node (const torch::Tensor &sample) |
Convert single sample to liblinear format. | |
torch::Tensor | from_predictions (const std::vector< double > &predictions) |
Convert predictions back to PyTorch tensor. | |
torch::Tensor | from_probabilities (const std::vector< std::vector< double > > &probabilities) |
Convert probabilities back to PyTorch tensor. | |
torch::Tensor | from_decision_values (const std::vector< std::vector< double > > &decision_values) |
Convert decision values back to PyTorch tensor. | |
void | validate_tensors (const torch::Tensor &X, const torch::Tensor &y=torch::Tensor()) |
Validate input tensors. | |
int | get_n_features () const |
Get number of features from last conversion. | |
int | get_n_samples () const |
Get number of samples from last conversion. | |
void | cleanup () |
Clean up all allocated memory. | |
void | set_sparse_threshold (double threshold) |
Set sparse threshold (features with absolute value below this are ignored) | |
double | get_sparse_threshold () const |
Get sparse threshold. | |
Data converter between libtorch tensors and SVM library formats.
This class handles the conversion between PyTorch tensors and the data structures required by libsvm and liblinear libraries. It manages memory allocation and provides efficient conversion methods.
Definition at line 23 of file data_converter.hpp.
svm_classifier::DataConverter::DataConverter | ( | ) |
Default constructor.
Definition at line 10 of file data_converter.cpp.
svm_classifier::DataConverter::~DataConverter | ( | ) |
Destructor - cleans up allocated memory.
Definition at line 17 of file data_converter.cpp.
void svm_classifier::DataConverter::cleanup | ( | ) |
Clean up all allocated memory.
Definition at line 198 of file data_converter.cpp.
torch::Tensor svm_classifier::DataConverter::from_decision_values | ( | const std::vector< std::vector< double > > & | decision_values | ) |
Convert decision values back to PyTorch tensor.
decision_values | 2D vector of decision function values |
Definition at line 152 of file data_converter.cpp.
torch::Tensor svm_classifier::DataConverter::from_decision_values | ( | const std::vector< std::vector< double > > & | decision_values | ) |
Convert decision values back to PyTorch tensor.
decision_values | 2D vector of decision function values |
torch::Tensor svm_classifier::DataConverter::from_predictions | ( | const std::vector< double > & | predictions | ) |
Convert predictions back to PyTorch tensor.
predictions | Vector of predictions |
Definition at line 120 of file data_converter.cpp.
torch::Tensor svm_classifier::DataConverter::from_predictions | ( | const std::vector< double > & | predictions | ) |
Convert predictions back to PyTorch tensor.
predictions | Vector of predictions |
torch::Tensor svm_classifier::DataConverter::from_probabilities | ( | const std::vector< std::vector< double > > & | probabilities | ) |
Convert probabilities back to PyTorch tensor.
probabilities | 2D vector of class probabilities |
Definition at line 132 of file data_converter.cpp.
torch::Tensor svm_classifier::DataConverter::from_probabilities | ( | const std::vector< std::vector< double > > & | probabilities | ) |
Convert probabilities back to PyTorch tensor.
probabilities | 2D vector of class probabilities |
|
inline |
Get number of features from last conversion.
Definition at line 100 of file data_converter.hpp.
|
inline |
Get number of features from last conversion.
Definition at line 100 of file kernel_parameters.hpp.
|
inline |
Get number of samples from last conversion.
Definition at line 106 of file data_converter.hpp.
|
inline |
Get number of samples from last conversion.
Definition at line 106 of file kernel_parameters.hpp.
|
inline |
Get sparse threshold.
Definition at line 123 of file data_converter.hpp.
|
inline |
Get sparse threshold.
Definition at line 123 of file kernel_parameters.hpp.
|
inline |
Set sparse threshold (features with absolute value below this are ignored)
threshold | Sparse threshold (default: 1e-8) |
Definition at line 117 of file data_converter.hpp.
|
inline |
Set sparse threshold (features with absolute value below this are ignored)
threshold | Sparse threshold (default: 1e-8) |
Definition at line 117 of file kernel_parameters.hpp.
feature_node * svm_classifier::DataConverter::to_feature_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to liblinear format.
sample | Feature tensor of shape (n_features,) |
Definition at line 110 of file data_converter.cpp.
feature_node * svm_classifier::DataConverter::to_feature_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to liblinear format.
sample | Feature tensor of shape (n_features,) |
std::unique_ptr< problem > svm_classifier::DataConverter::to_linear_problem | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Convert PyTorch tensors to liblinear format.
X | Feature tensor of shape (n_samples, n_features) |
y | Target tensor of shape (n_samples,) - optional for prediction |
Definition at line 60 of file data_converter.cpp.
std::unique_ptr< problem > svm_classifier::DataConverter::to_linear_problem | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Convert PyTorch tensors to liblinear format.
X | Feature tensor of shape (n_samples, n_features) |
y | Target tensor of shape (n_samples,) - optional for prediction |
svm_node * svm_classifier::DataConverter::to_svm_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to libsvm format.
sample | Feature tensor of shape (n_features,) |
Definition at line 100 of file data_converter.cpp.
svm_node * svm_classifier::DataConverter::to_svm_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to libsvm format.
sample | Feature tensor of shape (n_features,) |
std::unique_ptr< svm_problem > svm_classifier::DataConverter::to_svm_problem | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Convert PyTorch tensors to libsvm format.
X | Feature tensor of shape (n_samples, n_features) |
y | Target tensor of shape (n_samples,) - optional for prediction |
Definition at line 22 of file data_converter.cpp.
std::unique_ptr< svm_problem > svm_classifier::DataConverter::to_svm_problem | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Convert PyTorch tensors to libsvm format.
X | Feature tensor of shape (n_samples, n_features) |
y | Target tensor of shape (n_samples,) - optional for prediction |
void svm_classifier::DataConverter::validate_tensors | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Validate input tensors.
X | Feature tensor |
y | Target tensor (optional) |
std::invalid_argument | if tensors are invalid |
Definition at line 172 of file data_converter.cpp.
void svm_classifier::DataConverter::validate_tensors | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Validate input tensors.
X | Feature tensor |
y | Target tensor (optional) |
std::invalid_argument | if tensors are invalid |