1 | // $Id: SVM.h 307 2005-05-03 13:28:29Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_svm_svm_ |
---|
4 | #define _theplu_svm_svm_ |
---|
5 | |
---|
6 | #include <c++_tools/svm/Kernel_MEV.h> |
---|
7 | #include <c++_tools/gslapi/vector.h> |
---|
8 | |
---|
9 | #include <utility> |
---|
10 | #include <vector> |
---|
11 | |
---|
12 | |
---|
13 | namespace theplu { |
---|
14 | namespace svm { |
---|
15 | /// |
---|
16 | /// Class for SVM using Keerthi's second modification of Platt's SMO. Also |
---|
17 | /// the elements of the kernel is not computed sequentially, but the |
---|
18 | /// complete kernel matrix is taken as input and stored in memory. This |
---|
19 | /// means that the training is faster, but also that it is not possible to |
---|
20 | /// train a large number of samples N, since the memory cost for the kernel |
---|
21 | /// matrix is N^2. The SVM object does not contain any data, hence any true |
---|
22 | /// prediction is not possible. |
---|
23 | /// |
---|
24 | class SVM |
---|
25 | { |
---|
26 | |
---|
27 | public: |
---|
28 | /// |
---|
29 | /// Constructor taking the kernel matrix and the target vector as input |
---|
30 | /// |
---|
31 | SVM(const Kernel_MEV&, |
---|
32 | const gslapi::vector&, |
---|
33 | const std::vector<size_t>& = std::vector<size_t>()); |
---|
34 | |
---|
35 | /// |
---|
36 | /// Function returns \f$\alpha\f$ |
---|
37 | /// |
---|
38 | inline gslapi::vector get_alpha(void) const { return alpha_; } |
---|
39 | |
---|
40 | /// |
---|
41 | /// Function returns the C-parameter |
---|
42 | /// |
---|
43 | inline double get_c(void) const { return c_; } |
---|
44 | |
---|
45 | /// |
---|
46 | /// @return number of maximal epochs |
---|
47 | /// |
---|
48 | inline long int max_epochs(void) const {return max_epochs_;} |
---|
49 | |
---|
50 | /// |
---|
51 | /// Changing number of maximal epochs |
---|
52 | /// |
---|
53 | inline void max_epochs(const unsigned long int d) {max_epochs_=d;} |
---|
54 | |
---|
55 | /// |
---|
56 | /// @return output |
---|
57 | /// @todo |
---|
58 | theplu::gslapi::vector output(void) const; |
---|
59 | |
---|
60 | /// |
---|
61 | /// Changing the C-parameter |
---|
62 | /// |
---|
63 | inline void set_c(const double c) {c_ = c;} |
---|
64 | |
---|
65 | /// |
---|
66 | /// Training the SVM following Platt's SMO, with Keerti's |
---|
67 | /// modifacation. However the complete kernel is stored in |
---|
68 | /// memory. The reason for this is speed. When number of samples N |
---|
69 | /// is large this is not possible since the memory cost for the |
---|
70 | /// kernel scales N^2. In that case one should follow the SMO and |
---|
71 | /// calculate the kernel elements sequentially. Minimizing \f$ |
---|
72 | /// \frac{1}{2}\sum |
---|
73 | /// y_iy_j\alpha_i\alpha_j(K_{ij}+\frac{1}{C_i}\delta_{ij}) \f$, |
---|
74 | /// which corresponds to minimizing \f$ \sum w_i^2+\sum |
---|
75 | /// C_i\xi_i^2 \f$ |
---|
76 | /// |
---|
77 | |
---|
78 | bool train(void); |
---|
79 | |
---|
80 | |
---|
81 | private: |
---|
82 | gslapi::vector alpha_; |
---|
83 | double bias_; |
---|
84 | double c_; |
---|
85 | const Kernel_MEV kernel_; |
---|
86 | unsigned long int max_epochs_; |
---|
87 | const gslapi::vector& target_; |
---|
88 | bool trained_; |
---|
89 | std::vector<size_t> train_set_; |
---|
90 | double tolerance_; |
---|
91 | |
---|
92 | /// |
---|
93 | /// Private function choosing which two elements that should be |
---|
94 | /// updated. First checking for the biggest violation (output - target = |
---|
95 | /// 0) among support vectors (alpha!=0). If no violation was found check |
---|
96 | /// for sequentially among the other samples. If no violation there as |
---|
97 | /// well, stop_condition is fullfilled. |
---|
98 | /// |
---|
99 | std::pair<size_t, size_t> choose(const theplu::gslapi::vector&, |
---|
100 | const theplu::gslapi::vector&, |
---|
101 | const theplu::gslapi::vector&, |
---|
102 | bool&); |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | }} // of namespace svm and namespace theplu |
---|
112 | |
---|
113 | #endif |
---|