1 | // $Id: svm_test.cc 307 2005-05-03 13:28:29Z peter $ |
---|
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_SEV.h> |
---|
7 | #include <c++_tools/svm/PolynomialKernelFunction.h> |
---|
8 | |
---|
9 | #include <fstream> |
---|
10 | #include <iostream> |
---|
11 | #include <cstdlib> |
---|
12 | #include <limits.h> |
---|
13 | |
---|
14 | using namespace theplu; |
---|
15 | |
---|
16 | int main() |
---|
17 | |
---|
18 | { |
---|
19 | |
---|
20 | // ifstream is("data/nm_kernel.txt"); |
---|
21 | // theplu::gslapi::matrix kernel_matrix(is); |
---|
22 | // is.close(); |
---|
23 | |
---|
24 | // is.open("data/nm_target_bin.txt"); |
---|
25 | // theplu::gslapi::vector target(is); |
---|
26 | // is.close(); |
---|
27 | |
---|
28 | // is.open("data/nm_alpha_linear_matlab.txt"); |
---|
29 | // theplu::gslapi::vector alpha_matlab(is); |
---|
30 | // is.close(); |
---|
31 | |
---|
32 | // theplu::cpptools::SVM svm(kernel_matrix, target); |
---|
33 | // svm.train(); |
---|
34 | |
---|
35 | // theplu::gslapi::vector alpha = svm.get_alpha(); |
---|
36 | |
---|
37 | // // Comparing alpha to alpha_matlab |
---|
38 | // theplu::gslapi::vector diff_alpha = alpha - alpha_matlab; |
---|
39 | // if (diff_alpha*diff_alpha> pow(10.0,-10)){ |
---|
40 | // cerr << "Difference to matlab alphas too large/n"; |
---|
41 | // return -1; |
---|
42 | // } |
---|
43 | // // Comparing output to target |
---|
44 | // theplu::gslapi::vector output = svm.get_output(); |
---|
45 | // double slack = 0; |
---|
46 | // for (unsigned int i=0; i<target.size(); i++){ |
---|
47 | // if (output[i]*target[i] < 1){ |
---|
48 | // slack += 1 - output[i]*target[i]; |
---|
49 | // } |
---|
50 | // } |
---|
51 | // if (slack > pow(10.0,-10)){ |
---|
52 | // cerr << "Difference to matlab alphas too large/n"; |
---|
53 | // return -1; |
---|
54 | // } |
---|
55 | |
---|
56 | // testing on non solvable problem |
---|
57 | gslapi::matrix data(3,1); |
---|
58 | data(0,0)=-1; |
---|
59 | data(1,0)=10; |
---|
60 | data(2,0)=0; |
---|
61 | |
---|
62 | gslapi::vector target(3,1.0); |
---|
63 | target(2)=-1; |
---|
64 | svm::KernelFunction* kf = |
---|
65 | new svm::PolynomialKernelFunction(); |
---|
66 | svm::Kernel_SEV kernel(data,*kf); |
---|
67 | //theplu::gslapi::matrix m=kernel.get(); |
---|
68 | //std::cout << "kernel:\n" << m << "\n"; |
---|
69 | svm::SVM svm(kernel,target); |
---|
70 | // svm.set_c(1); |
---|
71 | //svm.train(); |
---|
72 | |
---|
73 | return 0; |
---|
74 | |
---|
75 | } |
---|