source: trunk/test/test_svm.cc @ 116

Last change on this file since 116 was 116, checked in by Peter, 18 years ago

test_crossvalidation and test_statistics added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.2 KB
Line 
1// $Id: test_svm.cc 116 2004-07-19 14:40:11Z 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(is);
24  is.close();
25  theplu::gslapi::matrix k(kernel);
26 
27  is.open("data/nm_target_bin.txt");
28  theplu::gslapi::vector target(is);
29  is.close();
30
31  is.open("data/nm_alpha_linear_matlab.txt");
32  theplu::gslapi::vector alpha_matlab(is);
33  is.close();
34
35
36  theplu::cpptools::SVM svm(kernel, target);
37  svm.train();
38
39  theplu::gslapi::vector alpha = svm.get_alpha();
40     
41  // Comparing alpha to alpha_matlab
42  theplu::gslapi::vector diff_alpha = alpha - alpha_matlab;
43  if (diff_alpha*diff_alpha> pow(10.0,-10)){
44    cerr << "Difference to matlab alphas too large/n";
45    return -1;
46  }
47  // Comparing output to target
48  theplu::gslapi::vector output = svm.get_output();
49  double slack = 0;
50  for (unsigned int i=0; i<target.size(); i++){
51    if (output[i]*target[i] < 1){
52      slack += 1 - output[i]*target[i];
53    }
54  }
55  if (slack > pow(10.0,-10)){
56    cerr << "Difference to matlab alphas too large/n";
57    return -1;
58  }
59  return 0;
60 
61}
Note: See TracBrowser for help on using the repository browser.