56 KiB
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. | |
Detailed Description
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.
Constructor & Destructor Documentation
◆ DataConverter()
svm_classifier::DataConverter::DataConverter | ( | ) |
Default constructor.
Definition at line 10 of file data_converter.cpp.
◆ ~DataConverter()
svm_classifier::DataConverter::~DataConverter | ( | ) |
Destructor - cleans up allocated memory.
Definition at line 17 of file data_converter.cpp.
Member Function Documentation
◆ cleanup()
void svm_classifier::DataConverter::cleanup | ( | ) |
Clean up all allocated memory.
Definition at line 198 of file data_converter.cpp.
◆ from_decision_values() [1/2]
torch::Tensor svm_classifier::DataConverter::from_decision_values | ( | const std::vector< std::vector< double > > & | decision_values | ) |
Convert decision values back to PyTorch tensor.
- Parameters
-
decision_values 2D vector of decision function values
- Returns
- PyTorch tensor with decision values
Definition at line 152 of file data_converter.cpp.
◆ from_decision_values() [2/2]
torch::Tensor svm_classifier::DataConverter::from_decision_values | ( | const std::vector< std::vector< double > > & | decision_values | ) |
Convert decision values back to PyTorch tensor.
- Parameters
-
decision_values 2D vector of decision function values
- Returns
- PyTorch tensor with decision values
◆ from_predictions() [1/2]
torch::Tensor svm_classifier::DataConverter::from_predictions | ( | const std::vector< double > & | predictions | ) |
Convert predictions back to PyTorch tensor.
- Parameters
-
predictions Vector of predictions
- Returns
- PyTorch tensor with predictions
Definition at line 120 of file data_converter.cpp.
◆ from_predictions() [2/2]
torch::Tensor svm_classifier::DataConverter::from_predictions | ( | const std::vector< double > & | predictions | ) |
Convert predictions back to PyTorch tensor.
- Parameters
-
predictions Vector of predictions
- Returns
- PyTorch tensor with predictions
◆ from_probabilities() [1/2]
torch::Tensor svm_classifier::DataConverter::from_probabilities | ( | const std::vector< std::vector< double > > & | probabilities | ) |
Convert probabilities back to PyTorch tensor.
- Parameters
-
probabilities 2D vector of class probabilities
- Returns
- PyTorch tensor with probabilities of shape (n_samples, n_classes)
Definition at line 132 of file data_converter.cpp.
◆ from_probabilities() [2/2]
torch::Tensor svm_classifier::DataConverter::from_probabilities | ( | const std::vector< std::vector< double > > & | probabilities | ) |
Convert probabilities back to PyTorch tensor.
- Parameters
-
probabilities 2D vector of class probabilities
- Returns
- PyTorch tensor with probabilities of shape (n_samples, n_classes)
◆ get_n_features() [1/2]
|
inline |
Get number of features from last conversion.
- Returns
- Number of features
Definition at line 100 of file data_converter.hpp.
◆ get_n_features() [2/2]
|
inline |
Get number of features from last conversion.
- Returns
- Number of features
Definition at line 100 of file kernel_parameters.hpp.
◆ get_n_samples() [1/2]
|
inline |
Get number of samples from last conversion.
- Returns
- Number of samples
Definition at line 106 of file data_converter.hpp.
◆ get_n_samples() [2/2]
|
inline |
Get number of samples from last conversion.
- Returns
- Number of samples
Definition at line 106 of file kernel_parameters.hpp.
◆ get_sparse_threshold() [1/2]
|
inline |
Get sparse threshold.
- Returns
- Current sparse threshold
Definition at line 123 of file data_converter.hpp.
◆ get_sparse_threshold() [2/2]
|
inline |
Get sparse threshold.
- Returns
- Current sparse threshold
Definition at line 123 of file kernel_parameters.hpp.
◆ set_sparse_threshold() [1/2]
|
inline |
Set sparse threshold (features with absolute value below this are ignored)
- Parameters
-
threshold Sparse threshold (default: 1e-8)
Definition at line 117 of file data_converter.hpp.
◆ set_sparse_threshold() [2/2]
|
inline |
Set sparse threshold (features with absolute value below this are ignored)
- Parameters
-
threshold Sparse threshold (default: 1e-8)
Definition at line 117 of file kernel_parameters.hpp.
◆ to_feature_node() [1/2]
feature_node * svm_classifier::DataConverter::to_feature_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to liblinear format.
- Parameters
-
sample Feature tensor of shape (n_features,)
- Returns
- Pointer to feature_node array
Definition at line 110 of file data_converter.cpp.
◆ to_feature_node() [2/2]
feature_node * svm_classifier::DataConverter::to_feature_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to liblinear format.
- Parameters
-
sample Feature tensor of shape (n_features,)
- Returns
- Pointer to feature_node array
◆ to_linear_problem() [1/2]
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.
- Parameters
-
X Feature tensor of shape (n_samples, n_features) y Target tensor of shape (n_samples,) - optional for prediction
- Returns
- Pointer to problem structure
Definition at line 60 of file data_converter.cpp.
◆ to_linear_problem() [2/2]
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.
- Parameters
-
X Feature tensor of shape (n_samples, n_features) y Target tensor of shape (n_samples,) - optional for prediction
- Returns
- Pointer to problem structure
◆ to_svm_node() [1/2]
svm_node * svm_classifier::DataConverter::to_svm_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to libsvm format.
- Parameters
-
sample Feature tensor of shape (n_features,)
- Returns
- Pointer to svm_node array
Definition at line 100 of file data_converter.cpp.
◆ to_svm_node() [2/2]
svm_node * svm_classifier::DataConverter::to_svm_node | ( | const torch::Tensor & | sample | ) |
Convert single sample to libsvm format.
- Parameters
-
sample Feature tensor of shape (n_features,)
- Returns
- Pointer to svm_node array
◆ to_svm_problem() [1/2]
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.
- Parameters
-
X Feature tensor of shape (n_samples, n_features) y Target tensor of shape (n_samples,) - optional for prediction
- Returns
- Pointer to svm_problem structure
Definition at line 22 of file data_converter.cpp.
◆ to_svm_problem() [2/2]
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.
- Parameters
-
X Feature tensor of shape (n_samples, n_features) y Target tensor of shape (n_samples,) - optional for prediction
- Returns
- Pointer to svm_problem structure
◆ validate_tensors() [1/2]
void svm_classifier::DataConverter::validate_tensors | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Validate input tensors.
- Parameters
-
X Feature tensor y Target tensor (optional)
- Exceptions
-
std::invalid_argument if tensors are invalid
Definition at line 172 of file data_converter.cpp.
◆ validate_tensors() [2/2]
void svm_classifier::DataConverter::validate_tensors | ( | const torch::Tensor & | X, |
const torch::Tensor & | y = torch::Tensor() |
||
) |
Validate input tensors.
- Parameters
-
X Feature tensor y Target tensor (optional)
- Exceptions
-
std::invalid_argument if tensors are invalid
The documentation for this class was generated from the following files:
- include/svm_classifier/data_converter.hpp
- include/svm_classifier/kernel_parameters.hpp
- src/data_converter.cpp
Generated on Sun Jun 22 2025 11:25:27 for SVM Classifier C++ by