Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
svm.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <vector>
5#include "../utils/matrix.hpp"
6#include "../utils/autograd.hpp"
7
15namespace ml {
16 using namespace utils;
17
21 enum class KernelType {
22 LINEAR,
24 RBF,
25 SIGMOID
26 };
27
51 template<typename T>
52 class SVM {
53 public:
65 SVM(KernelType kernel_type = KernelType::RBF, T C = 1.0, T gamma = 1.0,
66 int degree = 3, T coef0 = 0.0, T tol = 1e-3, size_t max_iter = 1000, T learning_rate = 0.01);
67
73 void fit(const Matrix<T> &X, const std::vector<int> &y);
74
80 std::vector<int> predict(const Matrix<T> &X) const;
81
87 Matrix<T> predict_proba(const Matrix<T> &X) const;
88
94 std::vector<T> decision_function(const Matrix<T> &X) const;
95
101
106 std::vector<size_t> support() const;
107
112 std::vector<T> dual_coef() const;
113
118 T intercept() const;
119
124 std::vector<T> loss_history() const { return loss_history_; }
125
126 private:
127 KernelType kernel_type_;
128 T C_;
129 T gamma_;
130 int degree_;
131 T coef0_;
132 T tol_;
133 size_t max_iter_;
134 T learning_rate_;
135
136 Variable<T> weights_;
137 Variable<T> bias_;
138 Matrix<T> support_vectors_;
139 std::vector<size_t> support_indices_;
140 std::vector<T> dual_coef_;
141 std::vector<int> classes_;
142 bool is_fitted_;
143 std::vector<T> loss_history_;
144
151 Variable<T> kernel(const Variable<T> &x1, const Variable<T> &x2) const;
152
159 Variable<T> kernel_matrix(const Variable<T> &X1, const Variable<T> &X2) const;
160
167 Variable<T> compute_loss(const Variable<T> &X, const std::vector<int> &y) const;
168
175 T gradient_step(const Matrix<T> &X, const std::vector<int> &y);
176
183 Variable<T> to_variable(const Matrix<T> &matrix, bool requires_grad = false) const;
184 };
185
186 // Type aliases
189
190} // namespace ml
std::vector< size_t > support() const
Get support vector indices.
Definition svm.cpp:337
std::vector< T > decision_function(const Matrix< T > &X) const
Compute the decision function for samples.
Definition svm.cpp:290
Matrix< T > support_vectors() const
Get support vectors.
Definition svm.cpp:332
Matrix< T > predict_proba(const Matrix< T > &X) const
Predict class probabilities for samples.
Definition svm.cpp:317
std::vector< int > predict(const Matrix< T > &X) const
Predict class labels for samples.
Definition svm.cpp:274
void fit(const Matrix< T > &X, const std::vector< int > &y)
Fit the SVM model to training data using automatic differentiation.
Definition svm.cpp:31
T intercept() const
Get intercept term.
Definition svm.cpp:347
std::vector< T > dual_coef() const
Get dual coefficients.
Definition svm.cpp:342
std::vector< T > loss_history() const
Get training loss history.
Definition svm.hpp:124
Namespace containing traditional machine learning algorithms.
Definition kmeans.hpp:15
KernelType
Kernel function types for SVM.
Definition svm.hpp:21
@ RBF
Radial Basis Function kernel: K(x, y) = exp(-gamma * ||x - y||^2)
@ LINEAR
Linear kernel: K(x, y) = x^T * y.
@ SIGMOID
Sigmoid kernel: K(x, y) = tanh(gamma * x^T * y + coef0)
@ POLYNOMIAL
Polynomial kernel: K(x, y) = (gamma * x^T * y + coef0)^degree.