source: trunk/test/test_svm.cc @ 127

Last change on this file since 127 was 127, checked in by Peter, 17 years ago

test_vector added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.7 KB
Line 
1// $Id: test_svm.cc 127 2004-08-03 09:36:41Z peter $
2
3// C++ tools include
4////////////////////
5#include "matrix.h"
6#include "vector.h"
7#include "SVM.h"
8#include "Kernel.h"
9#include "PolynomialKernelFunction.h"
10
11// Standard includes
12////////////////////
13#include <fstream>
14#include <iostream>
15#include <cstdlib>
16
17using namespace std;
18
19int main()
20
21{ 
22  ifstream is("data/nm_kernel.txt");
23  theplu::gslapi::matrix kernel_matrix(is);
24  is.close();
25 
26  is.open("data/nm_target_bin.txt");
27  theplu::gslapi::vector target(is);
28  is.close();
29
30  is.open("data/nm_alpha_linear_matlab.txt");
31  theplu::gslapi::vector alpha_matlab(is);
32  is.close();
33
34  theplu::cpptools::SVM svm(kernel_matrix, target);
35  svm.train();
36
37  theplu::gslapi::vector alpha = svm.get_alpha();
38     
39  // Comparing alpha to alpha_matlab
40  theplu::gslapi::vector diff_alpha = alpha - alpha_matlab;
41  if (diff_alpha*diff_alpha> pow(10.0,-10)){
42    cerr << "Difference to matlab alphas too large/n";
43    return -1;
44  }
45  // Comparing output to target
46  theplu::gslapi::vector output = svm.get_output();
47  double slack = 0;
48  for (unsigned int i=0; i<target.size(); i++){
49    if (output[i]*target[i] < 1){
50      slack += 1 - output[i]*target[i];
51    }
52  }
53  if (slack > pow(10.0,-10)){
54    cerr << "Difference to matlab alphas too large/n";
55    return -1;
56  }
57
58  // testing on XOR problem
59  cout << "testing XOR\n";
60  theplu::gslapi::matrix data(4,2);
61  data(0,0)=1;
62  data(0,1)=1;
63 
64  data(1,0)=1;
65  data(1,1)=-1;
66 
67  data(2,0)=-1;
68  data(2,1)=1;
69 
70  data(3,0)=-1;
71  data(3,1)=-1;
72 
73  target = theplu::gslapi::vector(4,1.0);
74  target(1)=-1; 
75  target(2)=-1;
76 
77  theplu::cpptools::KernelFunction* kf =
78    new theplu::cpptools::PolynomialKernelFunction(); 
79  theplu::cpptools::Kernel kernel(data,*kf); 
80  svm = theplu::cpptools::SVM(kernel.get(),target);
81  //  svm.train();
82  cout << kernel.get() << "\n";
83  return 0;
84 
85}
Note: See TracBrowser for help on using the repository browser.