1 | // $Id: svm_test.cc 865 2007-09-10 19:41:04Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
6 | Copyright (C) 2007 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://trac.thep.lu.se/trac/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 2 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "yat/classifier/SVM.h" |
---|
27 | #include "yat/classifier/Kernel.h" |
---|
28 | #include "yat/classifier/KernelLookup.h" |
---|
29 | #include "yat/classifier/Kernel_SEV.h" |
---|
30 | #include "yat/classifier/Kernel_MEV.h" |
---|
31 | #include "yat/classifier/PolynomialKernelFunction.h" |
---|
32 | #include "yat/classifier/Target.h" |
---|
33 | #include "yat/utility/matrix.h" |
---|
34 | #include "yat/utility/vector.h" |
---|
35 | |
---|
36 | #include <cassert> |
---|
37 | #include <fstream> |
---|
38 | #include <iostream> |
---|
39 | #include <cstdlib> |
---|
40 | #include <limits> |
---|
41 | |
---|
42 | using namespace theplu::yat; |
---|
43 | |
---|
44 | int main(const int argc,const char* argv[]) |
---|
45 | { |
---|
46 | |
---|
47 | std::ostream* error; |
---|
48 | if (argc>1 && argv[1]==std::string("-v")) |
---|
49 | error = &std::cerr; |
---|
50 | else { |
---|
51 | error = new std::ofstream("/dev/null"); |
---|
52 | if (argc>1) |
---|
53 | std::cout << "svm_test -v : for printing extra information\n"; |
---|
54 | } |
---|
55 | *error << "testing svm" << std::endl; |
---|
56 | bool ok = true; |
---|
57 | |
---|
58 | utility::matrix data2_core(2,3); |
---|
59 | data2_core(0,0)=0; |
---|
60 | data2_core(1,0)=0; |
---|
61 | data2_core(0,1)=0; |
---|
62 | data2_core(1,1)=1; |
---|
63 | data2_core(0,2)=1; |
---|
64 | data2_core(1,2)=0; |
---|
65 | classifier::MatrixLookup data2(data2_core); |
---|
66 | std::vector<std::string> label; |
---|
67 | label.reserve(3); |
---|
68 | label.push_back("-1"); |
---|
69 | label.push_back("1"); |
---|
70 | label.push_back("1"); |
---|
71 | classifier::Target target2(label); |
---|
72 | classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); |
---|
73 | classifier::Kernel_MEV kernel2(data2,*kf2); |
---|
74 | assert(kernel2.size()==3); |
---|
75 | assert(target2.size()==3); |
---|
76 | for (size_t i=0; i<3; i++){ |
---|
77 | for (size_t j=0; j<3; j++) |
---|
78 | *error << kernel2(i,j) << " "; |
---|
79 | *error << std::endl; |
---|
80 | } |
---|
81 | classifier::KernelLookup kv2(kernel2); |
---|
82 | *error << "testing with linear kernel" << std::endl; |
---|
83 | assert(kv2.rows()==target2.size()); |
---|
84 | classifier::SVM classifier2(kv2, target2); |
---|
85 | *error << "training..."; |
---|
86 | classifier2.train(); |
---|
87 | *error << " done!" << std::endl; |
---|
88 | |
---|
89 | double tmp=0; |
---|
90 | for (size_t i=0; i<target2.size(); i++) |
---|
91 | if (target2.binary(i)) |
---|
92 | tmp += classifier2.alpha()(i); |
---|
93 | else |
---|
94 | tmp -= classifier2.alpha()(i); |
---|
95 | |
---|
96 | if (tmp){ |
---|
97 | *error << "ERROR: found " << tmp << " expected zero" << std::endl; |
---|
98 | return -1; |
---|
99 | } |
---|
100 | |
---|
101 | double tol=1e-6; |
---|
102 | if (fabs(classifier2.alpha()(1)-2)>tol || |
---|
103 | fabs(classifier2.alpha()(2)-2)>tol){ |
---|
104 | *error << "wrong alpha" << std::endl; |
---|
105 | *error << "alpha: " << classifier2.alpha() << std::endl; |
---|
106 | *error << "expected: 4 2 2" << std::endl; |
---|
107 | |
---|
108 | return -1; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | std::ifstream is("data/nm_data_centralized.txt"); |
---|
114 | utility::matrix data_core(is); |
---|
115 | is.close(); |
---|
116 | |
---|
117 | classifier::MatrixLookup data(data_core); |
---|
118 | |
---|
119 | classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); |
---|
120 | classifier::Kernel_SEV kernel(data,*kf); |
---|
121 | |
---|
122 | |
---|
123 | is.open("data/nm_target_bin.txt"); |
---|
124 | classifier::Target target(is); |
---|
125 | is.close(); |
---|
126 | |
---|
127 | is.open("data/nm_alpha_linear_matlab.txt"); |
---|
128 | theplu::yat::utility::vector alpha_matlab(is); |
---|
129 | is.close(); |
---|
130 | |
---|
131 | classifier::KernelLookup kv(kernel); |
---|
132 | theplu::yat::classifier::SVM svm(kv, target); |
---|
133 | if (!svm.train()){ |
---|
134 | ok=false; |
---|
135 | *error << "Training failured" << std::endl; |
---|
136 | } |
---|
137 | |
---|
138 | theplu::yat::utility::vector alpha = svm.alpha(); |
---|
139 | |
---|
140 | // Comparing alpha to alpha_matlab |
---|
141 | theplu::yat::utility::vector diff_alpha(alpha); |
---|
142 | diff_alpha-=alpha_matlab; |
---|
143 | if (diff_alpha*diff_alpha> 1e-10 ){ |
---|
144 | *error << "Difference to matlab alphas too large\n"; |
---|
145 | ok=false; |
---|
146 | } |
---|
147 | |
---|
148 | // Comparing output to target |
---|
149 | theplu::yat::utility::vector output(svm.output()); |
---|
150 | double slack = 0; |
---|
151 | for (unsigned int i=0; i<target.size(); i++){ |
---|
152 | if (output(i)*target(i) < 1){ |
---|
153 | if (target.binary(i)) |
---|
154 | slack += 1 - output(i); |
---|
155 | else |
---|
156 | slack += 1 + output(i); |
---|
157 | } |
---|
158 | } |
---|
159 | double slack_bound=2e-7; |
---|
160 | if (slack > slack_bound || std::isnan(slack)){ |
---|
161 | *error << "Slack too large. Is the bias correct?\n"; |
---|
162 | *error << "slack: " << slack << std::endl; |
---|
163 | *error << "expected less than " << slack_bound << std::endl; |
---|
164 | ok = false; |
---|
165 | } |
---|
166 | |
---|
167 | delete kf; |
---|
168 | delete kf2; |
---|
169 | |
---|
170 | if (error!=&std::cerr) |
---|
171 | delete error; |
---|
172 | |
---|
173 | if(ok) |
---|
174 | return 0; |
---|
175 | return -1; |
---|
176 | |
---|
177 | } |
---|