1 | #ifndef _theplu_yat_classifier_svm_ |
---|
2 | #define _theplu_yat_classifier_svm_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
9 | Copyright (C) 2007 Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 2 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "SVindex.h" |
---|
30 | #include "Target.h" |
---|
31 | #include "yat/utility/Vector.h" |
---|
32 | |
---|
33 | #include <utility> |
---|
34 | #include <vector> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace utility{ |
---|
39 | class Matrix; |
---|
40 | } |
---|
41 | |
---|
42 | namespace classifier { |
---|
43 | |
---|
44 | class DataLookup1D; |
---|
45 | class DataLookupWeighted1D; |
---|
46 | class KernelLookup; |
---|
47 | |
---|
48 | /** |
---|
49 | \brief Support Vector Machine |
---|
50 | */ |
---|
51 | class SVM |
---|
52 | { |
---|
53 | |
---|
54 | public: |
---|
55 | /// |
---|
56 | /// \brief Constructor |
---|
57 | /// |
---|
58 | SVM(void); |
---|
59 | |
---|
60 | /** |
---|
61 | \brief Copy constructor. |
---|
62 | */ |
---|
63 | SVM(const SVM&); |
---|
64 | |
---|
65 | /// |
---|
66 | /// \brief Destructor |
---|
67 | /// |
---|
68 | virtual ~SVM(); |
---|
69 | |
---|
70 | /** |
---|
71 | \brief Create an untrained copy of SVM. |
---|
72 | |
---|
73 | \returns A dynamically allocated SVM, which has to be deleted |
---|
74 | by the caller to avoid memory leaks. |
---|
75 | */ |
---|
76 | SVM* make_classifier(void) const; |
---|
77 | |
---|
78 | /// |
---|
79 | /// @return alpha parameters |
---|
80 | /// |
---|
81 | const utility::Vector& alpha(void) const; |
---|
82 | |
---|
83 | /// |
---|
84 | /// The C-parameter is the balance term (see train()). A very |
---|
85 | /// large C means the training will be focused on getting samples |
---|
86 | /// correctly classified, with risk for overfitting and poor |
---|
87 | /// generalisation. A too small C will result in a training, in |
---|
88 | /// which misclassifications are not penalized. C is weighted with |
---|
89 | /// respect to the size such that \f$ n_+C_+ = n_-C_- \f$, meaning |
---|
90 | /// a misclassificaion of the smaller group is penalized |
---|
91 | /// harder. This balance is equivalent to the one occuring for |
---|
92 | /// %regression with regularisation, or ANN-training with a |
---|
93 | /// weight-decay term. Default is C set to infinity. |
---|
94 | /// |
---|
95 | /// @returns mean of vector \f$ C_i \f$ |
---|
96 | /// |
---|
97 | double C(void) const; |
---|
98 | |
---|
99 | /// |
---|
100 | /// Default is max_epochs set to 100,000. |
---|
101 | /// |
---|
102 | /// @return number of maximal epochs |
---|
103 | /// |
---|
104 | long int max_epochs(void) const; |
---|
105 | |
---|
106 | /** |
---|
107 | \brief set maximal number of epochs in training |
---|
108 | */ |
---|
109 | void max_epochs(long int); |
---|
110 | |
---|
111 | /** |
---|
112 | The output is calculated as \f$ o_i = \sum \alpha_j t_j K_{ij} |
---|
113 | + bias \f$, where \f$ t \f$ is the target. |
---|
114 | |
---|
115 | @return output of training samples |
---|
116 | */ |
---|
117 | const theplu::yat::utility::Vector& output(void) const; |
---|
118 | |
---|
119 | /** |
---|
120 | Generate prediction @a predict from @a input. The prediction |
---|
121 | is calculated as the output times the margin, i.e., geometric |
---|
122 | distance from decision hyperplane: \f$ \frac{ \sum \alpha_j |
---|
123 | t_j K_{ij} + bias}{|w|} \f$ The output has 2 rows. The first row |
---|
124 | is for binary target true, and the second is for binary target |
---|
125 | false. The second row is superfluous as it is the first row |
---|
126 | negated. It exist just to be aligned with multi-class |
---|
127 | SupervisedClassifiers. Each column in @a input and @a output |
---|
128 | corresponds to a sample to predict. Each row in @a input |
---|
129 | corresponds to a training sample, and more exactly row i in @a |
---|
130 | input should correspond to row i in KernelLookup that was used |
---|
131 | for training. |
---|
132 | */ |
---|
133 | void predict(const KernelLookup& input, utility::Matrix& predict) const; |
---|
134 | |
---|
135 | /* |
---|
136 | /// |
---|
137 | /// @return output times margin (i.e. geometric distance from |
---|
138 | /// decision hyperplane) from data @a input |
---|
139 | /// |
---|
140 | double predict(const DataLookup1D& input) const; |
---|
141 | |
---|
142 | /// |
---|
143 | /// @return output times margin from data @a input with |
---|
144 | /// corresponding @a weight |
---|
145 | /// |
---|
146 | double predict(const DataLookupWeighted1D& input) const; |
---|
147 | */ |
---|
148 | |
---|
149 | /// |
---|
150 | /// @brief sets the C-Parameter |
---|
151 | /// |
---|
152 | void set_C(const double); |
---|
153 | |
---|
154 | /** |
---|
155 | Training the SVM following Platt's SMO, with Keerti's |
---|
156 | modifacation. Minimizing \f$ \frac{1}{2}\sum |
---|
157 | y_iy_j\alpha_i\alpha_j(K_{ij}+\frac{1}{C_i}\delta_{ij}) - \sum |
---|
158 | alpha_i\f$ , which corresponds to minimizing \f$ \sum |
---|
159 | w_i^2+\sum C_i\xi_i^2 \f$. |
---|
160 | |
---|
161 | @note If the training problem is not linearly separable and C |
---|
162 | is set to infinity, the minima will be located in the infinity, |
---|
163 | and thus the minimum will not be reached within the maximal |
---|
164 | number of epochs. More exactly, when the problem is not |
---|
165 | linearly separable, there exists an eigenvector to \f$ |
---|
166 | H_{ij}=y_iy_jK_{ij} \f$ within the space defined by the |
---|
167 | conditions: \f$ \alpha_i>0 \f$ and \f$ \sum \alpha_i y_i = 0 |
---|
168 | \f$. As the eigenvalue is zero in this direction the quadratic |
---|
169 | term does not contribute to the objective, but the objective |
---|
170 | only consists of the linear term and hence there is no |
---|
171 | minumum. This problem only occurs when \f$ C \f$ is set to |
---|
172 | infinity because for a finite \f$ C \f$ all eigenvalues are |
---|
173 | finite. However, for a large \f$ C \f$ (and training problem is |
---|
174 | non-linearly separable) there exists an eigenvector |
---|
175 | corresponding to a small eigenvalue, which means the minima has |
---|
176 | moved from infinity to "very far away". In practice this will |
---|
177 | also result in that the minima is not reached withing the |
---|
178 | maximal number of epochs and the of \f$ C \f$ should be |
---|
179 | decreased. |
---|
180 | |
---|
181 | Class for SVM using Keerthi's second modification of Platt's |
---|
182 | Sequential Minimal Optimization. The SVM uses all data given for |
---|
183 | training. |
---|
184 | |
---|
185 | \throw std::runtime_error if maximal number of epoch is reach. |
---|
186 | */ |
---|
187 | void train(const KernelLookup& kernel, const Target& target); |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | private: |
---|
192 | /// |
---|
193 | /// Calculates bounds for alpha2 |
---|
194 | /// |
---|
195 | void bounds(double&, double&) const; |
---|
196 | |
---|
197 | /// |
---|
198 | /// @brief calculates the bias term |
---|
199 | /// |
---|
200 | /// @return true if successful |
---|
201 | /// |
---|
202 | void calculate_bias(void); |
---|
203 | |
---|
204 | /// |
---|
205 | /// Calculate margin that is inverse of w |
---|
206 | /// |
---|
207 | void calculate_margin(void); |
---|
208 | |
---|
209 | /// |
---|
210 | /// Private function choosing which two elements that should be |
---|
211 | /// updated. First checking for the biggest violation (output - target = |
---|
212 | /// 0) among support vectors (alpha!=0). If no violation was found check |
---|
213 | /// sequentially among the other samples. If no violation there as |
---|
214 | /// well training is completed |
---|
215 | /// |
---|
216 | /// @return true if a pair of samples that violate the conditions |
---|
217 | /// can be found |
---|
218 | /// |
---|
219 | bool choose(const theplu::yat::utility::Vector&); |
---|
220 | |
---|
221 | /// |
---|
222 | /// @return kernel modified with diagonal term (soft margin) |
---|
223 | /// |
---|
224 | double kernel_mod(const size_t i, const size_t j) const; |
---|
225 | |
---|
226 | /// |
---|
227 | /// @return 1 if i belong to binary target true else -1 |
---|
228 | /// |
---|
229 | int target(size_t i) const; |
---|
230 | |
---|
231 | utility::Vector alpha_; |
---|
232 | double bias_; |
---|
233 | double C_inverse_; |
---|
234 | // not owned by SVM |
---|
235 | const KernelLookup* kernel_; |
---|
236 | double margin_; |
---|
237 | unsigned long int max_epochs_; |
---|
238 | utility::Vector output_; |
---|
239 | SVindex sample_; |
---|
240 | Target target_; |
---|
241 | double tolerance_; |
---|
242 | bool trained_; |
---|
243 | |
---|
244 | }; |
---|
245 | |
---|
246 | }}} // of namespace classifier, yat, and theplu |
---|
247 | |
---|
248 | #endif |
---|