1 | // $Id: SVM.cc 30 2004-01-16 17:40:51Z peter $ |
---|
2 | |
---|
3 | #ifndef _THEP_SVM_ |
---|
4 | #define _THEP_SVM_ |
---|
5 | |
---|
6 | // System includes |
---|
7 | #include <math.h> |
---|
8 | |
---|
9 | // Thep C++ Tools |
---|
10 | #include "SVM.h" |
---|
11 | #include "matrix.h" |
---|
12 | #include "vector.h" |
---|
13 | #include "random_singleton.h" |
---|
14 | |
---|
15 | using namespace thep_cpp_tools; |
---|
16 | using namespace std; |
---|
17 | |
---|
18 | SVM::SVM(const thep_gsl_api::matrix& kernel, |
---|
19 | const thep_gsl_api::vector& target) : trained_(false), |
---|
20 | kernel_(kernel), |
---|
21 | target_(target), |
---|
22 | alpha_(target.size(),true,true) |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | void SVM::train() //Should be done so one can choose to not train on all the samples |
---|
27 | { |
---|
28 | thep_gsl_api::vector E = thep_gsl_api::vector(-target_); |
---|
29 | |
---|
30 | double upper_bound = pow(10., 32); |
---|
31 | u_int count = 0; |
---|
32 | double alpha_new; |
---|
33 | random_singleton* rnd=random_singleton::get_instance(); |
---|
34 | double u; |
---|
35 | double v; |
---|
36 | |
---|
37 | // Stop criteria |
---|
38 | ofstream myout("alpha.tst"); |
---|
39 | double tmp; |
---|
40 | thep_gsl_api::vector dalpha; |
---|
41 | thep_gsl_api::vector one(E.size(),true,true); |
---|
42 | one.set_all(1); |
---|
43 | while (count<10000) |
---|
44 | { |
---|
45 | count++; |
---|
46 | dalpha = alpha_.mul_elements(target_); |
---|
47 | E=kernel_*dalpha-target_; //should be done in another way!! |
---|
48 | tmp=dalpha*(kernel_*dalpha)/2; |
---|
49 | tmp=tmp-alpha_*one; |
---|
50 | myout << tmp << '\n'; |
---|
51 | // Choosing a pair of variables to modify (Should be done more clever) |
---|
52 | u_long index1 = rnd->get_uniform_int(kernel_.cols()); |
---|
53 | u_long index2 = rnd->get_uniform_int(kernel_.cols()-1); |
---|
54 | if (index2 >= index1) |
---|
55 | index2++; |
---|
56 | |
---|
57 | //Updating the two variables |
---|
58 | if (target_[index1]!=target_[index2]) |
---|
59 | { |
---|
60 | if (alpha_[index2] > alpha_[index1] ) |
---|
61 | { |
---|
62 | v = upper_bound; |
---|
63 | u = alpha_[index2] - alpha_[index1]; |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | v = upper_bound - alpha_[index1] + alpha_[index2]; |
---|
68 | u = 0; |
---|
69 | } |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | if (alpha_[index2] + alpha_[index1] > upper_bound) |
---|
74 | { |
---|
75 | u = alpha_[index2] + alpha_[index1] - upper_bound; |
---|
76 | v = upper_bound; |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | u = 0; |
---|
81 | v = alpha_[index1] + alpha_[index2]; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | double k = kernel_.get(index1, index1) + kernel_.get(index2, index2) - |
---|
86 | 2*kernel_.get(index1, index2); |
---|
87 | alpha_new = alpha_[index2] + target_[index2]* |
---|
88 | (E[index1]-E[index2])/k; |
---|
89 | if (alpha_new > v) |
---|
90 | alpha_new = v; |
---|
91 | else if (alpha_new<u) |
---|
92 | alpha_new = u; |
---|
93 | |
---|
94 | alpha_[index1]+=target_[index1]*target_[index2]*(alpha_[index2]-alpha_new); |
---|
95 | alpha_[index2]=alpha_new; |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | myout.close(); |
---|
105 | |
---|
106 | //thep_gsl_api::vector output = kernel_*dalpha; |
---|
107 | //for (int i=0; i<64; i++){ |
---|
108 | //cout << output.get(i) << '\t'; |
---|
109 | //cout << alpha_.get(i) << endl; |
---|
110 | //} |
---|
111 | |
---|
112 | trained_= true; |
---|
113 | } |
---|
114 | |
---|
115 | #endif |
---|