1 | // $Id: kernel_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 Jari Häkkinen, 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/utility/matrix.h" |
---|
27 | #include "yat/classifier/KernelFunction.h" |
---|
28 | #include "yat/classifier/PolynomialKernelFunction.h" |
---|
29 | #include "yat/classifier/GaussianKernelFunction.h" |
---|
30 | #include "yat/classifier/KernelLookup.h" |
---|
31 | #include "yat/classifier/Kernel_MEV.h" |
---|
32 | #include "yat/classifier/Kernel_SEV.h" |
---|
33 | |
---|
34 | #include <cassert> |
---|
35 | #include <cmath> |
---|
36 | #include <cstdlib> |
---|
37 | #include <fstream> |
---|
38 | #include <iostream> |
---|
39 | #include <vector> |
---|
40 | |
---|
41 | using namespace theplu::yat; |
---|
42 | |
---|
43 | bool test_MEV(const classifier::MatrixLookup& data, |
---|
44 | const classifier::KernelFunction* kf, |
---|
45 | const utility::matrix& control, const double error_bound, |
---|
46 | std::ostream* error); |
---|
47 | |
---|
48 | bool test_SEV(const classifier::MatrixLookup& data, |
---|
49 | const classifier::KernelFunction* kf, |
---|
50 | const utility::matrix& control, const double error_bound, |
---|
51 | std::ostream* error); |
---|
52 | |
---|
53 | |
---|
54 | int main(const int argc,const char* argv[]) |
---|
55 | |
---|
56 | { |
---|
57 | std::ostream* error; |
---|
58 | if (argc>1 && argv[1]==std::string("-v")) |
---|
59 | error = &std::cerr; |
---|
60 | else { |
---|
61 | error = new std::ofstream("/dev/null"); |
---|
62 | if (argc>1) |
---|
63 | std::cout << "kernel_test -v : for printing extra information\n"; |
---|
64 | } |
---|
65 | *error << "testing kernels" << std::endl; |
---|
66 | bool ok = true; |
---|
67 | |
---|
68 | utility::matrix data2_core(2,3); |
---|
69 | data2_core(0,0)=0; |
---|
70 | data2_core(1,0)=0; |
---|
71 | data2_core(0,1)=0; |
---|
72 | data2_core(1,1)=1; |
---|
73 | data2_core(0,2)=1; |
---|
74 | data2_core(1,2)=0; |
---|
75 | classifier::MatrixLookup data2(data2_core); |
---|
76 | classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); |
---|
77 | classifier::Kernel_SEV kernel2(data2,*kf2); |
---|
78 | assert(kernel2.size()==3); |
---|
79 | if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || |
---|
80 | kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || |
---|
81 | kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){ |
---|
82 | ok = false; |
---|
83 | *error << "Found:\n"; |
---|
84 | for (size_t i=0; i<3; i++){ |
---|
85 | for (size_t j=0; j<3; j++) |
---|
86 | *error << kernel2(i,j) << " "; |
---|
87 | *error << std::endl; |
---|
88 | } |
---|
89 | *error << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl; |
---|
90 | } |
---|
91 | classifier::KernelLookup kv2(kernel2); |
---|
92 | delete kf2; |
---|
93 | |
---|
94 | // Peter, the hardcoded number below should be changed. |
---|
95 | double error_bound = 1e-8; |
---|
96 | std::ifstream is("data/nm_data_centralized.txt"); |
---|
97 | utility::matrix data_core(is); |
---|
98 | is.close(); |
---|
99 | |
---|
100 | classifier::MatrixLookup data(data_core); |
---|
101 | |
---|
102 | is.open("data/nm_kernel.txt"); |
---|
103 | utility::matrix kernel_matlab(is); |
---|
104 | is.close(); |
---|
105 | classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); |
---|
106 | ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error) |
---|
107 | & test_SEV(data,kf,kernel_matlab,error_bound, error)); |
---|
108 | delete kf; |
---|
109 | |
---|
110 | is.open("data/nm_kernel2.txt"); |
---|
111 | utility::matrix kernel_matlab2(is); |
---|
112 | is.close(); |
---|
113 | kf = new classifier::PolynomialKernelFunction(2); |
---|
114 | ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error) |
---|
115 | && test_SEV(data,kf,kernel_matlab2,error_bound, error)); |
---|
116 | delete kf; |
---|
117 | |
---|
118 | // Checking that GaussianKernelFunction. |
---|
119 | *error << "Checking GaussianKernelFunction.\n"; |
---|
120 | is.open("data/nm_kernel_gaussian.txt"); |
---|
121 | utility::matrix kernel_gaussian(is); |
---|
122 | is.close(); |
---|
123 | kf = new classifier::GaussianKernelFunction(100); |
---|
124 | ok = ok && test_MEV(data,kf,kernel_gaussian,error_bound, error); |
---|
125 | ok = ok && test_SEV(data,kf,kernel_gaussian,error_bound, error); |
---|
126 | delete kf; |
---|
127 | |
---|
128 | data_core.clone(utility::matrix(1,5)); |
---|
129 | for (size_t i=0; i<data_core.columns(); i++) |
---|
130 | data_core(0,i)=i; |
---|
131 | data = classifier::MatrixLookup(data_core); |
---|
132 | classifier::PolynomialKernelFunction pkf; |
---|
133 | classifier::Kernel_SEV kernel(data,pkf); |
---|
134 | |
---|
135 | |
---|
136 | if (error!=&std::cerr) |
---|
137 | delete error; |
---|
138 | |
---|
139 | if (ok=true) |
---|
140 | return 0; |
---|
141 | return -1; |
---|
142 | } |
---|
143 | |
---|
144 | bool test_MEV(const classifier::MatrixLookup& data, |
---|
145 | const classifier::KernelFunction* kf, |
---|
146 | const utility::matrix& control, const double error_bound, |
---|
147 | std::ostream* error) |
---|
148 | { |
---|
149 | *error << "testing Kernel_MEV" << std::endl; |
---|
150 | classifier::Kernel_MEV kernel(data,*kf); |
---|
151 | for(u_int i=0;i<control.rows();i++) |
---|
152 | for(u_int j=0;j<control.columns();j++) |
---|
153 | if (fabs(kernel(i,j)-control(i,j))>error_bound) |
---|
154 | return false; |
---|
155 | |
---|
156 | // checking view |
---|
157 | std::vector<size_t> index(3); |
---|
158 | index[0]=1; |
---|
159 | index[1]=2; |
---|
160 | index[2]=3; |
---|
161 | classifier::KernelLookup kv_raw(kernel); |
---|
162 | classifier::KernelLookup kv(kv_raw,index,index); |
---|
163 | if (kv.rows()!=index.size()){ |
---|
164 | *error << "Error: KernelLookup(kernel, index)\n" << std::endl |
---|
165 | << "Size of KernelLookup is " << kv.rows() << std::endl |
---|
166 | << "expected " << index.size() << std::endl; |
---|
167 | |
---|
168 | return false; |
---|
169 | } |
---|
170 | classifier::KernelLookup kv2(kernel); |
---|
171 | if (kv2.rows()!=kernel.size()){ |
---|
172 | *error << "Error: KernelLookup(kernel)\n" << std::endl |
---|
173 | << "Size of KernelLookup is " << kv.rows() << std::endl |
---|
174 | << "expected " << kernel.size() << std::endl; |
---|
175 | |
---|
176 | return false; |
---|
177 | } |
---|
178 | |
---|
179 | return true; |
---|
180 | } |
---|
181 | |
---|
182 | bool test_SEV(const classifier::MatrixLookup& data, |
---|
183 | const classifier::KernelFunction* kf, |
---|
184 | const utility::matrix& control, const double error_bound, |
---|
185 | std::ostream* error) |
---|
186 | { |
---|
187 | *error << "testing Kernel_SEV" << std::endl; |
---|
188 | classifier::Kernel_SEV kernel(data,*kf); |
---|
189 | for(u_int i=0;i<control.rows();i++) |
---|
190 | for(u_int j=0;j<control.columns();j++) |
---|
191 | if (fabs(kernel(i,j)-control(i,j))>error_bound) |
---|
192 | return false; |
---|
193 | |
---|
194 | // checking view |
---|
195 | std::vector<size_t> index(3); |
---|
196 | index[0]=1; |
---|
197 | index[1]=2; |
---|
198 | index[2]=3; |
---|
199 | classifier::KernelLookup kv_raw(kernel); |
---|
200 | classifier::KernelLookup kv(kv_raw,index, index); |
---|
201 | if (kv.rows()!=index.size()){ |
---|
202 | *error << "Error: KernelLookup(kernel, index)\n" << std::endl |
---|
203 | << "Size of KernelLookup is " << kv.rows() << std::endl |
---|
204 | << "expected " << index.size() << std::endl; |
---|
205 | |
---|
206 | return false; |
---|
207 | } |
---|
208 | classifier::KernelLookup kv2(kernel); |
---|
209 | if (kv2.rows()!=kernel.size()){ |
---|
210 | *error << "Error: KernelLookup(kernel)\n" << std::endl |
---|
211 | << "Size of KernelLookup is " << kv.rows() << std::endl |
---|
212 | << "expected " << kernel.size() << std::endl; |
---|
213 | |
---|
214 | return false; |
---|
215 | } |
---|
216 | return true; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|