15 template <
class T>
static inline T
min(T
x,T y) {
return (x<y)?x:y; }
18 template <
class T>
static inline T
max(T
x,T y) {
return (x>y)?x:y; }
20 template <
class T>
static inline void swap(T&
x, T& y) { T t=
x; x=y; y=t; }
21 template <
class S,
class T>
static inline void clone(T*& dst, S* src,
int n)
24 memcpy((
void *)dst,(
void *)src,
sizeof(T)*n);
26 static inline double powi(
double base,
int times)
28 double tmp = base, ret = 1.0;
30 for(
int t=times; t>0; t/=2)
39 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 48 static void info(
const char *fmt,...)
55 (*svm_print_string)(buf);
58 static void info(
const char *fmt,...) {}
130 int more = len - h->
len;
173 swap(h->data[i],h->data[j]);
196 virtual Qfloat *get_Q(
int column,
int len)
const = 0;
197 virtual double *get_QD()
const = 0;
198 virtual void swap_index(
int i,
int j)
const = 0;
209 virtual Qfloat *get_Q(
int column,
int len)
const = 0;
210 virtual double *get_QD()
const = 0;
214 if(x_square)
swap(x_square[i],x_square[j]);
218 double (
Kernel::*kernel_function)(
int i,
int j)
const;
233 return dot(x[i],x[j]);
237 return powi(gamma*
dot(x[i],x[j])+coef0,degree);
241 return exp(-gamma*(x_square[i]+x_square[j]-2*
dot(x[i],x[j])));
245 return tanh(gamma*
dot(x[i],x[j])+coef0);
249 return x[i][(int)(x[j][0].value)].
value;
254 :kernel_type(param.kernel_type), degree(param.degree),
255 gamma(param.gamma), coef0(param.coef0)
352 while(x->
index != -1)
358 while(y->
index != -1)
364 return exp(-param.
gamma*sum);
406 void Solve(
int l,
const QMatrix& Q,
const double *p_,
const schar *y_,
407 double *alpha_,
double Cp,
double Cn,
double eps,
428 return (y[i] > 0)? Cp : Cn;
432 if(alpha[i] >= get_C(i))
433 alpha_status[i] = UPPER_BOUND;
434 else if(alpha[i] <= 0)
435 alpha_status[i] = LOWER_BOUND;
436 else alpha_status[i] = FREE;
440 bool is_free(
int i) {
return alpha_status[i] == FREE; }
442 void reconstruct_gradient();
443 virtual int select_working_set(
int &i,
int &j);
444 virtual double calculate_rho();
445 virtual void do_shrinking();
447 bool be_shrunk(
int i,
double Gmax1,
double Gmax2);
455 swap(alpha_status[i],alpha_status[j]);
456 swap(alpha[i],alpha[j]);
458 swap(active_set[i],active_set[j]);
459 swap(G_bar[i],G_bar[j]);
466 if(active_size == l)
return;
471 for(j=active_size;j<l;j++)
472 G[j] = G_bar[j] + p[j];
474 for(j=0;j<active_size;j++)
478 if(2*nr_free < active_size)
479 info(
"\nWARNING: using -h 0 may be faster\n");
481 if (nr_free*l > 2*active_size*(l-active_size))
483 for(i=active_size;i<l;i++)
485 const Qfloat *Q_i = Q->get_Q(i,active_size);
486 for(j=0;j<active_size;j++)
488 G[i] += alpha[j] * Q_i[j];
493 for(i=0;i<active_size;i++)
496 const Qfloat *Q_i = Q->get_Q(i,l);
497 double alpha_i = alpha[i];
498 for(j=active_size;j<l;j++)
499 G[j] += alpha_i * Q_i[j];
505 double *alpha_,
double Cp,
double Cn,
double eps,
513 clone(alpha,alpha_,l);
521 alpha_status =
new char[l];
523 update_alpha_status(i);
528 active_set =
new int[l];
537 G_bar =
new double[l];
545 if(!is_lower_bound(i))
548 double alpha_i = alpha[i];
551 G[j] += alpha_i*Q_i[j];
552 if(is_upper_bound(i))
554 G_bar[j] += get_C(i) * Q_i[j];
561 int max_iter =
max(10000000, l>INT_MAX/100 ? INT_MAX : 100*l);
562 int counter =
min(l,1000)+1;
564 while(iter < max_iter)
570 counter =
min(l,1000);
571 if(shrinking) do_shrinking();
576 if(select_working_set(i,j)!=0)
579 reconstruct_gradient();
583 if(select_working_set(i,j)!=0)
596 double C_i = get_C(i);
597 double C_j = get_C(j);
599 double old_alpha_i = alpha[i];
600 double old_alpha_j = alpha[j];
604 double quad_coef = QD[i]+QD[j]+2*Q_i[j];
607 double delta = (-G[i]-G[j])/quad_coef;
608 double diff = alpha[i] - alpha[j];
633 alpha[j] = C_i - diff;
641 alpha[i] = C_j + diff;
647 double quad_coef = QD[i]+QD[j]-2*Q_i[j];
650 double delta = (G[i]-G[j])/quad_coef;
651 double sum = alpha[i] + alpha[j];
660 alpha[j] = sum - C_i;
676 alpha[i] = sum - C_j;
691 double delta_alpha_i = alpha[i] - old_alpha_i;
692 double delta_alpha_j = alpha[j] - old_alpha_j;
694 for(
int k=0;k<active_size;k++)
696 G[k] += Q_i[k]*delta_alpha_i + Q_j[k]*delta_alpha_j;
702 bool ui = is_upper_bound(i);
703 bool uj = is_upper_bound(j);
704 update_alpha_status(i);
705 update_alpha_status(j);
707 if(ui != is_upper_bound(i))
712 G_bar[k] -= C_i * Q_i[k];
715 G_bar[k] += C_i * Q_i[k];
718 if(uj != is_upper_bound(j))
723 G_bar[k] -= C_j * Q_j[k];
726 G_bar[k] += C_j * Q_j[k];
736 reconstruct_gradient();
740 info(
"\nWARNING: reaching max number of iterations");
745 si->
rho = calculate_rho();
752 v += alpha[i] * (G[i] + p[i]);
760 alpha_[active_set[i]] = alpha[i];
774 info(
"\noptimization finished, #iter = %d\n",iter);
779 delete[] alpha_status;
798 double obj_diff_min =
INF;
800 for(
int t=0;t<active_size;t++)
803 if(!is_upper_bound(t))
812 if(!is_lower_bound(t))
823 Q_i = Q->get_Q(i,active_size);
825 for(
int j=0;j<active_size;j++)
829 if (!is_lower_bound(j))
831 double grad_diff=Gmax+G[j];
837 double quad_coef = QD[i]+QD[j]-2.0*
y[i]*Q_i[j];
839 obj_diff = -(grad_diff*grad_diff)/quad_coef;
841 obj_diff = -(grad_diff*grad_diff)/
TAU;
843 if (obj_diff <= obj_diff_min)
846 obj_diff_min = obj_diff;
853 if (!is_upper_bound(j))
855 double grad_diff= Gmax-G[j];
861 double quad_coef = QD[i]+QD[j]+2.0*
y[i]*Q_i[j];
863 obj_diff = -(grad_diff*grad_diff)/quad_coef;
865 obj_diff = -(grad_diff*grad_diff)/
TAU;
867 if (obj_diff <= obj_diff_min)
870 obj_diff_min = obj_diff;
887 if(is_upper_bound(i))
890 return(-G[i] > Gmax1);
892 return(-G[i] > Gmax2);
894 else if(is_lower_bound(i))
897 return(G[i] > Gmax2);
899 return(G[i] > Gmax1);
912 for(i=0;i<active_size;i++)
916 if(!is_upper_bound(i))
921 if(!is_lower_bound(i))
929 if(!is_upper_bound(i))
934 if(!is_lower_bound(i))
942 if(unshrink ==
false && Gmax1 + Gmax2 <= eps*10)
945 reconstruct_gradient();
950 for(i=0;i<active_size;i++)
951 if (be_shrunk(i, Gmax1, Gmax2))
954 while (active_size > i)
956 if (!be_shrunk(active_size, Gmax1, Gmax2))
970 double ub =
INF, lb = -
INF, sum_free = 0;
971 for(
int i=0;i<active_size;i++)
973 double yG =
y[i]*G[i];
975 if(is_upper_bound(i))
982 else if(is_lower_bound(i))
997 r = sum_free/nr_free;
1014 double *alpha,
double Cp,
double Cn,
double eps,
1022 int select_working_set(
int &i,
int &j);
1023 double calculate_rho();
1024 bool be_shrunk(
int i,
double Gmax1,
double Gmax2,
double Gmax3,
double Gmax4);
1025 void do_shrinking();
1037 double Gmaxp = -
INF;
1038 double Gmaxp2 = -
INF;
1041 double Gmaxn = -
INF;
1042 double Gmaxn2 = -
INF;
1046 double obj_diff_min =
INF;
1048 for(
int t=0;t<active_size;t++)
1051 if(!is_upper_bound(t))
1060 if(!is_lower_bound(t))
1070 const Qfloat *Q_ip = NULL;
1071 const Qfloat *Q_in = NULL;
1073 Q_ip = Q->get_Q(ip,active_size);
1075 Q_in = Q->get_Q(in,active_size);
1077 for(
int j=0;j<active_size;j++)
1081 if (!is_lower_bound(j))
1083 double grad_diff=Gmaxp+G[j];
1089 double quad_coef = QD[ip]+QD[j]-2*Q_ip[j];
1091 obj_diff = -(grad_diff*grad_diff)/quad_coef;
1093 obj_diff = -(grad_diff*grad_diff)/
TAU;
1095 if (obj_diff <= obj_diff_min)
1098 obj_diff_min = obj_diff;
1105 if (!is_upper_bound(j))
1107 double grad_diff=Gmaxn-G[j];
1108 if (-G[j] >= Gmaxn2)
1113 double quad_coef = QD[in]+QD[j]-2*Q_in[j];
1115 obj_diff = -(grad_diff*grad_diff)/quad_coef;
1117 obj_diff = -(grad_diff*grad_diff)/
TAU;
1119 if (obj_diff <= obj_diff_min)
1122 obj_diff_min = obj_diff;
1129 if(
max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) < eps)
1132 if (
y[Gmin_idx] == +1)
1143 if(is_upper_bound(i))
1146 return(-G[i] > Gmax1);
1148 return(-G[i] > Gmax4);
1150 else if(is_lower_bound(i))
1153 return(G[i] > Gmax2);
1155 return(G[i] > Gmax3);
1163 double Gmax1 = -
INF;
1164 double Gmax2 = -
INF;
1165 double Gmax3 = -
INF;
1166 double Gmax4 = -
INF;
1170 for(i=0;i<active_size;i++)
1172 if(!is_upper_bound(i))
1176 if(-G[i] > Gmax1) Gmax1 = -G[i];
1178 else if(-G[i] > Gmax4) Gmax4 = -G[i];
1180 if(!is_lower_bound(i))
1184 if(G[i] > Gmax2) Gmax2 = G[i];
1186 else if(G[i] > Gmax3) Gmax3 = G[i];
1190 if(unshrink ==
false &&
max(Gmax1+Gmax2,Gmax3+Gmax4) <= eps*10)
1193 reconstruct_gradient();
1197 for(i=0;i<active_size;i++)
1198 if (be_shrunk(i, Gmax1, Gmax2, Gmax3, Gmax4))
1201 while (active_size > i)
1203 if (!be_shrunk(active_size, Gmax1, Gmax2, Gmax3, Gmax4))
1215 int nr_free1 = 0,nr_free2 = 0;
1216 double ub1 =
INF, ub2 =
INF;
1217 double lb1 = -
INF, lb2 = -
INF;
1218 double sum_free1 = 0, sum_free2 = 0;
1220 for(
int i=0;i<active_size;i++)
1224 if(is_upper_bound(i))
1225 lb1 =
max(lb1,G[i]);
1226 else if(is_lower_bound(i))
1227 ub1 =
min(ub1,G[i]);
1236 if(is_upper_bound(i))
1237 lb2 =
max(lb2,G[i]);
1238 else if(is_lower_bound(i))
1239 ub2 =
min(ub2,G[i]);
1250 r1 = sum_free1/nr_free1;
1255 r2 = sum_free2/nr_free2;
1270 :
Kernel(prob.l, prob.
x, param)
1274 QD =
new double[prob.
l];
1275 for(
int i=0;i<prob.
l;i++)
1283 if((start = cache->get_data(i,&data,len)) < len)
1285 for(j=start;j<len;j++)
1298 cache->swap_index(i,j);
1320 :
Kernel(prob.l, prob.
x, param)
1323 QD =
new double[prob.
l];
1324 for(
int i=0;i<prob.
l;i++)
1332 if((start = cache->get_data(i,&data,len)) < len)
1334 for(j=start;j<len;j++)
1347 cache->swap_index(i,j);
1366 :
Kernel(prob.l, prob.
x, param)
1370 QD =
new double[2*l];
1371 sign =
new schar[2*l];
1372 index =
new int[2*l];
1373 for(
int k=0;k<l;k++)
1389 swap(sign[i],sign[j]);
1397 int j, real_i =
index[i];
1398 if(cache->get_data(real_i,&data,l) < l)
1406 next_buffer = 1 - next_buffer;
1445 double *minus_ones =
new double[l];
1454 if(prob->
y[i] > 0) y[i] = +1;
else y[i] = -1;
1458 s.
Solve(l,
SVC_Q(*prob,*param,y), minus_ones, y,
1463 sum_alpha += alpha[i];
1466 info(
"nu = %f\n", sum_alpha/(Cp*prob->
l));
1471 delete[] minus_ones;
1481 double nu = param->
nu;
1491 double sum_pos = nu*l/2;
1492 double sum_neg = nu*l/2;
1497 alpha[i] =
min(1.0,sum_pos);
1498 sum_pos -= alpha[i];
1502 alpha[i] =
min(1.0,sum_neg);
1503 sum_neg -= alpha[i];
1506 double *zeros =
new double[l];
1516 info(
"C = %f\n",1/r);
1535 double *zeros =
new double[l];
1539 int n = (int)(param->
nu*prob->
l);
1544 alpha[n] = param->
nu * prob->
l - n;
1567 double *alpha2 =
new double[2*l];
1568 double *linear_term =
new double[2*l];
1575 linear_term[i] = param->
p - prob->
y[i];
1579 linear_term[i+l] = param->
p + prob->
y[i];
1584 s.
Solve(2*l,
SVR_Q(*prob,*param), linear_term, y,
1587 double sum_alpha = 0;
1590 alpha[i] = alpha2[i] - alpha2[i+l];
1591 sum_alpha += fabs(alpha[i]);
1593 info(
"nu = %f\n",sum_alpha/(param->
C*l));
1596 delete[] linear_term;
1605 double C = param->
C;
1606 double *alpha2 =
new double[2*l];
1607 double *linear_term =
new double[2*l];
1611 double sum = C * param->
nu * l / 2;
1614 alpha2[i] = alpha2[i+l] =
min(sum,C);
1617 linear_term[i] = - prob->
y[i];
1620 linear_term[i+l] = prob->
y[i];
1625 s.
Solve(2*l,
SVR_Q(*prob,*param), linear_term, y,
1628 info(
"epsilon = %f\n",-si->
r);
1631 alpha[i] = alpha2[i] - alpha2[i+l];
1634 delete[] linear_term;
1649 double Cp,
double Cn)
1651 double *alpha =
Malloc(
double,prob->
l);
1678 for(
int i=0;i<prob->
l;i++)
1680 if(fabs(alpha[i]) > 0)
1696 info(
"nSV = %d, nBSV = %d\n",nSV,nBSV);
1706 int l,
const double *dec_values,
const double *labels,
1707 double& A,
double& B)
1709 double prior1=0, prior0 = 0;
1713 if (labels[i] > 0) prior1+=1;
1717 double min_step=1e-10;
1720 double hiTarget=(prior1+1.0)/(prior1+2.0);
1721 double loTarget=1/(prior0+2.0);
1722 double *t=
Malloc(
double,l);
1723 double fApB,p,q,h11,h22,h21,g1,g2,det,dA,dB,gd,stepsize;
1724 double newA,newB,newf,d1,d2;
1728 A=0.0; B=log((prior0+1.0)/(prior1+1.0));
1733 if (labels[i]>0) t[i]=hiTarget;
1735 fApB = dec_values[i]*A+B;
1737 fval += t[i]*fApB + log(1+exp(-fApB));
1739 fval += (t[i] - 1)*fApB +log(1+exp(fApB));
1741 for (iter=0;iter<max_iter;iter++)
1746 h21=0.0;g1=0.0;g2=0.0;
1749 fApB = dec_values[i]*A+B;
1752 p=exp(-fApB)/(1.0+exp(-fApB));
1753 q=1.0/(1.0+exp(-fApB));
1757 p=1.0/(1.0+exp(fApB));
1758 q=exp(fApB)/(1.0+exp(fApB));
1761 h11+=dec_values[i]*dec_values[i]*d2;
1763 h21+=dec_values[i]*d2;
1765 g1+=dec_values[i]*d1;
1770 if (fabs(g1)<eps && fabs(g2)<eps)
1774 det=h11*h22-h21*h21;
1775 dA=-(h22*g1 - h21 * g2) / det;
1776 dB=-(-h21*g1+ h11 * g2) / det;
1781 while (stepsize >= min_step)
1783 newA = A + stepsize * dA;
1784 newB = B + stepsize * dB;
1790 fApB = dec_values[i]*newA+newB;
1792 newf += t[i]*fApB + log(1+exp(-fApB));
1794 newf += (t[i] - 1)*fApB +log(1+exp(fApB));
1797 if (newf<fval+0.0001*stepsize*gd)
1799 A=newA;B=newB;fval=newf;
1803 stepsize = stepsize / 2.0;
1806 if (stepsize < min_step)
1808 info(
"Line search fails in two-class probability estimates\n");
1814 info(
"Reaching maximal iterations in two-class probability estimates\n");
1820 double fApB = decision_value*A+B;
1823 return exp(-fApB)/(1.0+exp(-fApB));
1825 return 1.0/(1+exp(fApB)) ;
1832 int iter = 0, max_iter=
max(100,k);
1833 double **Q=
Malloc(
double *,k);
1834 double *Qp=
Malloc(
double,k);
1835 double pQp, eps=0.005/k;
1844 Q[t][t]+=r[j][t]*r[j][t];
1849 Q[t][t]+=r[j][t]*r[j][t];
1850 Q[t][j]=-r[j][t]*r[t][j];
1853 for (iter=0;iter<max_iter;iter++)
1861 Qp[t]+=Q[t][j]*p[j];
1867 double error=fabs(Qp[t]-pQp);
1868 if (error>max_error)
1871 if (max_error<eps)
break;
1875 double diff=(-Qp[t]+pQp)/Q[t][t];
1877 pQp=(pQp+diff*(diff*Q[t][t]+2*Qp[t]))/(1+diff)/(1+diff);
1880 Qp[j]=(Qp[j]+diff*Q[t][j])/(1+diff);
1886 info(
"Exceeds max_iter in multiclass_prob\n");
1887 for(t=0;t<k;t++) free(Q[t]);
1895 double Cp,
double Cn,
double& probA,
double& probB)
1899 int *perm =
Malloc(
int,prob->
l);
1900 double *dec_values =
Malloc(
double,prob->
l);
1903 for(i=0;i<prob->
l;i++) perm[i]=i;
1904 for(i=0;i<prob->
l;i++)
1906 int j = i+rand()%(prob->
l-i);
1907 swap(perm[i],perm[j]);
1912 int end = (i+1)*prob->
l/nr_fold;
1916 subprob.
l = prob->
l-(end-begin);
1918 subprob.y =
Malloc(
double,subprob.l);
1921 for(j=0;j<begin;j++)
1923 subprob.x[k] = prob->
x[perm[j]];
1924 subprob.y[k] = prob->
y[perm[j]];
1927 for(j=end;j<prob->
l;j++)
1929 subprob.x[k] = prob->
x[perm[j]];
1930 subprob.y[k] = prob->
y[perm[j]];
1933 int p_count=0,n_count=0;
1940 if(p_count==0 && n_count==0)
1941 for(j=begin;j<end;j++)
1942 dec_values[perm[j]] = 0;
1943 else if(p_count > 0 && n_count == 0)
1944 for(j=begin;j<end;j++)
1945 dec_values[perm[j]] = 1;
1946 else if(p_count == 0 && n_count > 0)
1947 for(j=begin;j<end;j++)
1948 dec_values[perm[j]] = -1;
1962 for(j=begin;j<end;j++)
1966 dec_values[perm[j]] *= submodel->
label[0];
1985 double *ymv =
Malloc(
double,prob->
l);
1991 for(i=0;i<prob->
l;i++)
1993 ymv[i]=prob->
y[i]-ymv[i];
1994 mae += fabs(ymv[i]);
1997 double std=sqrt(2*mae*mae);
2000 for(i=0;i<prob->
l;i++)
2001 if (fabs(ymv[i]) > 5*std)
2005 mae /= (prob->
l-count);
2006 info(
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma= %g\n",mae);
2017 int max_nr_class = 16;
2020 int *count =
Malloc(
int,max_nr_class);
2021 int *data_label =
Malloc(
int,l);
2026 int this_label = (int)prob->
y[i];
2028 for(j=0;j<nr_class;j++)
2030 if(this_label == label[j])
2039 if(nr_class == max_nr_class)
2042 label = (
int *)realloc(label,max_nr_class*
sizeof(
int));
2043 count = (
int *)realloc(count,max_nr_class*
sizeof(
int));
2051 int *start =
Malloc(
int,nr_class);
2054 start[i] = start[i-1]+count[i-1];
2057 perm[start[data_label[i]]] = i;
2058 ++start[data_label[i]];
2062 start[i] = start[i-1]+count[i-1];
2086 model->
label = NULL;
2105 for(i=0;i<prob->
l;i++)
2111 for(i=0;i<prob->
l;i++)
2112 if(fabs(f.
alpha[i]) > 0)
2114 model->
SV[j] = prob->
x[i];
2129 int *perm =
Malloc(
int,l);
2134 info(
"WARNING: training data in only one class. See README for details.\n");
2139 x[i] = prob->
x[perm[i]];
2143 double *weighted_C =
Malloc(
double, nr_class);
2145 weighted_C[i] = param->
C;
2146 for(i=0;i<param->nr_weight;i++)
2153 fprintf(stderr,
"WARNING: class label %d specified in weight is not found\n", param->
weight_label[i]);
2155 weighted_C[j] *= param->
weight[i];
2160 bool *nonzero =
Malloc(
bool,l);
2168 probA=
Malloc(
double,nr_class*(nr_class-1)/2);
2169 probB=
Malloc(
double,nr_class*(nr_class-1)/2);
2177 int si = start[i], sj = start[j];
2178 int ci = count[i], cj = count[j];
2181 sub_prob.
y =
Malloc(
double,sub_prob.
l);
2185 sub_prob.
x[k] = x[si+k];
2190 sub_prob.
x[ci+k] = x[sj+k];
2191 sub_prob.
y[ci+k] = -1;
2197 f[p] =
svm_train_one(&sub_prob,param,weighted_C[i],weighted_C[j]);
2199 if(!nonzero[si+k] && fabs(f[p].alpha[k]) > 0)
2200 nonzero[si+k] =
true;
2202 if(!nonzero[sj+k] && fabs(f[p].alpha[ci+k]) > 0)
2203 nonzero[sj+k] =
true;
2215 model->
label[i] = label[i];
2217 model->
rho =
Malloc(
double,nr_class*(nr_class-1)/2);
2218 for(i=0;i<nr_class*(nr_class-1)/2;i++)
2219 model->
rho[i] = f[i].
rho;
2223 model->
probA =
Malloc(
double,nr_class*(nr_class-1)/2);
2224 model->
probB =
Malloc(
double,nr_class*(nr_class-1)/2);
2225 for(i=0;i<nr_class*(nr_class-1)/2;i++)
2227 model->
probA[i] = probA[i];
2228 model->
probB[i] = probB[i];
2238 int *nz_count =
Malloc(
int,nr_class);
2243 for(
int j=0;j<count[i];j++)
2244 if(nonzero[start[i]+j])
2253 info(
"Total nSV = %d\n",total_sv);
2255 model->
l = total_sv;
2259 if(nonzero[i]) model->
SV[p++] = x[i];
2261 int *nz_start =
Malloc(
int,nr_class);
2264 nz_start[i] = nz_start[i-1]+nz_count[i-1];
2267 for(i=0;i<nr_class-1;i++)
2283 int q = nz_start[i];
2304 for(i=0;i<nr_class*(nr_class-1)/2;i++)
2317 int *fold_start =
Malloc(
int,nr_fold+1);
2319 int *perm =
Malloc(
int,l);
2333 int *fold_count =
Malloc(
int,nr_fold);
2339 for(i=0;i<count[
c];i++)
2341 int j = i+rand()%(count[
c]-i);
2342 swap(index[start[c]+j],index[start[c]+i]);
2348 fold_count[i]+=(i+1)*count[
c]/nr_fold-i*count[
c]/
nr_fold;
2352 fold_start[i] = fold_start[i-1]+fold_count[i-1];
2356 int begin = start[
c]+i*count[
c]/
nr_fold;
2357 int end = start[
c]+(i+1)*count[c]/nr_fold;
2358 for(
int j=begin;j<end;j++)
2360 perm[fold_start[i]] = index[j];
2366 fold_start[i] = fold_start[i-1]+fold_count[i-1];
2375 for(i=0;i<
l;i++) perm[i]=i;
2378 int j = i+rand()%(l-i);
2379 swap(perm[i],perm[j]);
2382 fold_start[i]=i*l/nr_fold;
2387 int begin = fold_start[i];
2388 int end = fold_start[i+1];
2392 subprob.
l = l-(end-begin);
2394 subprob.
y =
Malloc(
double,subprob.
l);
2397 for(j=0;j<begin;j++)
2399 subprob.
x[k] = prob->
x[perm[j]];
2400 subprob.
y[k] = prob->
y[perm[j]];
2405 subprob.
x[k] = prob->
x[perm[j]];
2406 subprob.
y[k] = prob->
y[perm[j]];
2414 for(j=begin;j<end;j++)
2416 free(prob_estimates);
2419 for(j=begin;j<end;j++)
2420 target[perm[j]] =
svm_predict(submodel,prob->
x[perm[j]]);
2442 if (model->
label != NULL)
2444 label[i] = model->
label[i];
2451 return model->
probA[0];
2454 fprintf(stderr,
"Model doesn't contain information for SVR probability inference\n");
2468 for(i=0;i<model->
l;i++)
2470 sum -= model->
rho[0];
2474 return (sum>0)?1:-1;
2483 double *kvalue =
Malloc(
double,l);
2487 int *start =
Malloc(
int,nr_class);
2490 start[i] = start[i-1]+model->
nSV[i-1];
2492 int *vote =
Malloc(
int,nr_class);
2503 int ci = model->
nSV[i];
2504 int cj = model->
nSV[j];
2507 double *coef1 = model->
sv_coef[j-1];
2508 double *coef2 = model->
sv_coef[i];
2510 sum += coef1[si+k] * kvalue[si+k];
2512 sum += coef2[sj+k] * kvalue[sj+k];
2513 sum -= model->
rho[p];
2514 dec_values[p] = sum;
2516 if(dec_values[p] > 0)
2523 int vote_max_idx = 0;
2525 if(vote[i] > vote[vote_max_idx])
2531 return model->
label[vote_max_idx];
2542 dec_values =
Malloc(
double, 1);
2544 dec_values =
Malloc(
double, nr_class*(nr_class-1)/2);
2558 double *dec_values =
Malloc(
double, nr_class*(nr_class-1)/2);
2561 double min_prob=1e-7;
2562 double **pairwise_prob=
Malloc(
double *,nr_class);
2564 pairwise_prob[i]=
Malloc(
double,nr_class);
2570 pairwise_prob[j][i]=1-pairwise_prob[i][j];
2575 int prob_max_idx = 0;
2577 if(prob_estimates[i] > prob_estimates[prob_max_idx])
2580 free(pairwise_prob[i]);
2582 free(pairwise_prob);
2583 return model->
label[prob_max_idx];
2591 "c_svc",
"nu_svc",
"one_class",
"epsilon_svr",
"nu_svr",NULL
2596 "linear",
"polynomial",
"rbf",
"sigmoid",
"precomputed",NULL
2601 FILE *fp = fopen(model_file_name,
"w");
2602 if(fp==NULL)
return -1;
2604 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2605 setlocale(LC_ALL,
"C");
2613 fprintf(fp,
"degree %d\n", param.
degree);
2616 fprintf(fp,
"gamma %g\n", param.
gamma);
2619 fprintf(fp,
"coef0 %g\n", param.
coef0);
2623 fprintf(fp,
"nr_class %d\n", nr_class);
2624 fprintf(fp,
"total_sv %d\n",l);
2628 for(
int i=0;i<nr_class*(nr_class-1)/2;i++)
2629 fprintf(fp,
" %g",model->
rho[i]);
2635 fprintf(fp,
"label");
2637 fprintf(fp,
" %d",model->
label[i]);
2643 fprintf(fp,
"probA");
2644 for(
int i=0;i<nr_class*(nr_class-1)/2;i++)
2645 fprintf(fp,
" %g",model->
probA[i]);
2650 fprintf(fp,
"probB");
2651 for(
int i=0;i<nr_class*(nr_class-1)/2;i++)
2652 fprintf(fp,
" %g",model->
probB[i]);
2658 fprintf(fp,
"nr_sv");
2660 fprintf(fp,
" %d",model->
nSV[i]);
2664 fprintf(fp,
"SV\n");
2668 for(
int i=0;i<
l;i++)
2670 for(
int j=0;j<nr_class-1;j++)
2671 fprintf(fp,
"%.16g ",sv_coef[j][i]);
2676 fprintf(fp,
"0:%d ",(
int)(p->
value));
2678 while(p->
index != -1)
2686 setlocale(LC_ALL, old_locale);
2689 if (ferror(fp) != 0 || fclose(fp) != 0)
return -1;
2703 while(strrchr(
line,
'\n') == NULL)
2707 len = (int) strlen(
line);
2716 FILE *fp = fopen(model_file_name,
"rb");
2717 if(fp==NULL)
return NULL;
2719 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2720 setlocale(LC_ALL,
"C");
2727 model->
probA = NULL;
2728 model->
probB = NULL;
2729 model->
label = NULL;
2735 fscanf(fp,
"%80s",cmd);
2737 if(strcmp(cmd,
"svm_type")==0)
2739 fscanf(fp,
"%80s",cmd);
2751 fprintf(stderr,
"unknown svm type.\n");
2753 setlocale(LC_ALL, old_locale);
2762 else if(strcmp(cmd,
"kernel_type")==0)
2764 fscanf(fp,
"%80s",cmd);
2776 fprintf(stderr,
"unknown kernel function.\n");
2778 setlocale(LC_ALL, old_locale);
2787 else if(strcmp(cmd,
"degree")==0)
2788 fscanf(fp,
"%d",¶m.
degree);
2789 else if(strcmp(cmd,
"gamma")==0)
2790 fscanf(fp,
"%lf",¶m.
gamma);
2791 else if(strcmp(cmd,
"coef0")==0)
2792 fscanf(fp,
"%lf",¶m.
coef0);
2793 else if(strcmp(cmd,
"nr_class")==0)
2795 else if(strcmp(cmd,
"total_sv")==0)
2796 fscanf(fp,
"%d",&model->
l);
2797 else if(strcmp(cmd,
"rho")==0)
2801 for(
int i=0;i<n;i++)
2802 fscanf(fp,
"%lf",&model->
rho[i]);
2804 else if(strcmp(cmd,
"label")==0)
2808 for(
int i=0;i<n;i++)
2809 fscanf(fp,
"%d",&model->
label[i]);
2811 else if(strcmp(cmd,
"probA")==0)
2815 for(
int i=0;i<n;i++)
2816 fscanf(fp,
"%lf",&model->
probA[i]);
2818 else if(strcmp(cmd,
"probB")==0)
2822 for(
int i=0;i<n;i++)
2823 fscanf(fp,
"%lf",&model->
probB[i]);
2825 else if(strcmp(cmd,
"nr_sv")==0)
2829 for(
int i=0;i<n;i++)
2830 fscanf(fp,
"%d",&model->
nSV[i]);
2832 else if(strcmp(cmd,
"SV")==0)
2837 if(c==EOF || c==
'\n')
break;
2843 fprintf(stderr,
"unknown text in model file: [%s]\n",cmd);
2845 setlocale(LC_ALL, old_locale);
2858 long pos = ftell(fp);
2862 char *p,*endptr,*idx,*val;
2866 p = strtok(
line,
":");
2869 p = strtok(NULL,
":");
2875 elements += model->
l;
2877 fseek(fp,pos,SEEK_SET);
2893 model->
SV[i] = &x_space[j];
2895 p = strtok(
line,
" \t");
2896 model->
sv_coef[0][i] = strtod(p,&endptr);
2897 for(
int k=1;k<m;k++)
2899 p = strtok(NULL,
" \t");
2900 model->
sv_coef[k][i] = strtod(p,&endptr);
2905 idx = strtok(NULL,
":");
2906 val = strtok(NULL,
" \t");
2910 x_space[j].
index = (int) strtol(idx,&endptr,10);
2911 x_space[j].
value = strtod(val,&endptr);
2915 x_space[j++].
index = -1;
2919 setlocale(LC_ALL, old_locale);
2922 if (ferror(fp) != 0 || fclose(fp) != 0)
2931 if(model_ptr->
free_sv && model_ptr->
l > 0 && model_ptr->
SV != NULL)
2932 free((
void *)(model_ptr->
SV[0]));
2935 for(
int i=0;i<model_ptr->
nr_class-1;i++)
2939 free(model_ptr->
SV);
2940 model_ptr->
SV = NULL;
2945 free(model_ptr->
rho);
2946 model_ptr->
rho = NULL;
2948 free(model_ptr->
label);
2949 model_ptr->
label= NULL;
2951 free(model_ptr->
probA);
2952 model_ptr->
probA = NULL;
2954 free(model_ptr->
probB);
2955 model_ptr->
probB= NULL;
2957 free(model_ptr->
nSV);
2958 model_ptr->
nSV = NULL;
2963 if(model_ptr_ptr != NULL && *model_ptr_ptr != NULL)
2966 free(*model_ptr_ptr);
2967 *model_ptr_ptr = NULL;
2982 if(svm_type !=
C_SVC &&
2987 return "unknown svm type";
2992 if(kernel_type !=
LINEAR &&
2993 kernel_type !=
POLY &&
2994 kernel_type !=
RBF &&
2997 return "unknown kernel type";
2999 if(param->
gamma < 0)
3003 return "degree of polynomial kernel < 0";
3008 return "cache_size <= 0";
3013 if(svm_type ==
C_SVC ||
3022 if(param->
nu <= 0 || param->
nu > 1)
3023 return "nu <= 0 or nu > 1";
3031 return "shrinking != 0 and shrinking != 1";
3035 return "probability != 0 and probability != 1";
3039 return "one-class SVM probability output not supported yet";
3047 int max_nr_class = 16;
3050 int *count =
Malloc(
int,max_nr_class);
3055 int this_label = (int)prob->
y[i];
3057 for(j=0;j<nr_class;j++)
3058 if(this_label == label[j])
3065 if(nr_class == max_nr_class)
3068 label = (
int *)realloc(label,max_nr_class*
sizeof(
int));
3069 count = (
int *)realloc(count,max_nr_class*
sizeof(
int));
3083 if(param->
nu*(n1+n2)/2 >
min(n1,n2))
3087 return "specified nu is infeasible";
3103 model->
probA!=NULL);
3108 if(print_func == NULL)
int svm_save_model(const char *model_file_name, const svm_model *model)
svm_model * svm_train(const svm_problem *prob, const svm_parameter *param)
Qfloat * get_Q(int i, int len) const
static void svm_group_classes(const svm_problem *prob, int *nr_class_ret, int **label_ret, int **start_ret, int **count_ret, int *perm)
static void solve_one_class(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
int get_data(const int index, Qfloat **data, int len)
void swap_index(int i, int j) const
void svm_free_model_content(svm_model *model_ptr)
void Solve(int l, const QMatrix &Q, const double *p, const schar *y, double *alpha, double Cp, double Cn, double eps, SolutionInfo *si, int shrinking)
const char * svm_check_parameter(const svm_problem *prob, const svm_parameter *param)
static double dot(const svm_node *px, const svm_node *py)
struct svm_parameter param
void swap_index(int i, int j)
ONE_CLASS_Q(const svm_problem &prob, const svm_parameter ¶m)
double kernel_sigmoid(int i, int j) const
virtual Qfloat * get_Q(int column, int len) const =0
static double sigmoid_predict(double decision_value, double A, double B)
int select_working_set(int &i, int &j)
static void solve_epsilon_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
Qfloat * get_Q(int i, int len) const
static const char * kernel_type_table[]
void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target)
TFSIMD_FORCE_INLINE const tfScalar & y() const
static void sigmoid_train(int l, const double *dec_values, const double *labels, double &A, double &B)
double svm_get_svr_probability(const svm_model *model)
virtual int select_working_set(int &i, int &j)
double svm_predict(const svm_model *model, const svm_node *x)
static void info(const char *fmt,...)
static void print_string_stdout(const char *s)
int svm_get_nr_class(const svm_model *model)
Kernel(int l, svm_node *const *x, const svm_parameter ¶m)
int svm_check_probability_model(const svm_model *model)
static void solve_nu_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
static void clone(T *&dst, S *src, int n)
bool is_upper_bound(int i)
void swap_index(int i, int j) const
static void(* svm_print_string)(const char *)
static void svm_binary_svc_probability(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, double &probA, double &probB)
void lru_insert(head_t *h)
static char * readline(FILE *input)
static double powi(double base, int times)
double(Kernel::* kernel_function)(int i, int j) const
double kernel_linear(int i, int j) const
virtual void swap_index(int i, int j) const
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
double svm_predict_values(const svm_model *model, const svm_node *x, double *dec_values)
double svm_predict_probability(const svm_model *model, const svm_node *x, double *prob_estimates)
void update_alpha_status(int i)
struct svm_parameter param
TFSIMD_FORCE_INLINE tfScalar dot(const Quaternion &q1, const Quaternion &q2)
static double svm_svr_probability(const svm_problem *prob, const svm_parameter *param)
SVR_Q(const svm_problem &prob, const svm_parameter ¶m)
void lru_delete(head_t *h)
void swap_index(int i, int j) const
virtual double * get_QD() const =0
static decision_function svm_train_one(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn)
void swap_index(int i, int j)
SVC_Q(const svm_problem &prob, const svm_parameter ¶m, const schar *y_)
static void multiclass_probability(int k, double **r, double *p)
static void swap(T &x, T &y)
int svm_get_svm_type(const svm_model *model)
virtual void do_shrinking()
double kernel_rbf(int i, int j) const
static double k_function(const svm_node *x, const svm_node *y, const svm_parameter ¶m)
double kernel_poly(int i, int j) const
static void solve_nu_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
bool is_lower_bound(int i)
Qfloat * get_Q(int i, int len) const
static const char * svm_type_table[]
void Solve(int l, const QMatrix &Q, const double *p_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, int shrinking)
bool be_shrunk(int i, double Gmax1, double Gmax2)
virtual double calculate_rho()
static void solve_c_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double Cp, double Cn)
svm_model * svm_load_model(const char *model_file_name)
void svm_destroy_param(svm_parameter *param)
struct svm_node * x_space
double kernel_precomputed(int i, int j) const
void svm_get_labels(const svm_model *model, int *label)
bool be_shrunk(int i, double Gmax1, double Gmax2, double Gmax3, double Gmax4)
void reconstruct_gradient()
Cache(int l, long int size)
void svm_set_print_string_function(void(*print_func)(const char *))