SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVRLight.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #ifdef USE_SVMLIGHT
14 
15 #include <shogun/io/SGIO.h>
17 #include <shogun/lib/Signal.h>
23 
24 #include <unistd.h>
25 
26 #ifdef USE_CPLEX
27 extern "C" {
28 #include <ilcplex/cplex.h>
29 }
30 #endif
31 
32 #include <shogun/base/Parallel.h>
33 
34 #ifdef HAVE_PTHREAD
35 #include <pthread.h>
36 #endif
37 
38 using namespace shogun;
39 
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 struct S_THREAD_PARAM
42 {
43  float64_t* lin;
44  int32_t start, end;
45  int32_t* active2dnum;
46  int32_t* docs;
47  CKernel* kernel;
48  int32_t num_vectors;
49 };
50 #endif // DOXYGEN_SHOULD_SKIP_THIS
51 
53 : CSVMLight(C, k, lab)
54 {
55  set_tube_epsilon(eps);
56 }
57 
59 : CSVMLight()
60 {
61 }
62 
65 {
66 }
67 
69 {
70  return CT_SVRLIGHT;
71 }
72 
74 {
75  //certain setup params
76  verbosity=1;
77  init_margin=0.15;
78  init_iter=500;
81 
82  strcpy (learn_parm->predfile, "");
83  learn_parm->biased_hyperplane=1;
84  learn_parm->sharedslack=0;
85  learn_parm->remove_inconsistent=0;
86  learn_parm->skip_final_opt_check=1;
87  learn_parm->svm_maxqpsize=get_qpsize();
88  learn_parm->svm_newvarsinqp=learn_parm->svm_maxqpsize-1;
89  learn_parm->maxiter=100000;
90  learn_parm->svm_iter_to_shrink=100;
91  learn_parm->svm_c=get_C1();
92  learn_parm->transduction_posratio=0.33;
93  learn_parm->svm_costratio=get_C2()/get_C1();
94  learn_parm->svm_costratio_unlab=1.0;
95  learn_parm->svm_unlabbound=1E-5;
96  learn_parm->epsilon_crit=epsilon; // GU: better decrease it ... ??
97  learn_parm->epsilon_a=1E-15;
98  learn_parm->compute_loo=0;
99  learn_parm->rho=1.0;
100  learn_parm->xa_depth=0;
101 
102  if (!kernel)
103  {
104  SG_ERROR( "SVR_light can not proceed without kernel!\n");
105  return false ;
106  }
107 
108  if (!m_labels)
109  {
110  SG_ERROR( "SVR_light can not proceed without labels!\n");
111  return false;
112  }
113 
114  if (data)
115  {
116  if (m_labels->get_num_labels() != data->get_num_vectors())
117  SG_ERROR("Number of training vectors does not match number of labels\n");
118  kernel->init(data, data);
119  }
120 
122  kernel->clear_normal();
123 
124  // output some info
125  SG_DEBUG( "qpsize = %i\n", learn_parm->svm_maxqpsize) ;
126  SG_DEBUG( "epsilon = %1.1e\n", learn_parm->epsilon_crit) ;
127  SG_DEBUG( "kernel->has_property(KP_LINADD) = %i\n", kernel->has_property(KP_LINADD)) ;
128  SG_DEBUG( "kernel->has_property(KP_KERNCOMBINATION) = %i\n", kernel->has_property(KP_KERNCOMBINATION)) ;
129  SG_DEBUG( "get_linadd_enabled() = %i\n", get_linadd_enabled()) ;
130  SG_DEBUG( "kernel->get_num_subkernels() = %i\n", kernel->get_num_subkernels()) ;
131 
134 
135  SG_DEBUG( "use_kernel_cache = %i\n", use_kernel_cache) ;
136 
137  // train the svm
138  svr_learn();
139 
140  // brain damaged svm light work around
141  create_new_model(model->sv_num-1);
142  set_bias(-model->b);
143  for (int32_t i=0; i<model->sv_num-1; i++)
144  {
145  set_alpha(i, model->alpha[i+1]);
146  set_support_vector(i, model->supvec[i+1]);
147  }
148 
150  kernel->clear_normal() ;
151 
152  return true ;
153 }
154 
156 {
157  int32_t *inconsistent, i, j;
158  int32_t upsupvecnum;
159  float64_t maxdiff, *lin, *c, *a;
160  int32_t iterations;
161  float64_t *xi_fullset; /* buffer for storing xi on full sample in loo */
162  float64_t *a_fullset; /* buffer for storing alpha on full sample in loo */
163  TIMING timing_profile;
164  SHRINK_STATE shrink_state;
165  int32_t* label;
166  int32_t* docs;
167 
168  ASSERT(m_labels);
169  int32_t totdoc=m_labels->get_num_labels();
170  num_vectors=totdoc;
171 
172  // set up regression problem in standard form
173  docs=SG_MALLOC(int32_t, 2*totdoc);
174  label=SG_MALLOC(int32_t, 2*totdoc);
175  c = SG_MALLOC(float64_t, 2*totdoc);
176 
177  for(i=0;i<totdoc;i++) {
178  docs[i]=i;
179  j=2*totdoc-1-i;
180  label[i]=+1;
181  c[i]=((CRegressionLabels*) m_labels)->get_label(i);
182  docs[j]=j;
183  label[j]=-1;
184  c[j]=((CRegressionLabels*) m_labels)->get_label(i);
185  }
186  totdoc*=2;
187 
188  //prepare kernel cache for regression (i.e. cachelines are twice of current size)
190 
192  {
194  CKernel* kn = k->get_first_kernel();
195 
196  while (kn)
197  {
199  SG_UNREF(kn);
200  kn = k->get_next_kernel();
201  }
202  }
203 
204  timing_profile.time_kernel=0;
205  timing_profile.time_opti=0;
206  timing_profile.time_shrink=0;
207  timing_profile.time_update=0;
208  timing_profile.time_model=0;
209  timing_profile.time_check=0;
210  timing_profile.time_select=0;
211 
212  SG_FREE(W);
213  W=NULL;
214 
216  {
218  for (i=0; i<totdoc*kernel->get_num_subkernels(); i++)
219  W[i]=0;
220  }
221 
222  /* make sure -n value is reasonable */
223  if((learn_parm->svm_newvarsinqp < 2)
224  || (learn_parm->svm_newvarsinqp > learn_parm->svm_maxqpsize)) {
225  learn_parm->svm_newvarsinqp=learn_parm->svm_maxqpsize;
226  }
227 
228  init_shrink_state(&shrink_state,totdoc,(int32_t)MAXSHRINK);
229 
230  inconsistent = SG_MALLOC(int32_t, totdoc);
231  a = SG_MALLOC(float64_t, totdoc);
232  a_fullset = SG_MALLOC(float64_t, totdoc);
233  xi_fullset = SG_MALLOC(float64_t, totdoc);
234  lin = SG_MALLOC(float64_t, totdoc);
235  learn_parm->svm_cost = SG_MALLOC(float64_t, totdoc);
236  if (m_linear_term.vlen>0)
238  else
239  {
240  learn_parm->eps=SG_MALLOC(float64_t, totdoc); /* equivalent regression epsilon for classification */
242  }
243 
244  SG_FREE(model->supvec);
245  SG_FREE(model->alpha);
246  SG_FREE(model->index);
247  model->supvec = SG_MALLOC(int32_t, totdoc+2);
248  model->alpha = SG_MALLOC(float64_t, totdoc+2);
249  model->index = SG_MALLOC(int32_t, totdoc+2);
250 
251  model->at_upper_bound=0;
252  model->b=0;
253  model->supvec[0]=0; /* element 0 reserved and empty for now */
254  model->alpha[0]=0;
255  model->totdoc=totdoc;
256 
257  model->kernel=kernel;
258 
259  model->sv_num=1;
260  model->loo_error=-1;
261  model->loo_recall=-1;
262  model->loo_precision=-1;
263  model->xa_error=-1;
264  model->xa_recall=-1;
265  model->xa_precision=-1;
266 
267  for(i=0;i<totdoc;i++) { /* various inits */
268  inconsistent[i]=0;
269  a[i]=0;
270  lin[i]=0;
271 
272  if(label[i] > 0) {
273  learn_parm->svm_cost[i]=learn_parm->svm_c*learn_parm->svm_costratio*
274  fabs((float64_t)label[i]);
275  }
276  else if(label[i] < 0) {
277  learn_parm->svm_cost[i]=learn_parm->svm_c*fabs((float64_t)label[i]);
278  }
279  else
280  ASSERT(false);
281  }
282 
283  if(verbosity==1) {
284  SG_DEBUG( "Optimizing...\n");
285  }
286 
287  /* train the svm */
288  SG_DEBUG( "num_train: %d\n", totdoc);
289  iterations=optimize_to_convergence(docs,label,totdoc,
290  &shrink_state,inconsistent,a,lin,
291  c,&timing_profile,
292  &maxdiff,(int32_t)-1,
293  (int32_t)1);
294 
295 
296  if(verbosity>=1) {
297  SG_DONE();
298  SG_INFO("(%ld iterations)\n",iterations);
299  SG_INFO( "Optimization finished (maxdiff=%.8f).\n",maxdiff);
300  SG_INFO( "obj = %.16f, rho = %.16f\n",get_objective(),model->b);
301 
302  upsupvecnum=0;
303 
304  SG_DEBUG( "num sv: %d\n", model->sv_num);
305  for(i=1;i<model->sv_num;i++)
306  {
307  if(fabs(model->alpha[i]) >=
308  (learn_parm->svm_cost[model->supvec[i]]-
309  learn_parm->epsilon_a))
310  upsupvecnum++;
311  }
312  SG_INFO( "Number of SV: %ld (including %ld at upper bound)\n",
313  model->sv_num-1,upsupvecnum);
314  }
315 
316  /* this makes sure the model we return does not contain pointers to the
317  temporary documents */
318  for(i=1;i<model->sv_num;i++) {
319  j=model->supvec[i];
320  if(j >= (totdoc/2)) {
321  j=totdoc-j-1;
322  }
323  model->supvec[i]=j;
324  }
325 
326  shrink_state_cleanup(&shrink_state);
327  SG_FREE(label);
328  SG_FREE(inconsistent);
329  SG_FREE(c);
330  SG_FREE(a);
331  SG_FREE(a_fullset);
332  SG_FREE(xi_fullset);
333  SG_FREE(lin);
334  SG_FREE(learn_parm->svm_cost);
335  SG_FREE(docs);
336 }
337 
339  float64_t *a, float64_t *lin, float64_t *c, float64_t* eps, int32_t *label,
340  int32_t totdoc)
341 {
342  /* calculate value of objective function */
343  float64_t criterion=0;
344 
345  for(int32_t i=0;i<totdoc;i++)
346  criterion+=(eps[i]-(float64_t)label[i]*c[i])*a[i]+0.5*a[i]*label[i]*lin[i];
347 
348  /* float64_t check=0;
349  for(int32_t i=0;i<totdoc;i++)
350  {
351  check+=a[i]*eps-a[i]*label[i]*c[i];
352  for(int32_t j=0;j<totdoc;j++)
353  check+= 0.5*a[i]*label[i]*a[j]*label[j]*compute_kernel(i,j);
354 
355  }
356 
357  SG_INFO("REGRESSION OBJECTIVE %f vs. CHECK %f (diff %f)\n", criterion, check, criterion-check); */
358 
359  return(criterion);
360 }
361 
363 {
364  S_THREAD_PARAM * params = (S_THREAD_PARAM*) params_ ;
365 
366  int32_t jj=0, j=0 ;
367 
368  for(jj=params->start;(jj<params->end) && (j=params->active2dnum[jj])>=0;jj++)
369  params->lin[j]+=params->kernel->compute_optimized(CSVRLight::regression_fix_index2(params->docs[j], params->num_vectors));
370 
371  return NULL ;
372 }
373 
375 {
376  if (i>=num_vectors)
377  i=2*num_vectors-1-i;
378 
379  return i;
380 }
381 
383  int32_t i, int32_t num_vectors)
384 {
385  if (i>=num_vectors)
386  i=2*num_vectors-1-i;
387 
388  return i;
389 }
390 
392 {
395  return kernel->kernel(i, j);
396 }
397 
399  int32_t* docs, int32_t* label, int32_t *active2dnum, float64_t *a,
400  float64_t *a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
401  float64_t *aicache, float64_t* c)
402  /* keep track of the linear component */
403  /* lin of the gradient etc. by updating */
404  /* based on the change of the variables */
405  /* in the current working set */
406 {
407  register int32_t i=0,ii=0,j=0,jj=0;
408 
410  {
411  if (callback)
412  {
413  update_linear_component_mkl_linadd(docs, label, active2dnum, a, a_old, working2dnum,
414  totdoc, lin, aicache, c) ;
415  }
416  else
417  {
418  kernel->clear_normal();
419 
420  int32_t num_working=0;
421  for(ii=0;(i=working2dnum[ii])>=0;ii++) {
422  if(a[i] != a_old[i]) {
423  kernel->add_to_normal(regression_fix_index(docs[i]), (a[i]-a_old[i])*(float64_t)label[i]);
424  num_working++;
425  }
426  }
427 
428  if (num_working>0)
429  {
430  if (parallel->get_num_threads() < 2)
431  {
432  for(jj=0;(j=active2dnum[jj])>=0;jj++) {
433  lin[j]+=kernel->compute_optimized(regression_fix_index(docs[j]));
434  }
435  }
436 #ifdef HAVE_PTHREAD
437  else
438  {
439  int32_t num_elem = 0 ;
440  for(jj=0;(j=active2dnum[jj])>=0;jj++) num_elem++ ;
441 
442  pthread_t* threads = SG_MALLOC(pthread_t, parallel->get_num_threads()-1);
443  S_THREAD_PARAM* params = SG_MALLOC(S_THREAD_PARAM, parallel->get_num_threads()-1);
444  int32_t start = 0 ;
445  int32_t step = num_elem/parallel->get_num_threads() ;
446  int32_t end = step ;
447 
448  for (int32_t t=0; t<parallel->get_num_threads()-1; t++)
449  {
450  params[t].kernel = kernel ;
451  params[t].lin = lin ;
452  params[t].docs = docs ;
453  params[t].active2dnum=active2dnum ;
454  params[t].start = start ;
455  params[t].end = end ;
456  params[t].num_vectors=num_vectors ;
457 
458  start=end ;
459  end+=step ;
460  pthread_create(&threads[t], NULL, update_linear_component_linadd_helper, (void*)&params[t]) ;
461  }
462 
463  for(jj=params[parallel->get_num_threads()-2].end;(j=active2dnum[jj])>=0;jj++) {
464  lin[j]+=kernel->compute_optimized(regression_fix_index(docs[j]));
465  }
466  void* ret;
467  for (int32_t t=0; t<parallel->get_num_threads()-1; t++)
468  pthread_join(threads[t], &ret) ;
469 
470  SG_FREE(params);
471  SG_FREE(threads);
472  }
473 #endif
474  }
475  }
476  }
477  else
478  {
479  if (callback)
480  {
481  update_linear_component_mkl(docs, label, active2dnum,
482  a, a_old, working2dnum, totdoc, lin, aicache, c) ;
483  }
484  else {
485  for(jj=0;(i=working2dnum[jj])>=0;jj++) {
486  if(a[i] != a_old[i]) {
487  kernel->get_kernel_row(i,active2dnum,aicache);
488  for(ii=0;(j=active2dnum[ii])>=0;ii++)
489  lin[j]+=(a[i]-a_old[i])*aicache[j]*(float64_t)label[i];
490  }
491  }
492  }
493  }
494 }
495 
497  int32_t* docs, int32_t* label, int32_t *active2dnum, float64_t *a,
498  float64_t *a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
499  float64_t *aicache, float64_t* c)
500 {
501  int32_t num = totdoc;
502  int32_t num_weights = -1;
503  int32_t num_kernels = kernel->get_num_subkernels() ;
504  const float64_t* old_beta = kernel->get_subkernel_weights(num_weights);
505 
506  ASSERT(num_weights==num_kernels);
507 
508  if ((kernel->get_kernel_type()==K_COMBINED) &&
509  (!((CCombinedKernel*)kernel)->get_append_subkernel_weights()))// for combined kernel
510  {
512  CKernel* kn = k->get_first_kernel() ;
513  int32_t n = 0, i, j ;
514 
515  while (kn!=NULL)
516  {
517  for(i=0;i<num;i++)
518  {
519  if(a[i] != a_old[i])
520  {
521  kn->get_kernel_row(i,NULL,aicache, true);
522  for(j=0;j<num;j++)
523  W[j*num_kernels+n]+=(a[i]-a_old[i])*aicache[regression_fix_index(j)]*(float64_t)label[i];
524  }
525  }
526  SG_UNREF(kn);
527  kn = k->get_next_kernel();
528  n++ ;
529  }
530  }
531  else // hope the kernel is fast ...
532  {
533  float64_t* w_backup = SG_MALLOC(float64_t, num_kernels);
534  float64_t* w1 = SG_MALLOC(float64_t, num_kernels);
535 
536  // backup and set to zero
537  for (int32_t i=0; i<num_kernels; i++)
538  {
539  w_backup[i] = old_beta[i] ;
540  w1[i]=0.0 ;
541  }
542  for (int32_t n=0; n<num_kernels; n++)
543  {
544  w1[n]=1.0 ;
546 
547  for(int32_t i=0;i<num;i++)
548  {
549  if(a[i] != a_old[i])
550  {
551  for(int32_t j=0;j<num;j++)
552  W[j*num_kernels+n]+=(a[i]-a_old[i])*compute_kernel(i,j)*(float64_t)label[i];
553  }
554  }
555  w1[n]=0.0 ;
556  }
557 
558  // restore old weights
559  kernel->set_subkernel_weights(SGVector<float64_t>(w_backup,num_weights));
560 
561  SG_FREE(w_backup);
562  SG_FREE(w1);
563  }
564 
565  call_mkl_callback(a, label, lin, c, totdoc);
566 }
567 
568 
570  int32_t* docs, int32_t* label, int32_t *active2dnum, float64_t *a,
571  float64_t *a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin,
572  float64_t *aicache, float64_t* c)
573 {
574  // kernel with LP_LINADD property is assumed to have
575  // compute_by_subkernel functions
576  int32_t num_weights = -1;
577  int32_t num_kernels = kernel->get_num_subkernels() ;
578  const float64_t* old_beta = kernel->get_subkernel_weights(num_weights);
579 
580  ASSERT(num_weights==num_kernels);
581 
582  float64_t* w_backup=SG_MALLOC(float64_t, num_kernels);
583  float64_t* w1=SG_MALLOC(float64_t, num_kernels);
584 
585  // backup and set to one
586  for (int32_t i=0; i<num_kernels; i++)
587  {
588  w_backup[i] = old_beta[i] ;
589  w1[i]=1.0 ;
590  }
591  // set the kernel weights
593 
594  // create normal update (with changed alphas only)
595  kernel->clear_normal();
596  for(int32_t ii=0, i=0;(i=working2dnum[ii])>=0;ii++) {
597  if(a[i] != a_old[i]) {
598  kernel->add_to_normal(regression_fix_index(docs[i]), (a[i]-a_old[i])*(float64_t)label[i]);
599  }
600  }
601 
602  // determine contributions of different kernels
603  for (int32_t i=0; i<num_vectors; i++)
604  kernel->compute_by_subkernel(i,&W[i*num_kernels]) ;
605 
606  // restore old weights
607  kernel->set_subkernel_weights(SGVector<float64_t>(w_backup,num_weights));
608 
609  call_mkl_callback(a, label, lin, c, totdoc);
610 }
611 
612 void CSVRLight::call_mkl_callback(float64_t* a, int32_t* label, float64_t* lin, float64_t* c, int32_t totdoc)
613 {
614  int32_t num = totdoc;
615  int32_t num_kernels = kernel->get_num_subkernels() ;
616  float64_t sumalpha = 0;
617  float64_t* sumw=SG_MALLOC(float64_t, num_kernels);
618 
619  for (int32_t i=0; i<num; i++)
620  sumalpha-=a[i]*(learn_parm->eps[i]-label[i]*c[i]);
621 
622 #ifdef HAVE_LAPACK
623  int nk = (int) num_kernels; // calling external lib
624  double* alphay = SG_MALLOC(double, num);
625  for (int32_t i=0; i<num; i++)
626  alphay[i]=a[i]*label[i];
627 
628  for (int32_t i=0; i<num_kernels; i++)
629  sumw[i]=0;
630 
631  cblas_dgemv(CblasColMajor, CblasNoTrans, nk, (int) num, 0.5, (double*) W,
632  nk, (double*) alphay, 1, 1.0, (double*) sumw, 1);
633 
634  SG_FREE(alphay);
635 #else
636  for (int32_t d=0; d<num_kernels; d++)
637  {
638  sumw[d]=0;
639  for(int32_t i=0; i<num; i++)
640  sumw[d] += 0.5*a[i]*label[i]*W[i*num_kernels+d];
641  }
642 #endif
643 
644  if (callback)
645  mkl_converged=callback(mkl, sumw, sumalpha);
646 
647  const float64_t* new_beta = kernel->get_subkernel_weights(num_kernels);
648 
649  // update lin
650 #ifdef HAVE_LAPACK
651  cblas_dgemv(CblasColMajor, CblasTrans, nk, (int) num, 1.0, (double*) W,
652  nk, (double*) new_beta, 1, 0.0, (double*) lin, 1);
653 #else
654  for(int32_t i=0; i<num; i++)
655  lin[i]=0 ;
656  for (int32_t d=0; d<num_kernels; d++)
657  if (new_beta[d]!=0)
658  for(int32_t i=0; i<num; i++)
659  lin[i] += new_beta[d]*W[i*num_kernels+d] ;
660 #endif
661 
662 
663  SG_FREE(sumw);
664 }
665 
666 
668  int32_t* label, float64_t *a, SHRINK_STATE *shrink_state, float64_t *lin,
669  float64_t *c, int32_t totdoc, int32_t iteration, int32_t *inconsistent,
670  int32_t* docs, float64_t *aicache, float64_t *maxdiff)
671  /* Make all variables active again which had been removed by
672  shrinking. */
673  /* Computes lin for those variables from scratch. */
674 {
675  register int32_t i=0,j,ii=0,jj,t,*changed2dnum,*inactive2dnum;
676  int32_t *changed,*inactive;
677  register float64_t *a_old,dist;
678  float64_t ex_c,target;
679 
680  if (kernel->has_property(KP_LINADD) && get_linadd_enabled()) { /* special linear case */
681  a_old=shrink_state->last_a;
682 
683  kernel->clear_normal();
684  int32_t num_modified=0;
685  for(i=0;i<totdoc;i++) {
686  if(a[i] != a_old[i]) {
687  kernel->add_to_normal(regression_fix_index(docs[i]), ((a[i]-a_old[i])*(float64_t)label[i]));
688  a_old[i]=a[i];
689  num_modified++;
690  }
691  }
692 
693  if (num_modified>0)
694  {
695  for(i=0;i<totdoc;i++) {
696  if(!shrink_state->active[i]) {
697  lin[i]=shrink_state->last_lin[i]+kernel->compute_optimized(regression_fix_index(docs[i]));
698  }
699  shrink_state->last_lin[i]=lin[i];
700  }
701  }
702  }
703  else
704  {
705  changed=SG_MALLOC(int32_t, totdoc);
706  changed2dnum=SG_MALLOC(int32_t, totdoc+11);
707  inactive=SG_MALLOC(int32_t, totdoc);
708  inactive2dnum=SG_MALLOC(int32_t, totdoc+11);
709  for(t=shrink_state->deactnum-1;(t>=0) && shrink_state->a_history[t];t--) {
710  if(verbosity>=2) {
711  SG_INFO( "%ld..",t);
712  }
713  a_old=shrink_state->a_history[t];
714  for(i=0;i<totdoc;i++) {
715  inactive[i]=((!shrink_state->active[i])
716  && (shrink_state->inactive_since[i] == t));
717  changed[i]= (a[i] != a_old[i]);
718  }
719  compute_index(inactive,totdoc,inactive2dnum);
720  compute_index(changed,totdoc,changed2dnum);
721 
722  for(ii=0;(i=changed2dnum[ii])>=0;ii++) {
723  CKernelMachine::kernel->get_kernel_row(i,inactive2dnum,aicache);
724  for(jj=0;(j=inactive2dnum[jj])>=0;jj++)
725  lin[j]+=(a[i]-a_old[i])*aicache[j]*(float64_t)label[i];
726  }
727  }
728  SG_FREE(changed);
729  SG_FREE(changed2dnum);
730  SG_FREE(inactive);
731  SG_FREE(inactive2dnum);
732  }
733 
734  (*maxdiff)=0;
735  for(i=0;i<totdoc;i++) {
736  shrink_state->inactive_since[i]=shrink_state->deactnum-1;
737  if(!inconsistent[i]) {
738  dist=(lin[i]-model->b)*(float64_t)label[i];
739  target=-(learn_parm->eps[i]-(float64_t)label[i]*c[i]);
740  ex_c=learn_parm->svm_cost[i]-learn_parm->epsilon_a;
741  if((a[i]>learn_parm->epsilon_a) && (dist > target)) {
742  if((dist-target)>(*maxdiff)) /* largest violation */
743  (*maxdiff)=dist-target;
744  }
745  else if((a[i]<ex_c) && (dist < target)) {
746  if((target-dist)>(*maxdiff)) /* largest violation */
747  (*maxdiff)=target-dist;
748  }
749  if((a[i]>(0+learn_parm->epsilon_a))
750  && (a[i]<ex_c)) {
751  shrink_state->active[i]=1; /* not at bound */
752  }
753  else if((a[i]<=(0+learn_parm->epsilon_a)) && (dist < (target+learn_parm->epsilon_shrink))) {
754  shrink_state->active[i]=1;
755  }
756  else if((a[i]>=ex_c)
757  && (dist > (target-learn_parm->epsilon_shrink))) {
758  shrink_state->active[i]=1;
759  }
760  else if(learn_parm->sharedslack) { /* make all active when sharedslack */
761  shrink_state->active[i]=1;
762  }
763  }
764  }
765  if (use_kernel_cache) { /* update history for non-linear */
766  for(i=0;i<totdoc;i++) {
767  (shrink_state->a_history[shrink_state->deactnum-1])[i]=a[i];
768  }
769  for(t=shrink_state->deactnum-2;(t>=0) && shrink_state->a_history[t];t--) {
770  SG_FREE(shrink_state->a_history[t]);
771  shrink_state->a_history[t]=0;
772  }
773  }
774 }
775 #endif //USE_SVMLIGHT

SHOGUN Machine Learning Toolbox - Documentation