source: trunk/test/svm_test.cc @ 463

Last change on this file since 463 was 463, checked in by Peter, 16 years ago

fixed bug in KernelView? constructor and updated tests

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1// $Id: svm_test.cc 463 2005-12-16 17:59:15Z peter $
2
3#include <c++_tools/gslapi/matrix.h>
4#include <c++_tools/gslapi/vector.h>
5#include <c++_tools/classifier/SVM.h>
6#include <c++_tools/classifier/Kernel.h>
7#include <c++_tools/classifier/Kernel_SEV.h>
8#include <c++_tools/classifier/Kernel_MEV.h>
9#include <c++_tools/classifier/PolynomialKernelFunction.h>
10
11#include <cassert>
12#include <fstream>
13#include <iostream>
14#include <cstdlib>
15#include <limits>
16
17using namespace theplu;
18
19int main(const int argc,const char* argv[])
20{ 
21
22  std::ostream* error;
23  if (argc>1 && argv[1]==std::string("-v"))
24    error = &std::cerr;
25  else {
26    error = new std::ofstream("/dev/null");
27    if (argc>1)
28      std::cout << "svm_test -v : for printing extra information\n";
29  }
30  *error << "testing svm" << std::endl;
31  bool ok = true;
32
33  gslapi::matrix data2(2,3);
34  data2(0,0)=0;
35  data2(1,0)=0;
36  data2(0,1)=0;
37  data2(1,1)=1;
38  data2(0,2)=1;
39  data2(1,2)=0;
40  gslapi::vector target2(3);
41  target2(0)=-1;
42  target2(1)=1;
43  target2(2)=1;
44  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); 
45  classifier::Kernel_MEV kernel2(data2,*kf2);
46  assert(kernel2.size()==3);
47  assert(target2.size()==3);
48  classifier::KernelView kv2(kernel2);
49  *error << "testing with linear kernel" << std::endl;
50  assert(kv2.rows()==target2.size());
51  classifier::SVM classifier2(kv2, target2);
52  *error << "training...";
53  classifier2.train();
54  *error << " done." << std::endl;
55
56  if (classifier2.alpha()*target2){
57    *error << "condition not fullfilled" << std::endl;
58    return -1;
59  }
60
61  if (classifier2.alpha()(1)!=2 || classifier2.alpha()(2)!=2){
62    *error << "wrong alpha" << std::endl;
63    *error << "alpha: " << classifier2.alpha() <<  std::endl;
64    *error << "expected: 4 2 2" <<  std::endl;
65
66    return -1;
67  }
68
69 
70
71  std::ifstream is("data/nm_data_centralized.txt");
72  gslapi::matrix transposed_data(is);
73  is.close();
74  // Because how the kernel is treated is changed, data must be transposed.
75  gslapi::matrix data=transposed_data;
76
77  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
78  classifier::Kernel_SEV kernel(data,*kf);
79
80
81  is.open("data/nm_target_bin.txt");
82  theplu::gslapi::vector target(is);
83  is.close();
84
85  is.open("data/nm_alpha_linear_matlab.txt");
86  theplu::gslapi::vector alpha_matlab(is);
87  is.close();
88
89  classifier::KernelView kv(kernel);
90  theplu::classifier::SVM svm(kv, target);
91  if (!svm.train()){
92    ok=false;
93    *error << "Training failured" << std::endl;
94  }
95
96  theplu::gslapi::vector alpha = svm.alpha();
97     
98  // Comparing alpha to alpha_matlab
99  theplu::gslapi::vector diff_alpha(alpha);
100  diff_alpha-=alpha_matlab;
101  if (diff_alpha*diff_alpha> 1e-10 ){
102    *error << "Difference to matlab alphas too large\n";
103    ok=false;
104  }
105
106  // Comparing output to target
107  theplu::gslapi::vector output(svm.output());
108  double slack = 0;
109  for (unsigned int i=0; i<target.size(); i++){
110    if (output[i]*target[i] < 1){
111      slack += 1 - output[i]*target[i];
112    }
113  }
114  double slack_bound=2e-7;
115  if (slack > slack_bound){
116    *error << "Slack too large. Is the bias correct?\n";
117    *error << "slack: " << slack << std::endl;
118    *error << "expected less than " << slack_bound << std::endl;
119    ok = false;
120  }
121 
122  delete kf;
123  delete kf2;
124
125  if (error!=&std::cerr)
126    delete error;
127
128  if(ok)
129    return 0;
130  return -1;
131 
132}
Note: See TracBrowser for help on using the repository browser.