34 #include <shogun/lib/external/pr_loqo.h>
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 struct S_THREAD_PARAM_REACTIVATE_LINADD
69 struct S_THREAD_PARAM_SVMLIGHT
74 int32_t * active2dnum ;
79 struct S_THREAD_PARAM_REACTIVATE_VANILLA
86 int32_t* changed2dnum;
87 int32_t* inactive2dnum;
93 struct S_THREAD_PARAM_KERNEL
101 #endif // DOXYGEN_SHOULD_SKIP_THIS
105 S_THREAD_PARAM_SVMLIGHT* params = (S_THREAD_PARAM_SVMLIGHT*) p;
109 for (jj=params->start;(jj<params->end) && (j=params->active2dnum[jj])>=0;jj++)
110 params->lin[j]+=params->kernel->compute_optimized(params->docs[j]);
117 S_THREAD_PARAM_KERNEL* params = (S_THREAD_PARAM_KERNEL*) p;
120 for (jj=params->start;jj<params->end;jj++)
121 params->Kval[jj]=params->svmlight->compute_kernel(params->KI[jj], params->KJ[jj]) ;
153 model=SG_MALLOC(MODEL, 1);
167 SG_FREE(
model->supvec);
168 SG_FREE(
model->alpha);
169 SG_FREE(
model->index);
212 SG_ERROR(
"SVM_light can not proceed without kernel!\n")
218 SG_ERROR(
"%s::train_machine(): Number of training vectors (%d) does"
219 " not match number of labels (%d)\n",
get_name(),
226 SG_ERROR(
"SVM_light can not proceed without initialized kernel!\n")
274 for (int32_t i=0; i<
model->sv_num-1; i++)
303 int32_t *inconsistent, i;
304 int32_t misclassified,upsupvecnum;
307 int32_t trainpos=0, trainneg=0 ;
310 int32_t totdoc=lab.
vlen;
314 int32_t* docs=SG_MALLOC(int32_t, totdoc);
326 for (i=0; i<totdoc; i++)
331 TIMING timing_profile;
332 SHRINK_STATE shrink_state;
334 timing_profile.time_kernel=0;
335 timing_profile.time_opti=0;
336 timing_profile.time_shrink=0;
337 timing_profile.time_update=0;
338 timing_profile.time_model=0;
339 timing_profile.time_check=0;
340 timing_profile.time_select=0;
350 inconsistent = SG_MALLOC(int32_t, totdoc);
353 a_fullset = SG_MALLOC(
float64_t, totdoc);
354 xi_fullset = SG_MALLOC(
float64_t, totdoc);
366 SG_FREE(
model->supvec);
367 SG_FREE(
model->alpha);
368 SG_FREE(
model->index);
369 model->supvec = SG_MALLOC(int32_t, totdoc+2);
371 model->index = SG_MALLOC(int32_t, totdoc+2);
373 model->at_upper_bound=0;
377 model->totdoc=totdoc;
383 model->loo_recall=-1;
384 model->loo_precision=-1;
387 model->xa_precision=-1;
389 for (i=0;i<totdoc;i++) {
401 else if(label[i] < 0) {
416 SG_INFO(
"Computing starting state...")
421 for (i=0; i<totdoc; i++)
427 int32_t* index = SG_MALLOC(int32_t, totdoc);
428 int32_t* index2dnum = SG_MALLOC(int32_t, totdoc+11);
430 for (i=0;i<totdoc;i++) {
432 alpha[i]=fabs(alpha[i]);
433 if(alpha[i]<0) alpha[i]=0;
447 for (i=0;i<totdoc;i++)
448 if((alpha[i]>0) && (alpha[i]<
learn_parm->svm_cost[i])
452 for (i=0;i<totdoc;i++)
462 for (i=0;i<totdoc;i++)
463 if((alpha[i]>0) && (alpha[i]<
learn_parm->svm_cost[i])
467 for (i=0;i<totdoc;i++)
477 index2dnum,index2dnum);
478 for (i=0;i<totdoc;i++) {
490 SG_DEBUG(
"%d totdoc %d pos %d neg\n", totdoc, trainpos, trainneg)
495 &shrink_state,inconsistent,a,lin,
497 &maxdiff,(int32_t)-1,
505 SG_DEBUG(
"(%ld iterations)", iterations)
509 for (i=0;(i<totdoc);i++) {
514 SG_INFO(
"Optimization finished (%ld misclassified, maxdiff=%.8f).\n",
515 misclassified,maxdiff);
519 SG_WARNING(
"maximum violation (%f) exceeds svm_epsilon (%f) due to numerical difficulties\n", maxdiff,
epsilon)
522 for (i=1;i<
model->sv_num;i++)
524 if(fabs(
model->alpha[i]) >=
529 SG_INFO(
"Number of SV: %d (including %d at upper bound)\n",
530 model->sv_num-1,upsupvecnum);
535 SG_FREE(inconsistent);
547 SHRINK_STATE *shrink_state,
548 int32_t *inconsistent,
550 TIMING *timing_profile,
float64_t *maxdiff,
551 int32_t heldout, int32_t retrain)
570 int32_t *chosen,*key,i,j,jj,*last_suboptimal_at,noshrink;
571 int32_t inconsistentnum,choosenum,already_chosen=0,iteration;
572 int32_t misclassified,supvecnum=0,*active2dnum,inactivenum;
573 int32_t *working2dnum,*selexam;
577 int32_t t0=0,t1=0,t2=0,t3=0,t4=0,t5=0,t6=0;
581 int32_t bestmaxdiffiter,terminate;
582 bool reactivated=
false;
597 chosen = SG_MALLOC(int32_t, totdoc);
598 last_suboptimal_at =SG_MALLOC(int32_t, totdoc);
599 key =SG_MALLOC(int32_t, totdoc+11);
601 selexam =SG_MALLOC(int32_t, totdoc);
604 working2dnum =SG_MALLOC(int32_t, totdoc+11);
605 active2dnum =SG_MALLOC(int32_t, totdoc+11);
616 if(!retrain) retrain=1;
619 bestmaxdiff=999999999;
626 for (i=0;i<totdoc;i++) {
629 last_suboptimal_at[i]=1;
633 activenum=
compute_index(shrink_state->active,totdoc,active2dnum);
634 inactivenum=totdoc-activenum;
654 SG_DEBUG(
"\nSelecting working set... ")
661 for (jj=0;(j=working2dnum[jj])>=0;jj++) {
679 for (jj=0;(j=working2dnum[jj])>=0;jj++) {
683 for (i=0;i<totdoc;i++) {
684 if((inconsistent[i] || (heldout==i)) && (a[i] != 0.0)) {
692 for (i=0;i<totdoc;i++) {
695 for (i=0;(i<totdoc) && (fabs(eq) >
learn_parm->epsilon_a);i++) {
696 if((eq*label[i] > 0) && (a[i] > 0)) {
699 if((eq*label[i]) > a[i]) {
722 label,a,lin,c,totdoc,
725 inconsistent,active2dnum,
726 working2dnum,selcrit,selexam,1,
728 choosenum+=already_chosen;
731 label,a,lin,c,totdoc,
734 inconsistent,active2dnum,
735 working2dnum,selcrit,selexam,0,key,
742 label,a,lin,c,totdoc,
745 inconsistent,active2dnum,
746 working2dnum,selcrit,selexam,key,
752 SG_INFO(
" %ld vectors chosen\n",choosenum)
780 optimize_svm(docs,label,inconsistent,0.0,chosen,active2dnum,
781 totdoc,working2dnum,choosenum,a,lin,c,
782 aicache,&qp,&epsilon_crit_org);
794 for (jj=0;(i=working2dnum[jj])>=0;jj++) {
799 maxdiff,epsilon_crit_org,&misclassified,
800 inconsistent,active2dnum,last_suboptimal_at,
805 timing_profile->time_select+=t1-t0;
806 timing_profile->time_kernel+=t2-t1;
807 timing_profile->time_opti+=t3-t2;
808 timing_profile->time_update+=t4-t3;
809 timing_profile->time_model+=t5-t4;
810 timing_profile->time_check+=t6-t5;
814 if((*maxdiff) < bestmaxdiff) {
815 bestmaxdiff=(*maxdiff);
816 bestmaxdiffiter=iteration;
818 if(iteration > (bestmaxdiffiter+
learn_parm->maxiter)) {
822 SG_WARNING(
"Relaxing KT-Conditions due to slow progress! Terminating!\n")
823 SG_DEBUG(
"(iteration :%d, bestmaxdiffiter: %d, lean_param->maxiter: %d)\n", iteration, bestmaxdiffiter,
learn_parm->maxiter )
828 if ((!
callback) && (!retrain) && (inactivenum>0) &&
832 SG_DEBUG(
"reactivating inactive examples\n")
835 iteration,inconsistent,
839 SG_DEBUG(
"done reactivating inactive examples (maxdiff:%8f eps_crit:%8f orig_eps:%8f)\n", *maxdiff,
learn_parm->epsilon_crit, epsilon_crit_org)
841 activenum=
compute_index(shrink_state->active,totdoc,active2dnum);
842 inactivenum=totdoc-activenum;
844 bestmaxdiff=(*maxdiff);
845 bestmaxdiffiter=iteration;
852 SG_INFO(
"restarting optimization as we are - due to shrinking - deviating too much (maxdiff(%f) > eps(%f))\n", *maxdiff,
learn_parm->epsilon_crit)
859 SG_DEBUG(
"Number of inactive variables = %ld\n", inactivenum)
863 if((!retrain) && (
learn_parm->epsilon_crit>(*maxdiff)))
865 if((!retrain) && (
learn_parm->epsilon_crit>epsilon_crit_org)) {
874 SG_INFO(
" => (%ld SV (incl. %ld SV at u-bound), max violation=%.5f)\n",
875 supvecnum,
model->at_upper_bound,(*maxdiff));
881 if (((iteration % 10) == 0) && (!noshrink) && !
callback)
883 activenum=
shrink_problem(shrink_state,active2dnum,last_suboptimal_at,iteration,totdoc,
885 CMath::max((int32_t)(totdoc/500),(int32_t) 100)),
886 a,inconsistent, c, lin, label);
888 inactivenum=totdoc-activenum;
895 shrink_state->active);
899 if (bestmaxdiff>worstmaxdiff)
900 worstmaxdiff=bestmaxdiff;
912 SG_DEBUG(
"inactive:%d\n", inactivenum)
914 if (inactivenum && !reactivated && !
callback)
916 SG_DEBUG(
"reactivating inactive examples\n")
918 iteration,inconsistent,
921 SG_DEBUG(
"done reactivating inactive examples\n")
923 activenum=
compute_index(shrink_state->active,totdoc,active2dnum);
924 inactivenum=totdoc-activenum;
926 bestmaxdiff=(*maxdiff);
927 bestmaxdiffiter=iteration;
935 SG_FREE(last_suboptimal_at);
941 SG_FREE(working2dnum);
942 SG_FREE(active2dnum);
947 SG_FREE(qp.opt_xinit);
965 for (int32_t i=0;i<totdoc;i++)
966 criterion=criterion+(eps[i]-(
float64_t)label[i]*c[i])*a[i]+0.5*a[i]*label[i]*lin[i];
982 for (i=0;index[i] != -1;i++);
988 int32_t *binfeature, int32_t range, int32_t *index)
991 register int32_t i,ii;
994 for (i=0;i<range;i++) {
1008 int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
1009 float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t totdoc,
1023 exclude_from_eq_const,eq_target,chosen,
1024 active2dnum,working2dnum,a,lin,c,
1025 varnum,totdoc,aicache,qp);
1040 for (i=0;i<varnum;i++)
1041 a[working2dnum[i]]=a_v[i];
1045 int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
1046 float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key,
1053 chosen, active2dnum, key, a, lin, c,
1054 varnum, totdoc, aicache, qp) ;
1059 register int32_t ki,kj,i,j;
1063 qp->opt_ce0[0]=-eq_target;
1064 for (j=1;j<
model->sv_num;j++) {
1065 if((!chosen[
model->supvec[j]])
1066 && (!exclude_from_eq_const[(
model->supvec[j])])) {
1067 qp->opt_ce0[0]+=
model->alpha[j];
1076 for (i=0;i<varnum;i++) {
1077 qp->opt_g0[i]=lin[key[i]];
1081 int32_t *KI=SG_MALLOC(int32_t, varnum*varnum);
1082 int32_t *KJ=SG_MALLOC(int32_t, varnum*varnum);
1085 for (i=0;i<varnum;i++) {
1090 for (j=i+1;j<varnum;j++)
1098 ASSERT(Knum<=varnum*(varnum+1)/2)
1106 params[t].svmlight =
this;
1107 params[t].start = t*step;
1108 params[t].end = (t+1)*step;
1111 params[t].Kval=Kval ;
1118 pthread_join(threads[t], NULL);
1124 for (i=0;i<varnum;i++) {
1128 qp->opt_ce[i]=label[ki];
1132 kernel_temp=Kval[Knum] ;
1135 qp->opt_g0[i]-=(kernel_temp*a[ki]*(
float64_t)label[ki]);
1137 qp->opt_g[varnum*i+i]=kernel_temp;
1139 for (j=i+1;j<varnum;j++) {
1141 kernel_temp=Kval[Knum] ;
1144 qp->opt_g0[i]-=(kernel_temp*a[kj]*(
float64_t)label[kj]);
1145 qp->opt_g0[j]-=(kernel_temp*a[ki]*(
float64_t)label[ki]);
1148 qp->opt_g[varnum*j+i]=qp->opt_g[varnum*i+j];
1162 for (i=0;i<varnum;i++) {
1164 qp->opt_xinit[i]=a[key[i]];
1177 int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const,
1178 float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key,
1182 register int32_t ki,kj,i,j;
1186 qp->opt_ce0[0]=-eq_target;
1187 for (j=1;j<
model->sv_num;j++) {
1188 if((!chosen[
model->supvec[j]])
1189 && (!exclude_from_eq_const[(
model->supvec[j])])) {
1190 qp->opt_ce0[0]+=
model->alpha[j];
1199 for (i=0;i<varnum;i++) {
1200 qp->opt_g0[i]=lin[key[i]];
1203 for (i=0;i<varnum;i++) {
1207 qp->opt_ce[i]=label[ki];
1213 qp->opt_g0[i]-=(kernel_temp*a[ki]*(
float64_t)label[ki]);
1215 qp->opt_g[varnum*i+i]=kernel_temp;
1217 for (j=i+1;j<varnum;j++) {
1222 qp->opt_g0[i]-=(kernel_temp*a[kj]*(
float64_t)label[kj]);
1223 qp->opt_g0[j]-=(kernel_temp*a[ki]*(
float64_t)label[ki]);
1226 qp->opt_g[varnum*j+i]=qp->opt_g[varnum*i+j];
1236 for (i=0;i<varnum;i++) {
1238 qp->opt_xinit[i]=a[key[i]];
1255 int32_t i,ii,pos,b_calculated=0,first_low,first_high;
1267 for (ii=0;(i=working2dnum[ii])>=0;ii++) {
1268 if((a_old[i]>0) && (a[i]==0)) {
1269 pos=
model->index[i];
1276 else if((a_old[i]==0) && (a[i]>0)) {
1282 else if(a_old[i]==a[i]) {
1289 if((a_old[i]>=ex_c) && (a[i]<ex_c)) {
1290 (
model->at_upper_bound)--;
1292 else if((a_old[i]<ex_c) && (a[i]>=ex_c)) {
1293 (
model->at_upper_bound)++;
1297 && (a[i]>
learn_parm->epsilon_a) && (a[i]<ex_c)) {
1308 && (
model->sv_num-1 ==
model->at_upper_bound)) {
1313 for (ii=0;(i=active2dnum[ii])>=0;ii++) {
1318 if((b_temp>b_low) || (first_low)) {
1325 if((b_temp<b_high) || (first_high)) {
1334 if((b_temp>b_low) || (first_low)) {
1341 if((b_temp<b_high) || (first_high)) {
1351 else if(first_low) {
1355 model->b=-(b_high+b_low)/2.0;
1363 return(
model->sv_num-1);
1369 int32_t *inconsistent, int32_t *active2dnum, int32_t *last_suboptimal_at,
1373 int32_t i,ii,retrain;
1383 for (ii=0;(i=active2dnum[ii])>=0;ii++) {
1384 if((!inconsistent[i]) && label[i]) {
1392 if((a[i]>
learn_parm->epsilon_a) && (dist > target)) {
1393 if((dist-target)>(*maxdiff))
1394 (*maxdiff)=dist-target;
1396 else if((a[i]<ex_c) && (dist < target)) {
1397 if((target-dist)>(*maxdiff))
1398 (*maxdiff)=target-dist;
1407 last_suboptimal_at[i]=iteration;
1410 && (dist < (target+
learn_parm->epsilon_shrink))) {
1411 last_suboptimal_at[i]=iteration;
1413 else if((a[i]>=ex_c)
1414 && (dist > (target-
learn_parm->epsilon_shrink))) {
1415 last_suboptimal_at[i]=iteration;
1421 if((!retrain) && ((*maxdiff) >
learn_parm->epsilon_crit)) {
1428 int32_t* docs, int32_t* label, int32_t *active2dnum,
float64_t *a,
1436 register int32_t i=0,ii=0,j=0,jj=0;
1443 a_old, working2dnum, totdoc, lin, aicache);
1449 int32_t num_working=0;
1450 for (ii=0;(i=working2dnum[ii])>=0;ii++) {
1451 if(a[i] != a_old[i]) {
1461 for (jj=0;(j=active2dnum[jj])>=0;jj++) {
1468 int32_t num_elem = 0 ;
1469 for (jj=0;(j=active2dnum[jj])>=0;jj++) num_elem++ ;
1475 int32_t end = step ;
1479 params[t].kernel =
kernel ;
1480 params[t].lin = lin ;
1481 params[t].docs = docs ;
1482 params[t].active2dnum=active2dnum ;
1483 params[t].start = start ;
1484 params[t].end = end ;
1495 pthread_join(threads[t], &ret) ;
1509 a, a_old, working2dnum, totdoc, lin, aicache);
1512 for (jj=0;(i=working2dnum[jj])>=0;jj++) {
1513 if(a[i] != a_old[i]) {
1515 for (ii=0;(j=active2dnum[ii])>=0;ii++)
1516 lin[j]+=(a[i]-a_old[i])*aicache[j]*(
float64_t)label[i];
1525 int32_t* docs, int32_t* label, int32_t *active2dnum,
float64_t *a,
1531 int32_t num_weights = -1;
1534 ASSERT(num_weights==num_kernels)
1541 int32_t n = 0, i, j ;
1548 if(a[i] != a_old[i])
1552 W[j*num_kernels+n]+=(a[i]-a_old[i])*aicache[j]*(
float64_t)label[i];
1566 for (int32_t i=0; i<num_kernels; i++)
1568 w_backup[i] = old_beta[i] ;
1571 for (int32_t n=0; n<num_kernels; n++)
1576 for (int32_t i=0;i<num;i++)
1578 if(a[i] != a_old[i])
1580 for (int32_t j=0;j<num;j++)
1599 int32_t* docs, int32_t* label, int32_t *active2dnum,
float64_t *a,
1608 int32_t num_weights = -1;
1611 ASSERT(num_weights==num_kernels)
1617 for (int32_t i=0; i<num_kernels; i++)
1619 w_backup[i] = old_beta[i] ;
1627 for (int32_t ii=0, i=0;(i=working2dnum[ii])>=0;ii++) {
1628 if(a[i] != a_old[i]) {
1636 for (int32_t i=0; i<num; i++)
1648 params[t].kernel =
kernel;
1650 params[t].start = t*step;
1651 params[t].end = (t+1)*step;
1659 pthread_join(threads[t], NULL);
1674 S_THREAD_PARAM_SVMLIGHT* params = (S_THREAD_PARAM_SVMLIGHT*) p;
1676 int32_t num_kernels=params->kernel->get_num_subkernels();
1679 for (int32_t i=params->start; i<params->end; i++)
1680 params->kernel->compute_by_subkernel(i,&(params->W[i*num_kernels]));
1693 int nk = (int) num_kernels;
1694 double* alphay = SG_MALLOC(
double, num);
1696 for (int32_t i=0; i<num; i++)
1698 alphay[i]=a[i]*label[i];
1702 for (int32_t i=0; i<num_kernels; i++)
1705 cblas_dgemv(CblasColMajor, CblasNoTrans, num_kernels, (
int) num, 0.5, (
double*)
W,
1706 num_kernels, alphay, 1, 1.0, (
double*) sumw, 1);
1710 for (int32_t i=0; i<num; i++)
1713 for (int32_t d=0; d<num_kernels; d++)
1716 for (int32_t i=0; i<num; i++)
1717 sumw[d] += a[i]*(0.5*label[i]*
W[i*num_kernels+d]);
1729 cblas_dgemv(CblasColMajor, CblasTrans, nk, (
int) num, 1.0, (
double*)
W,
1730 nk, (
double*) new_beta, 1, 0.0, (
double*) lin, 1);
1732 for (int32_t i=0; i<num; i++)
1734 for (int32_t d=0; d<num_kernels; d++)
1736 for (int32_t i=0; i<num; i++)
1737 lin[i] += new_beta[d]*
W[i*num_kernels+d] ;
1748 int32_t qp_size, int32_t *inconsistent, int32_t *active2dnum,
1749 int32_t *working2dnum,
float64_t *selcrit, int32_t *select,
1750 int32_t cache_only, int32_t *key, int32_t *chosen)
1756 int32_t choosenum,i,j,k,activedoc,inum,valid;
1759 for (inum=0;working2dnum[inum]>=0;inum++);
1762 for (i=0;(j=active2dnum[i])>=0;i++) {
1774 && (!((a[j]<=(0+
learn_parm->epsilon_a)) && (s<0)))
1779 && (!inconsistent[j]))
1786 select_top_n(selcrit,activedoc,select,(int32_t)(qp_size/2));
1787 for (k=0;(choosenum<(qp_size/2)) && (k<(qp_size/2)) && (k<activedoc);k++) {
1790 working2dnum[inum+choosenum]=i;
1799 for (i=0;(j=active2dnum[i])>=0;i++) {
1811 && (!((a[j]<=(0+
learn_parm->epsilon_a)) && (s<0)))
1816 && (!inconsistent[j]))
1824 select_top_n(selcrit,activedoc,select,(int32_t)(qp_size/2));
1825 for (k=0;(choosenum<qp_size) && (k<(qp_size/2)) && (k<activedoc);k++) {
1828 working2dnum[inum+choosenum]=i;
1834 working2dnum[inum+choosenum]=-1;
1840 int32_t qp_size, int32_t *inconsistent, int32_t *active2dnum,
1841 int32_t *working2dnum,
float64_t *selcrit, int32_t *select, int32_t *key,
1842 int32_t *chosen, int32_t iteration)
1848 int32_t choosenum,i,j,k,activedoc,inum;
1851 for (inum=0;working2dnum[inum]>=0;inum++);
1854 for (i=0;(j=active2dnum[i])>=0;i++) {
1856 if((!((a[j]<=(0+
learn_parm->epsilon_a)) && (s<0)))
1859 && (!inconsistent[j])
1862 selcrit[activedoc]=(j+iteration) % totdoc;
1867 select_top_n(selcrit,activedoc,select,(int32_t)(qp_size/2));
1868 for (k=0;(choosenum<(qp_size/2)) && (k<(qp_size/2)) && (k<activedoc);k++) {
1871 working2dnum[inum+choosenum]=i;
1879 for (i=0;(j=active2dnum[i])>=0;i++) {
1881 if((!((a[j]<=(0+
learn_parm->epsilon_a)) && (s<0)))
1884 && (!inconsistent[j])
1887 selcrit[activedoc]=(j+iteration) % totdoc;
1892 select_top_n(selcrit,activedoc,select,(int32_t)(qp_size/2));
1893 for (k=0;(choosenum<qp_size) && (k<(qp_size/2)) && (k<activedoc);k++) {
1896 working2dnum[inum+choosenum]=i;
1902 working2dnum[inum+choosenum]=-1;
1909 float64_t *selcrit, int32_t range, int32_t* select, int32_t n)
1911 register int32_t i,j;
1913 for (i=0;(i<n) && (i<range);i++) {
1914 for (j=i;j>=0;j--) {
1915 if((j>0) && (selcrit[select[j-1]]<selcrit[i])){
1916 select[j]=select[j-1];
1925 for (i=n;i<range;i++) {
1926 if(selcrit[i]>selcrit[select[n-1]]) {
1927 for (j=n-1;j>=0;j--) {
1928 if((j>0) && (selcrit[select[j-1]]<selcrit[i])) {
1929 select[j]=select[j-1];
1945 SHRINK_STATE *shrink_state, int32_t totdoc, int32_t maxhistory)
1949 shrink_state->deactnum=0;
1950 shrink_state->active = SG_MALLOC(int32_t, totdoc);
1951 shrink_state->inactive_since = SG_MALLOC(int32_t, totdoc);
1952 shrink_state->a_history = SG_MALLOC(
float64_t*, maxhistory);
1953 shrink_state->maxhistory=maxhistory;
1954 shrink_state->last_lin = SG_MALLOC(
float64_t, totdoc);
1955 shrink_state->last_a = SG_MALLOC(
float64_t, totdoc);
1957 for (i=0;i<totdoc;i++) {
1958 shrink_state->active[i]=1;
1959 shrink_state->inactive_since[i]=0;
1960 shrink_state->last_a[i]=0;
1961 shrink_state->last_lin[i]=0;
1967 SG_FREE(shrink_state->active);
1968 SG_FREE(shrink_state->inactive_since);
1969 if(shrink_state->deactnum > 0)
1970 SG_FREE((shrink_state->a_history[shrink_state->deactnum-1]));
1971 SG_FREE((shrink_state->a_history));
1972 SG_FREE((shrink_state->last_a));
1973 SG_FREE((shrink_state->last_lin));
1977 SHRINK_STATE *shrink_state, int32_t *active2dnum,
1978 int32_t *last_suboptimal_at, int32_t iteration, int32_t totdoc,
1984 int32_t i,ii,change,activenum,lastiter;
1989 for (ii=0;active2dnum[ii]>=0;ii++) {
1992 lastiter=last_suboptimal_at[i];
1994 if(((iteration-lastiter) >
learn_parm->svm_iter_to_shrink)
1995 || (inconsistent[i])) {
1999 if((change>=minshrink)
2000 && (shrink_state->deactnum<shrink_state->maxhistory)) {
2010 shrink_state->a_history[shrink_state->deactnum]=a_old;
2011 for (i=0;i<totdoc;i++) {
2015 for (ii=0;active2dnum[ii]>=0;ii++) {
2017 lastiter=last_suboptimal_at[i];
2018 if(((iteration-lastiter) >
learn_parm->svm_iter_to_shrink)
2019 || (inconsistent[i])) {
2020 shrink_state->active[i]=0;
2021 shrink_state->inactive_since[i]=shrink_state->deactnum;
2024 activenum=
compute_index(shrink_state->active,totdoc,active2dnum);
2025 shrink_state->deactnum++;
2027 shrink_state->deactnum=0;
2031 SG_DEBUG(
"Number of inactive variables = %ld\n", totdoc-activenum)
2039 S_THREAD_PARAM_REACTIVATE_LINADD* params = (S_THREAD_PARAM_REACTIVATE_LINADD*) p;
2044 int32_t* active = params->active;
2045 int32_t* docs = params->docs;
2046 int32_t start = params->start;
2047 int32_t end = params->end;
2049 for (int32_t i=start;i<end;i++)
2062 S_THREAD_PARAM_REACTIVATE_VANILLA* params = (S_THREAD_PARAM_REACTIVATE_VANILLA*) p;
2069 ASSERT(params->changed2dnum)
2070 ASSERT(params->inactive2dnum)
2078 int32_t* changed2dnum = params->changed2dnum;
2079 int32_t* inactive2dnum = params->inactive2dnum;
2080 int32_t* label = params->label;
2081 int32_t start = params->start;
2082 int32_t end = params->end;
2084 for (int32_t ii=start;ii<end;ii++)
2086 int32_t i=changed2dnum[ii];
2091 for (int32_t jj=0;(j=inactive2dnum[jj])>=0;jj++)
2092 lin[j]+=(a[i]-a_old[i])*aicache[j]*(
float64_t)label[i];
2099 float64_t *c, int32_t totdoc, int32_t iteration, int32_t *inconsistent,
2105 register int32_t i,j,ii,jj,t,*changed2dnum,*inactive2dnum;
2106 int32_t *changed,*inactive;
2111 a_old=shrink_state->last_a;
2115 SG_DEBUG(
" clear normal - linadd\n")
2118 int32_t num_modified=0;
2119 for (i=0;i<totdoc;i++) {
2120 if(a[i] != a_old[i]) {
2131 if (num_threads < 2)
2133 S_THREAD_PARAM_REACTIVATE_LINADD params;
2136 params.last_lin=shrink_state->last_lin;
2138 params.active=shrink_state->active;
2146 pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
2147 S_THREAD_PARAM_REACTIVATE_LINADD* params = SG_MALLOC(S_THREAD_PARAM_REACTIVATE_LINADD, num_threads);
2148 int32_t step= totdoc/num_threads;
2150 for (t=0; t<num_threads-1; t++)
2154 params[t].last_lin=shrink_state->last_lin;
2155 params[t].docs=docs;
2156 params[t].active=shrink_state->active;
2157 params[t].start = t*step;
2158 params[t].end = (t+1)*step;
2164 params[t].last_lin=shrink_state->last_lin;
2165 params[t].docs=docs;
2166 params[t].active=shrink_state->active;
2167 params[t].start = t*step;
2168 params[t].end = totdoc;
2171 for (t=0; t<num_threads-1; t++)
2172 pthread_join(threads[t], NULL);
2184 int32_t *idx = SG_MALLOC(int32_t, totdoc);
2185 int32_t num_suppvec=0 ;
2187 for (i=0; i<totdoc; i++)
2189 if(a[i] != a_old[i])
2191 alphas[num_suppvec] = (a[i]-a_old[i])*(
float64_t)label[i];
2192 idx[num_suppvec] = i ;
2200 int32_t num_inactive=0;
2201 int32_t* inactive_idx=SG_MALLOC(int32_t, totdoc);
2204 for (i=0;i<totdoc;i++)
2206 if(!shrink_state->active[i])
2208 inactive_idx[j++] = i;
2216 memset(dest, 0,
sizeof(
float64_t)*num_inactive);
2221 for (i=0;i<totdoc;i++) {
2222 if(!shrink_state->active[i]) {
2223 lin[i] = shrink_state->last_lin[i] + dest[j++] ;
2225 shrink_state->last_lin[i]=lin[i];
2232 for (i=0;i<totdoc;i++)
2233 shrink_state->last_lin[i]=lin[i];
2235 SG_FREE(inactive_idx);
2245 changed=SG_MALLOC(int32_t, totdoc);
2246 changed2dnum=SG_MALLOC(int32_t, totdoc+11);
2247 inactive=SG_MALLOC(int32_t, totdoc);
2248 inactive2dnum=SG_MALLOC(int32_t, totdoc+11);
2249 for (t=shrink_state->deactnum-1;(t>=0) && shrink_state->a_history[t];t--)
2254 a_old=shrink_state->a_history[t];
2255 for (i=0;i<totdoc;i++) {
2256 inactive[i]=((!shrink_state->active[i])
2257 && (shrink_state->inactive_since[i] == t));
2258 changed[i]= (a[i] != a_old[i]);
2265 int32_t num_threads=1;
2268 if (num_threads < 2)
2270 for (ii=0;(i=changed2dnum[ii])>=0;ii++) {
2272 for (jj=0;(j=inactive2dnum[jj])>=0;jj++)
2273 lin[j]+=(a[i]-a_old[i])*aicache[j]*(
float64_t)label[i];
2280 int32_t num_changed=0;
2281 for (ii=0;changed2dnum[ii]>=0;ii++)
2286 pthread_t* threads= SG_MALLOC(pthread_t, num_threads-1);
2287 S_THREAD_PARAM_REACTIVATE_VANILLA* params = SG_MALLOC(S_THREAD_PARAM_REACTIVATE_VANILLA, num_threads);
2288 int32_t step= num_changed/num_threads;
2292 memset(tmp_lin, 0,
sizeof(
float64_t)*((
size_t) totdoc)*num_threads);
2294 memset(tmp_aicache, 0,
sizeof(
float64_t)*((
size_t) totdoc)*num_threads);
2297 for (thr=0; thr<num_threads-1; thr++)
2299 params[thr].kernel=
kernel;
2300 params[thr].lin=&tmp_lin[thr*totdoc];
2301 params[thr].aicache=&tmp_aicache[thr*totdoc];
2303 params[thr].a_old=a_old;
2304 params[thr].changed2dnum=changed2dnum;
2305 params[thr].inactive2dnum=inactive2dnum;
2306 params[thr].label=label;
2307 params[thr].start = thr*step;
2308 params[thr].end = (thr+1)*step;
2312 params[thr].kernel=
kernel;
2313 params[thr].lin=&tmp_lin[thr*totdoc];
2314 params[thr].aicache=&tmp_aicache[thr*totdoc];
2316 params[thr].a_old=a_old;
2317 params[thr].changed2dnum=changed2dnum;
2318 params[thr].inactive2dnum=inactive2dnum;
2319 params[thr].label=label;
2320 params[thr].start = thr*step;
2321 params[thr].end = num_changed;
2324 for (jj=0;(j=inactive2dnum[jj])>=0;jj++)
2325 lin[j]+=tmp_lin[totdoc*thr+j];
2327 for (thr=0; thr<num_threads-1; thr++)
2329 pthread_join(threads[thr], NULL);
2332 for (jj=0;(j=inactive2dnum[jj])>=0;jj++)
2333 lin[j]+=tmp_lin[totdoc*thr+j];
2337 SG_FREE(tmp_aicache);
2345 SG_FREE(changed2dnum);
2347 SG_FREE(inactive2dnum);
2351 for (i=0;i<totdoc;i++) {
2352 shrink_state->inactive_since[i]=shrink_state->deactnum-1;
2353 if(!inconsistent[i]) {
2357 if((a[i]>
learn_parm->epsilon_a) && (dist > target)) {
2358 if((dist-target)>(*maxdiff))
2359 (*maxdiff)=dist-target;
2361 else if((a[i]<ex_c) && (dist < target)) {
2362 if((target-dist)>(*maxdiff))
2363 (*maxdiff)=target-dist;
2367 shrink_state->active[i]=1;
2370 shrink_state->active[i]=1;
2372 else if((a[i]>=ex_c)
2373 && (dist > (target-
learn_parm->epsilon_shrink))) {
2374 shrink_state->active[i]=1;
2377 shrink_state->active[i]=1;
2383 for (i=0;i<totdoc;i++) {
2384 (shrink_state->a_history[shrink_state->deactnum-1])[i]=a[i];
2386 for (t=shrink_state->deactnum-2;(t>=0) && shrink_state->a_history[t];t--) {
2387 SG_FREE(shrink_state->a_history[t]);
2388 shrink_state->a_history[t]=0;
2398 int32_t& svm_maxqpsize)
2400 register int32_t i, j, result;
2401 float64_t margin, obj_before, obj_after;
2411 for(i=0;i<qp->opt_n;i++) {
2412 obj_before+=(qp->opt_g0[i]*qp->opt_xinit[i]);
2413 obj_before+=(0.5*qp->opt_xinit[i]*qp->opt_xinit[i]*qp->opt_g[i*qp->opt_n+i]);
2415 obj_before+=(qp->opt_xinit[j]*qp->opt_xinit[i]*qp->opt_g[j*qp->opt_n+i]);
2419 result=STILL_RUNNING;
2420 qp->opt_ce0[0]*=(-1.0);
2424 (margin<=0.9999999) && (result!=OPTIMAL_SOLUTION);) {
2429 result=pr_loqo((int32_t)qp->opt_n,(int32_t)qp->opt_m,
2440 SG_SDEBUG(
"Restarting PR_LOQO with more conservative parameters.\n")
2445 margin=(margin+1.0)/2.0;
2448 SG_SDEBUG(
"Reducing precision of PR_LOQO.\n")
2451 else if(result!=OPTIMAL_SOLUTION) {
2456 SG_SDEBUG(
"Reducing precision of PR_LOQO due to (%ld).\n",result)
2469 for(i=0;i<qp->opt_n;i++) {
2471 dist+=(qp->opt_g0[i]+1.0);
2473 dist+=(
primal[j]*qp->opt_g[j*qp->opt_n+i]);
2475 for(j=i;j<qp->opt_n;j++) {
2476 dist+=(
primal[j]*qp->opt_g[i*qp->opt_n+j]);
2479 if((
primal[i]<(qp->opt_up[i]-epsilon_loqo)) && (dist < (1.0-(*epsilon_crit)))) {
2480 epsilon_loqo=(qp->opt_up[i]-
primal[i])*2.0;
2482 else if((
primal[i]>(0+epsilon_loqo)) && (dist > (1.0+(*epsilon_crit)))) {
2483 epsilon_loqo=
primal[i]*2.0;
2487 for(i=0;i<qp->opt_n;i++) {
2488 if(
primal[i]<=(0+epsilon_loqo)) {
2491 else if(
primal[i]>=(qp->opt_up[i]-epsilon_loqo)) {
2497 for(i=0;i<qp->opt_n;i++) {
2498 obj_after+=(qp->opt_g0[i]*
primal[i]);
2499 obj_after+=(0.5*
primal[i]*
primal[i]*qp->opt_g[i*qp->opt_n+i]);
2501 obj_after+=(
primal[j]*
primal[i]*qp->opt_g[j*qp->opt_n+i]);
2508 for(i=0;i<qp->opt_n;i++) {
2509 primal[i]=qp->opt_xinit[i];
2512 if(svm_maxqpsize>2) {
2517 if(obj_after >= obj_before) {
2521 SG_SDEBUG(
"Increasing Precision of PR_LOQO.\n")
2527 (*epsilon_crit)*=10.0;
2529 SG_SINFO(
"Relaxing epsilon on KT-Conditions.\n")
2534 if(result!=OPTIMAL_SOLUTION) {
2535 SG_SERROR(
"PR_LOQO did not converge.\n")
2536 return(qp->opt_xinit);
2544 #endif //USE_SVMLIGHT
virtual void clear_normal()
Class Time that implements a stopwatch based on either cpu time or wall clock time.
virtual bool init(CFeatures *lhs, CFeatures *rhs)
int32_t get_num_support_vectors()
void optimize_svm(int32_t *docs, int32_t *label, int32_t *exclude_from_eq_const, float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t totdoc, int32_t *working2dnum, int32_t varnum, float64_t *a, float64_t *lin, float64_t *c, float64_t *aicache, QP *qp, float64_t *epsilon_crit_target)
void select_top_n(float64_t *selcrit, int32_t range, int32_t *select, int32_t n)
int32_t get_activenum_cache()
static void fill_vector(T *vec, int32_t len, T value)
virtual ELabelType get_label_type() const =0
int32_t select_next_qp_subproblem_rand(int32_t *label, float64_t *a, float64_t *lin, float64_t *c, int32_t totdoc, int32_t qp_size, int32_t *inconsistent, int32_t *active2dnum, int32_t *working2dnum, float64_t *selcrit, int32_t *select, int32_t *key, int32_t *chosen, int32_t iteration)
virtual void compute_by_subkernel(int32_t vector_idx, float64_t *subkernel_contrib)
void cache_multiple_kernel_rows(int32_t *key, int32_t varnum)
int32_t compute_index(int32_t *binfeature, int32_t range, int32_t *index)
int32_t get_max_elems_cache()
static void * update_linear_component_linadd_helper(void *p)
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)
int32_t get_num_threads() const
static void * reactivate_inactive_examples_linadd_helper(void *p)
The class Labels models labels, i.e. class assignments of objects.
virtual int32_t get_num_labels() const =0
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)
static float64_t log10(float64_t v)
bool(* callback)(CMKL *mkl, const float64_t *sumw, const float64_t suma)
virtual int32_t get_num_vectors() const =0
float64_t m_max_train_time
virtual bool delete_optimization()
int32_t kernel_cache_space_available()
int32_t check_optimality(int32_t *label, float64_t *a, float64_t *lin, float64_t *c, int32_t totdoc, float64_t *maxdiff, float64_t epsilon_crit_org, int32_t *misclassified, int32_t *inconsistent, int32_t *active2dnum, int32_t *last_suboptimal_at, int32_t iteration)
int32_t get_num_kernels()
static void * update_linear_component_mkl_linadd_helper(void *p)
virtual int32_t get_num_vec_lhs()
void compute_matrices_for_optimization_parallel(int32_t *docs, int32_t *label, int32_t *exclude_from_eq_const, float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key, float64_t *a, float64_t *lin, float64_t *c, int32_t varnum, int32_t totdoc, float64_t *aicache, QP *qp)
int32_t kernel_cache_touch(int32_t cacheidx)
void kernel_cache_shrink(int32_t totdoc, int32_t num_shrink, int32_t *after)
void shrink_state_cleanup(SHRINK_STATE *shrink_state)
virtual float64_t * get_linear_term_array()
static T * clone_vector(const T *vec, int32_t len)
SGVector< float64_t > m_alpha
float64_t cur_time_diff(bool verbose=false)
SGVector< float64_t > m_linear_term
bool has_property(EKernelProperty p)
void add_to_index(int32_t *index, int32_t elem)
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)
bool get_batch_computation_enabled()
void set_bias(float64_t bias)
void cache_kernel_row(int32_t x)
void compute_matrices_for_optimization(int32_t *docs, int32_t *label, int32_t *exclude_from_eq_const, float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key, float64_t *a, float64_t *lin, float64_t *c, int32_t varnum, int32_t totdoc, float64_t *aicache, QP *qp)
static void clear_cancel()
CKernel * get_kernel(int32_t idx)
int32_t select_next_qp_subproblem_grad(int32_t *label, float64_t *a, float64_t *lin, float64_t *c, int32_t totdoc, int32_t qp_size, int32_t *inconsistent, int32_t *active2dnum, int32_t *working2dnum, float64_t *selcrit, int32_t *select, int32_t cache_only, int32_t *key, int32_t *chosen)
bool get_shrinking_enabled()
static void * compute_kernel_helper(void *p)
static void * reactivate_inactive_examples_vanilla_helper(void *p)
bool set_alpha(int32_t idx, float64_t val)
void set_objective(float64_t v)
virtual float64_t compute_objective_function(float64_t *a, float64_t *lin, float64_t *c, float64_t *eps, int32_t *label, int32_t totdoc)
virtual float64_t compute_optimized(int32_t vector_idx)
EOptimizationType get_optimization_type()
int32_t precision_violations
float64_t get_alpha(int32_t idx)
virtual bool train_machine(CFeatures *data=NULL)
float64_t * optimize_qp(QP *qp, float64_t *epsilon_crit, int32_t nx, float64_t *threshold, int32_t &svm_maxqpsize)
int32_t calculate_svm_model(int32_t *docs, int32_t *label, float64_t *lin, float64_t *a, float64_t *a_old, float64_t *c, int32_t *working2dnum, int32_t *active2dnum)
bool use_batch_computation
ESolverType get_solver_type()
virtual const float64_t * get_subkernel_weights(int32_t &num_weights)
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)
int32_t get_support_vector(int32_t idx)
static bool cancel_computations()
void clear_index(int32_t *index)
virtual int32_t get_num_vec_rhs()
virtual void set_subkernel_weights(SGVector< float64_t > weights)
#define SG_ABS_PROGRESS(...)
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)
all of classes and functions are contained in the shogun namespace
bool get_linadd_enabled()
virtual void compute_batch(int32_t num_vec, int32_t *vec_idx, float64_t *target, int32_t num_suppvec, int32_t *IDX, float64_t *alphas, float64_t factor=1.0)
virtual EKernelType get_kernel_type()=0
static int is_nan(double f)
checks whether a float is nan
The class Features is the base class of all feature objects.
void call_mkl_callback(float64_t *a, int32_t *label, float64_t *lin)
void kernel_cache_cleanup()
int32_t kernel_cache_check(int32_t cacheidx)
virtual int32_t get_num_subkernels()
A generic Support Vector Machine Interface.
int32_t shrink_problem(SHRINK_STATE *shrink_state, int32_t *active2dnum, int32_t *last_suboptimal_at, int32_t iteration, int32_t totdoc, int32_t minshrink, float64_t *a, int32_t *inconsistent, float64_t *c, float64_t *lin, int *label)
Binary Labels for binary classification.
virtual SGVector< float64_t > get_kernel_row(int32_t i)
void set_kernel(CKernel *k)
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)
virtual float64_t compute_kernel(int32_t i, int32_t j)
virtual bool has_features()
virtual void add_to_normal(int32_t vector_idx, float64_t weight)
float64_t get_objective()
void resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack=false)
bool create_new_model(int32_t num)
virtual const char * get_name() const
void init_shrink_state(SHRINK_STATE *shrink_state, int32_t totdoc, int32_t maxhistory)