source: branches/better_matrix_class/test/svm_test.cc @ 415

Last change on this file since 415 was 415, checked in by Jari Häkkinen, 17 years ago

Removed some gslapi copy intensive operators.
Made gslapi assignment operators ignore views, and change them
into normal vectors if assigned.
Cleaning up of code aswell.

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