31 inline double dot(
const double *x1,
const double *x2,
int m)
40 inline double evaluate(
const double *alpha,
const int *y,
const double **x,
int n,
int m)
48 s+= y[i] * y[j] * dot(x[i], x[j], m) * alpha[i] * alpha[j];
54 inline double linear(
const double *x,
const double *w,
double b,
int m)
56 return dot(x,w,m) - b;
60 inline double margin(
const double *w,
int m)
62 return 1.0 / std::sqrt(dot(w,w,m));
70 std::vector<double *> m_x;
87 void computeCache(
int n,
int m,
const std::vector<double *> & x)
92 std::cerr <<
"Cache completed" << std::endl;
95 inline double f(
int i,
int j)
const
97 return dot(m_x[i],m_x[j], m_m);
125 void computeCache(
int n,
int m,
const std::vector<double *> & x)
130 std::cerr <<
"Expected " << n*n*
sizeof(double)/(1024*1024) <<
" Mb for cache" << std::endl;
132 m_k =
new double[n*n];
134 std::cerr <<
"Builindg cache..." << std::endl;
138 m_k[i + j * n] = dot(x[i], x[j], m_m);
140 std::cerr <<
"Cache completed" << std::endl;
143 inline double f(
int i,
int j)
const
145 return m_k[i + j * m_n];
151 template<
class Functor>
161 std::vector<double *>
x;
191 double cache(
int i,
int j)
const {
192 return Functor::f(i,j);
195 double learnedFunc(
int k )
const
199 for( i = 0; i <
n; i++ ) {
201 s += alpha[i]*y[i]*cache(i,k);
208 void updateErrorCache()
210 for(
int i =0;i<
n;++i)
211 e[i] = (alpha[i]>0.0 && alpha[i]<
C) ? ( linear(x[i], w,
bias, m) - y[i] ) : 0.0;
215 int nonBoundLagrangeMultipliers()
const
219 for (
int i = 0; i <
n; i++)
221 if (alpha[i] > 0.0 && alpha[i] <
C)
231 int nonZeroLagrangeMultipliers()
const
235 for (
int i = 0; i <
n; i++)
247 bool takeStep(
int i1,
int i2)
255 double a1 = alpha[i1];
256 double a2 = alpha[i2];
263 L = std::max<double>(0, a2 - a1);
264 H = std::min<double>(
C, C + a2 - a1);
268 L = std::max<double>(0, a2 + a1 -
C);
269 H = std::min<double>(
C, a2 + a1);
280 double e1 = (a1 > 0.0 && a1 <
C) ? e[i1] : ( learnedFunc(i1) - y1 );
281 double e2 = (a2 > 0.0 && a2 <
C) ? e[i2] : ( learnedFunc(i2) - y2 );
283 double k11 = cache(i1,i1);
284 double k22 = cache(i2,i2);
285 double k12 = cache(i1,i2);
286 double eta = k11 + k22 - 2 * k12;
292 double a2_new = a2 + y2 * (e1 - e2) / eta;
294 a2_new = (a2_new >= H) ? H : (a2_new <= L) ? L : a2_new;
303 double a1_new = a1 + s * (a2 - a2_new);
306 double w1 = y1*(a1_new - a1);
307 double w2 = y2*(a2_new - a2);
308 double b1 = e1 + w1*k11 + w2*k12;
309 double b2 = e2 + w1*k12 + w2*k22;
312 bias += 0.5*(b1 + b2);
327 if (a1 > 0.0 && a1 <
C)
329 e[i1] = learnedFunc(i1) - y1;
332 if (a2 > 0.0 && a2 <
C)
334 e[i2] = learnedFunc(i2) - y2;
349 for (
int i = 0; i <
n; i++)
351 if (alpha[i] > 0.0 && alpha[i] <
C && i != i1 && i != i2)
353 e[i] += w1 * cache(i1,i)
357 if (e[i] > e[maximum])
362 if (e[i] < e[minimum])
382 bool examineNonBound(
int i2)
386 for (
int i = 0; i <
n; i++)
388 if (alpha[i1] > 0.0 && alpha[i1] <
C)
390 if (takeStep(i1, i2))
401 bool examineBound(
int i2)
405 for (
int i = 0; i <
n; i++)
407 if (alpha[i1] == 0.0 || alpha[i1] ==
C)
409 if (takeStep(i1, i2))
421 bool examineFirstChoice(
int i2)
426 if (std::abs(e2 - e[minimum]) > std::abs(e2 - e[maximum]))
428 if (takeStep(minimum, i2))
435 if (takeStep(maximum, i2))
445 bool examineExample(
int i2)
448 double a2 = alpha[i2];
454 if((r2 < -
tol && a2 <
C) || (r2 >
tol && a2 > 0.0))
457 if(examineFirstChoice(i2))
460 if(examineNonBound(i2))
498 void insert(
int y,
double *x)
500 this->y.push_back(y);
501 this->x.push_back(x);
502 this->alpha.push_back(0.0);
509 double examineAll =
true;
518 Functor::computeCache(n,m,x);
520 for (
int i = 0; i <
n; i++)
524 if (alpha[i] > 0.0 && alpha[i] <
C)
536 while(numChanged > 0 || examineAll)
543 for(
int i =0;i<
n;++i)
544 numChanged += examineExample(i) ? 1 : 0;
548 for(
int i =0;i<
n;++i)
549 if (alpha[i] > 0 && alpha[i] <
C)
550 numChanged += examineExample(i) ? 1 : 0;
561 for(
int j =0;j<
m;++j)
564 for(
int i =0;i<
n;++i)
565 w[j] += y[i]*alpha[i] * x[i][j];
568 std::cerr << numChanged << std::endl;
int m
number of feature (space size)
Definition: SVM.h:157
dot product
Definition: SVM.h:66
SVM trainer.
Definition: SVM.h:152
std::vector< double > alpha
alpha weights
Definition: SVM.h:164
int n
number of samples
Definition: SVM.h:155
std::vector< double * > x
input patterns
Definition: SVM.h:161
double tol
tollerance
Definition: SVM.h:180
std::vector< int > y
train category {-1,+1}
Definition: SVM.h:167
double epsilon
epsilon
Definition: SVM.h:178
double * w
hyperplane vector [out]
Definition: SVM.h:170
double C
C constant SVM.
Definition: SVM.h:175
double bias
hyperplane bias [out]
Definition: SVM.h:172
una metrica che calcola la cache
Definition: SVM.h:104