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

SHOGUN Machine Learning Toolbox - Documentation