49 template <
class T>
static inline T
min(T x,T y) {
return (x<y)?x:y; }
52 template <
class T>
static inline T
max(T x,T y) {
return (x>y)?x:y; }
54 template <
class T>
static inline void swap(T& x, T& y) { T t=x; x=y; y=t; }
55 template <
class S,
class T>
static inline void clone(T*& dst, S* src,
int n)
58 memcpy((
void *)dst,(
void *)src,
sizeof(T)*n);
60 static inline double powi(
double base,
int times)
62 double tmp = base, ret = 1.0;
64 for(
int t=times; t>0; t/=2)
73 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
82 static void info(
const char *fmt,...)
89 (*svm_print_string)(buf);
92 static void info(
const char *fmt,...) {}
164 int more = len - h->
len;
207 swap(h->data[i],h->data[j]);
230 virtual Qfloat *
get_Q(
int column,
int len)
const = 0;
231 virtual double *
get_QD()
const = 0;
232 virtual void swap_index(
int i,
int j)
const = 0;
244 virtual Qfloat *
get_Q(
int column,
int len)
const = 0;
245 virtual double *
get_QD()
const = 0;
268 return dot(
x[i],
x[j]);
284 return x[i][(int)(
x[j][0].value)].
value;
295 :kernel_type(param.kernel_type), degree(param.degree),
296 gamma(param.gamma), coef0(param.coef0)
399 while(y->
index != -1)
405 return exp(-param.
gamma*sum);
447 double *alpha_,
const double* C_,
double eps,
475 else if(
alpha[i] <= 0)
488 bool be_shrunk(
int i,
double Gmax1,
double Gmax2);
521 info(
"\nWARNING: using -h 0 may be faster\n");
530 G[i] +=
alpha[j] * Q_i[j];
539 double alpha_i =
alpha[i];
541 G[j] += alpha_i * Q_i[j];
547 double *alpha_,
const double* C_,
double eps,
589 double alpha_i =
alpha[i];
592 G[j] += alpha_i*Q_i[j];
602 int max_iter =
max(10000000,
l>INT_MAX/100 ? INT_MAX : 100*
l);
603 int counter =
min(
l,1000)+1;
605 while(iter < max_iter)
611 counter =
min(
l,1000);
637 double C_i =
get_C(i);
638 double C_j =
get_C(j);
640 double old_alpha_i =
alpha[i];
641 double old_alpha_j =
alpha[j];
645 double quad_coef =
QD[i]+
QD[j]+2*Q_i[j];
648 double delta = (-
G[i]-
G[j])/quad_coef;
674 alpha[j] = C_i - diff;
682 alpha[i] = C_j + diff;
688 double quad_coef =
QD[i]+
QD[j]-2*Q_i[j];
691 double delta = (
G[i]-
G[j])/quad_coef;
701 alpha[j] = sum - C_i;
717 alpha[i] = sum - C_j;
732 double delta_alpha_i =
alpha[i] - old_alpha_i;
733 double delta_alpha_j =
alpha[j] - old_alpha_j;
737 G[k] += Q_i[k]*delta_alpha_i + Q_j[k]*delta_alpha_j;
753 G_bar[k] -= C_i * Q_i[k];
756 G_bar[k] += C_i * Q_i[k];
764 G_bar[k] -= C_j * Q_j[k];
767 G_bar[k] += C_j * Q_j[k];
781 fprintf(stderr,
"\nWARNING: reaching max number of iterations\n");
815 info(
"\noptimization finished, #iter = %d\n",iter);
840 double obj_diff_min =
INF;
873 double grad_diff=Gmax+
G[j];
879 double quad_coef =
QD[i]+
QD[j]-2.0*
y[i]*Q_i[j];
881 obj_diff = -(grad_diff*grad_diff)/quad_coef;
883 obj_diff = -(grad_diff*grad_diff)/
TAU;
885 if (obj_diff <= obj_diff_min)
888 obj_diff_min = obj_diff;
897 double grad_diff= Gmax-
G[j];
903 double quad_coef =
QD[i]+
QD[j]+2.0*
y[i]*Q_i[j];
905 obj_diff = -(grad_diff*grad_diff)/quad_coef;
907 obj_diff = -(grad_diff*grad_diff)/
TAU;
909 if (obj_diff <= obj_diff_min)
912 obj_diff_min = obj_diff;
932 return(-
G[i] > Gmax1);
934 return(-
G[i] > Gmax2);
939 return(
G[i] > Gmax2);
941 return(
G[i] > Gmax1);
1012 double ub =
INF, lb = -
INF, sum_free = 0;
1015 double yG =
y[i]*
G[i];
1039 r = sum_free/nr_free;
1056 double *
alpha,
double* C_,
double eps,
1057 SolutionInfo*
si,
int shrinking)
1066 bool be_shrunk(
int i,
double Gmax1,
double Gmax2,
double Gmax3,
double Gmax4);
1079 double Gmaxp = -
INF;
1080 double Gmaxp2 = -
INF;
1083 double Gmaxn = -
INF;
1084 double Gmaxn2 = -
INF;
1088 double obj_diff_min =
INF;
1112 const Qfloat *Q_ip = NULL;
1113 const Qfloat *Q_in = NULL;
1125 double grad_diff=Gmaxp+
G[j];
1131 double quad_coef =
QD[ip]+
QD[j]-2*Q_ip[j];
1133 obj_diff = -(grad_diff*grad_diff)/quad_coef;
1135 obj_diff = -(grad_diff*grad_diff)/
TAU;
1137 if (obj_diff <= obj_diff_min)
1140 obj_diff_min = obj_diff;
1149 double grad_diff=Gmaxn-
G[j];
1150 if (-
G[j] >= Gmaxn2)
1155 double quad_coef =
QD[in]+
QD[j]-2*Q_in[j];
1157 obj_diff = -(grad_diff*grad_diff)/quad_coef;
1159 obj_diff = -(grad_diff*grad_diff)/
TAU;
1161 if (obj_diff <= obj_diff_min)
1164 obj_diff_min = obj_diff;
1171 if(
max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) <
eps)
1174 if (
y[Gmin_idx] == +1)
1188 return(-
G[i] > Gmax1);
1190 return(-
G[i] > Gmax4);
1195 return(
G[i] > Gmax2);
1197 return(
G[i] > Gmax3);
1205 double Gmax1 = -
INF;
1206 double Gmax2 = -
INF;
1207 double Gmax3 = -
INF;
1208 double Gmax4 = -
INF;
1218 if(-
G[i] > Gmax1) Gmax1 = -
G[i];
1220 else if(-
G[i] > Gmax4) Gmax4 = -
G[i];
1226 if(
G[i] > Gmax2) Gmax2 =
G[i];
1228 else if(
G[i] > Gmax3) Gmax3 =
G[i];
1240 if (
be_shrunk(i, Gmax1, Gmax2, Gmax3, Gmax4))
1257 int nr_free1 = 0,nr_free2 = 0;
1258 double ub1 =
INF, ub2 =
INF;
1259 double lb1 = -
INF, lb2 = -
INF;
1260 double sum_free1 = 0, sum_free2 = 0;
1267 lb1 =
max(lb1,
G[i]);
1269 ub1 =
min(ub1,
G[i]);
1279 lb2 =
max(lb2,
G[i]);
1281 ub2 =
min(ub2,
G[i]);
1292 r1 = sum_free1/nr_free1;
1297 r2 = sum_free2/nr_free2;
1312 :
Kernel(prob.l, prob.
x, param)
1316 QD =
new double[prob.
l];
1317 for(
int i=0;i<prob.
l;i++)
1327 for(j=start;j<len;j++)
1362 :
Kernel(prob.l, prob.
x, param)
1365 QD =
new double[prob.
l];
1366 for(
int i=0;i<prob.
l;i++)
1376 for(j=start;j<len;j++)
1412 QD =
new double[2*
l];
1415 for(
int k=0;k<
l;k++)
1439 int j, real_i =
index[i];
1487 double *minus_ones =
new double[l];
1489 double *C =
new double[l];
1500 C[i] = prob->
W[i]*Cp;
1505 C[i] = prob->
W[i]*Cn;
1512 s.
Solve(l,
SVC_Q(*prob,*param,y), minus_ones, y,
1527 delete[] minus_ones;
1537 double nu = param->
nu;
1540 double *C =
new double[l];
1552 for(i=0;i<l;i++) nu_l += nu*C[i];
1553 double sum_pos = nu_l/2;
1554 double sum_neg = nu_l/2;
1559 alpha[i] =
min(C[i],sum_pos);
1560 sum_pos -= alpha[i];
1564 alpha[i] =
min(C[i],sum_neg);
1565 sum_neg -= alpha[i];
1568 double *zeros =
new double[l];
1578 info(
"C = %f\n",1/
r);
1599 double *zeros =
new double[l];
1601 double *C =
new double[l];
1609 nu_l += C[i] * param->
nu;
1615 alpha[i] =
min(C[i],nu_l);
1642 double *alpha2 =
new double[2*l];
1643 double *linear_term =
new double[2*l];
1644 double *C =
new double[2*l];
1651 linear_term[i] = param->
p - prob->
y[i];
1653 C[i] = prob->
W[i]*param->
C;
1656 linear_term[i+l] = param->
p + prob->
y[i];
1658 C[i+l] = prob->
W[i]*param->
C;
1662 s.
Solve(2*l,
SVR_Q(*prob,*param), linear_term, y,
1664 double sum_alpha = 0;
1667 alpha[i] = alpha2[i] - alpha2[i+l];
1668 sum_alpha += fabs(alpha[i]);
1672 delete[] linear_term;
1682 double *C =
new double[2*l];
1683 double *alpha2 =
new double[2*l];
1684 double *linear_term =
new double[2*l];
1691 C[i] = C[i+l] = prob->
W[i]*param->
C;
1692 sum += C[i] * param->
nu;
1698 alpha2[i] = alpha2[i+l] =
min(sum,C[i]);
1701 linear_term[i] = - prob->
y[i];
1704 linear_term[i+l] = prob->
y[i];
1709 s.
Solve(2*l,
SVR_Q(*prob,*param), linear_term, y,
1712 info(
"epsilon = %f\n",-si->
r);
1715 alpha[i] = alpha2[i] - alpha2[i+l];
1718 delete[] linear_term;
1734 double Cp,
double Cn)
1736 double *alpha =
Malloc(
double,prob->
l);
1768 for(
int i=0;i<prob->
l;i++)
1770 if(fabs(alpha[i]) > 0)
1788 info(
"nSV = %d, nBSV = %d\n",nSV,nBSV);
1798 int l,
const double *dec_values,
const double *labels,
1799 double& A,
double& B)
1801 double prior1=0, prior0 = 0;
1805 if (labels[i] > 0) prior1+=1;
1809 double min_step=1e-10;
1812 double hiTarget=(prior1+1.0)/(prior1+2.0);
1813 double loTarget=1/(prior0+2.0);
1814 double *t=
Malloc(
double,l);
1815 double fApB,p,q,h11,h22,h21,g1,g2,det,dA,dB,gd,stepsize;
1816 double newA,newB,newf,d1,d2;
1820 A=0.0; B=log((prior0+1.0)/(prior1+1.0));
1825 if (labels[i]>0) t[i]=hiTarget;
1827 fApB = dec_values[i]*A+B;
1829 fval += t[i]*fApB + log(1+exp(-fApB));
1831 fval += (t[i] - 1)*fApB +log(1+exp(fApB));
1833 for (iter=0;iter<max_iter;iter++)
1838 h21=0.0;g1=0.0;g2=0.0;
1841 fApB = dec_values[i]*A+B;
1844 p=exp(-fApB)/(1.0+exp(-fApB));
1845 q=1.0/(1.0+exp(-fApB));
1849 p=1.0/(1.0+exp(fApB));
1850 q=exp(fApB)/(1.0+exp(fApB));
1853 h11+=dec_values[i]*dec_values[i]*d2;
1855 h21+=dec_values[i]*d2;
1857 g1+=dec_values[i]*d1;
1862 if (fabs(g1)<eps && fabs(g2)<eps)
1866 det=h11*h22-h21*h21;
1867 dA=-(h22*g1 - h21 * g2) / det;
1868 dB=-(-h21*g1+ h11 * g2) / det;
1873 while (stepsize >= min_step)
1875 newA = A + stepsize * dA;
1876 newB = B + stepsize * dB;
1882 fApB = dec_values[i]*newA+newB;
1884 newf += t[i]*fApB + log(1+exp(-fApB));
1886 newf += (t[i] - 1)*fApB +log(1+exp(fApB));
1889 if (newf<fval+0.0001*stepsize*gd)
1891 A=newA;B=newB;fval=newf;
1895 stepsize = stepsize / 2.0;
1898 if (stepsize < min_step)
1900 info(
"Line search fails in two-class probability estimates\n");
1906 info(
"Reaching maximal iterations in two-class probability estimates\n");
1912 double fApB = decision_value*A+B;
1915 return exp(-fApB)/(1.0+exp(-fApB));
1917 return 1.0/(1+exp(fApB)) ;
1924 int iter = 0, max_iter=
max(100,k);
1925 double **Q=
Malloc(
double *,k);
1926 double *Qp=
Malloc(
double,k);
1927 double pQp, eps=0.005/k;
1936 Q[t][t]+=
r[j][t]*
r[j][t];
1941 Q[t][t]+=
r[j][t]*
r[j][t];
1942 Q[t][j]=-
r[j][t]*
r[t][j];
1945 for (iter=0;iter<max_iter;iter++)
1953 Qp[t]+=Q[t][j]*p[j];
1959 double error=fabs(Qp[t]-pQp);
1960 if (error>max_error)
1963 if (max_error<eps)
break;
1967 double diff=(-Qp[t]+pQp)/Q[t][t];
1969 pQp=(pQp+diff*(diff*Q[t][t]+2*Qp[t]))/(1+diff)/(1+diff);
1972 Qp[j]=(Qp[j]+diff*Q[t][j])/(1+diff);
1978 info(
"Exceeds max_iter in multiclass_prob\n");
1979 for(t=0;t<k;t++) free(Q[t]);
1987 double Cp,
double Cn,
double& probA,
double& probB)
1991 int *perm =
Malloc(
int,prob->
l);
1992 double *dec_values =
Malloc(
double,prob->
l);
1995 for(i=0;i<prob->
l;i++) perm[i]=i;
1996 for(i=0;i<prob->
l;i++)
1998 int j = i+rand()%(prob->
l-i);
1999 swap(perm[i],perm[j]);
2001 for(i=0;i<nr_fold;i++)
2003 int begin = i*prob->
l/nr_fold;
2004 int end = (i+1)*prob->
l/nr_fold;
2008 subprob.
l = prob->
l-(end-begin);
2010 subprob.y =
Malloc(
double,subprob.l);
2011 subprob.W =
Malloc(
double,subprob.l);
2014 for(j=0;j<begin;j++)
2016 subprob.x[k] = prob->
x[perm[j]];
2017 subprob.y[k] = prob->
y[perm[j]];
2018 subprob.W[k] = prob->
W[perm[j]];
2021 for(j=end;j<prob->
l;j++)
2023 subprob.x[k] = prob->
x[perm[j]];
2024 subprob.y[k] = prob->
y[perm[j]];
2025 subprob.W[k] = prob->
W[perm[j]];
2028 int p_count=0,n_count=0;
2035 if(p_count==0 && n_count==0)
2036 for(j=begin;j<end;j++)
2037 dec_values[perm[j]] = 0;
2038 else if(p_count > 0 && n_count == 0)
2039 for(j=begin;j<end;j++)
2040 dec_values[perm[j]] = 1;
2041 else if(p_count == 0 && n_count > 0)
2042 for(j=begin;j<end;j++)
2043 dec_values[perm[j]] = -1;
2057 for(j=begin;j<end;j++)
2061 dec_values[perm[j]] *= submodel->
label[0];
2081 double *ymv =
Malloc(
double,prob->
l);
2087 for(i=0;i<prob->
l;i++)
2089 ymv[i]=prob->
y[i]-ymv[i];
2090 mae += fabs(ymv[i]);
2093 double std=sqrt(2*mae*mae);
2096 for(i=0;i<prob->
l;i++)
2097 if (fabs(ymv[i]) > 5*std)
2101 mae /= (prob->
l-count);
2102 info(
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma= %g\n",mae);
2113 int max_nr_class = 16;
2116 int *count =
Malloc(
int,max_nr_class);
2117 int *data_label =
Malloc(
int,
l);
2122 int this_label = (int)prob->
y[i];
2126 if(this_label ==
label[j])
2138 label = (
int *)realloc(
label,max_nr_class*
sizeof(
int));
2139 count = (
int *)realloc(count,max_nr_class*
sizeof(
int));
2150 start[i] = start[i-1]+count[i-1];
2153 perm[start[data_label[i]]] = i;
2154 ++start[data_label[i]];
2158 start[i] = start[i-1]+count[i-1];
2174 for(i=0;i<prob->
l;i++)
2175 if(prob->
W[i] > 0)
l++;
2183 for(i=0;i<prob->
l;i++)
2186 newprob->
x[j] = prob->
x[i];
2187 newprob->
y[j] = prob->
y[i];
2188 newprob->
W[j] = prob->
W[i];
2212 model->
label = NULL;
2231 for(i=0;i<prob->
l;i++)
2238 for(i=0;i<prob->
l;i++)
2239 if(fabs(f.
alpha[i]) > 0)
2241 model->
SV[j] = prob->
x[i];
2262 info(
"WARNING: training data in only one class. See README for details.\n");
2271 x[i] = prob->
x[perm[i]];
2272 W[i] = prob->
W[perm[i]];
2279 weighted_C[i] =
param->
C;
2280 for(i=0;i<param->nr_weight;i++)
2287 fprintf(stderr,
"WARNING: class label %d specified in weight is not found\n",
param->
weight_label[i]);
2294 bool *nonzero =
Malloc(
bool,
l);
2311 int si = start[i], sj = start[j];
2312 int ci = count[i], cj = count[j];
2315 sub_prob.
y =
Malloc(
double,sub_prob.
l);
2316 sub_prob.
W =
Malloc(
double,sub_prob.
l);
2320 sub_prob.
x[k] = x[si+k];
2322 sub_prob.
W[k] = W[si+k];
2326 sub_prob.
x[ci+k] = x[sj+k];
2327 sub_prob.
y[ci+k] = -1;
2328 sub_prob.
W[ci+k] = W[sj+k];
2336 if(!nonzero[si+k] && fabs(f[p].alpha[k]) > 0)
2337 nonzero[si+k] =
true;
2339 if(!nonzero[sj+k] && fabs(f[p].alpha[ci+k]) > 0)
2340 nonzero[sj+k] =
true;
2357 model->
rho[i] = f[i].
rho;
2381 for(
int j=0;j<count[i];j++)
2382 if(nonzero[start[i]+j])
2391 info(
"Total nSV = %d\n",total_sv);
2393 model->
l = total_sv;
2400 model->
SV[p] = x[i];
2407 nz_start[i] = nz_start[i-1]+nz_count[i-1];
2426 int q = nz_start[i];
2464 int *fold_start =
Malloc(
int,nr_fold+1);
2480 int *fold_count =
Malloc(
int,nr_fold);
2486 for(i=0;i<count[c];i++)
2488 int j = i+rand()%(count[c]-i);
2489 swap(index[start[c]+j],index[start[c]+i]);
2491 for(i=0;i<nr_fold;i++)
2495 fold_count[i]+=(i+1)*count[c]/nr_fold-i*count[c]/nr_fold;
2498 for (i=1;i<=nr_fold;i++)
2499 fold_start[i] = fold_start[i-1]+fold_count[i-1];
2501 for(i=0;i<nr_fold;i++)
2503 int begin = start[c]+i*count[c]/nr_fold;
2504 int end = start[c]+(i+1)*count[c]/nr_fold;
2505 for(
int j=begin;j<end;j++)
2507 perm[fold_start[i]] = index[j];
2512 for (i=1;i<=nr_fold;i++)
2513 fold_start[i] = fold_start[i-1]+fold_count[i-1];
2522 for(i=0;i<
l;i++) perm[i]=i;
2525 int j = i+rand()%(
l-i);
2526 swap(perm[i],perm[j]);
2528 for(i=0;i<=nr_fold;i++)
2529 fold_start[i]=i*
l/nr_fold;
2532 for(i=0;i<nr_fold;i++)
2534 int begin = fold_start[i];
2535 int end = fold_start[i+1];
2539 subprob.
l =
l-(end-begin);
2541 subprob.
y =
Malloc(
double,subprob.
l);
2543 subprob.
W =
Malloc(
double,subprob.
l);
2545 for(j=0;j<begin;j++)
2547 subprob.
x[k] = prob->
x[perm[j]];
2548 subprob.
y[k] = prob->
y[perm[j]];
2549 subprob.
W[k] = prob->
W[perm[j]];
2554 subprob.
x[k] = prob->
x[perm[j]];
2555 subprob.
y[k] = prob->
y[perm[j]];
2556 subprob.
W[k] = prob->
W[perm[j]];
2564 for(j=begin;j<end;j++)
2566 free(prob_estimates);
2569 for(j=begin;j<end;j++)
2570 target[perm[j]] =
svm_predict(submodel,prob->
x[perm[j]]);
2593 if (model->
label != NULL)
2601 for(
int i=0;i<model->
l;i++)
2614 return model->
probA[0];
2617 fprintf(stderr,
"Model doesn't contain information for SVR probability inference\n");
2631 double *coef = model->
sv_coef[0];
2637 sum += kv * coef[i] * coef[j];
2652 double *kvalue =
Malloc(
double,
l);
2658 double *coef = model->
sv_coef[0];
2660 sum += coef[i] * kvalue[i];
2661 sum -= model->
rho[0];
2665 return sum * model->
label[0];
2677 for(i=0;i<model->
l;i++)
2679 sum -= model->
rho[0];
2683 return (sum>0)?1:-1;
2692 double *kvalue =
Malloc(
double,
l);
2699 start[i] = start[i-1]+model->
nSV[i-1];
2712 int ci = model->
nSV[i];
2713 int cj = model->
nSV[j];
2716 double *coef1 = model->
sv_coef[j-1];
2717 double *coef2 = model->
sv_coef[i];
2719 sum += coef1[si+k] * kvalue[si+k];
2721 sum += coef2[sj+k] * kvalue[sj+k];
2722 sum -= model->
rho[p];
2723 dec_values[p] = sum;
2725 if(dec_values[p] > 0)
2732 int vote_max_idx = 0;
2734 if(vote[i] > vote[vote_max_idx])
2740 return model->
label[vote_max_idx];
2751 dec_values =
Malloc(
double, 1);
2770 double min_prob=1e-7;
2779 pairwise_prob[j][i]=1-pairwise_prob[i][j];
2784 int prob_max_idx = 0;
2786 if(prob_estimates[i] > prob_estimates[prob_max_idx])
2789 free(pairwise_prob[i]);
2791 free(pairwise_prob);
2792 return model->
label[prob_max_idx];
2800 "c_svc",
"nu_svc",
"one_class",
"epsilon_svr",
"nu_svr",NULL
2805 "linear",
"polynomial",
"rbf",
"sigmoid",
"precomputed",NULL
2810 FILE *fp = fopen(model_file_name,
"w");
2811 if(fp==NULL)
return -1;
2813 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2814 setlocale(LC_ALL,
"C");
2832 fprintf(fp,
"nr_class %d\n",
nr_class);
2833 fprintf(fp,
"total_sv %d\n",
l);
2838 fprintf(fp,
" %g",model->
rho[i]);
2844 fprintf(fp,
"label");
2846 fprintf(fp,
" %d",model->
label[i]);
2852 fprintf(fp,
"probA");
2854 fprintf(fp,
" %g",model->
probA[i]);
2859 fprintf(fp,
"probB");
2861 fprintf(fp,
" %g",model->
probB[i]);
2867 fprintf(fp,
"nr_sv");
2869 fprintf(fp,
" %d",model->
nSV[i]);
2873 fprintf(fp,
"SV\n");
2877 for(
int i=0;i<
l;i++)
2880 fprintf(fp,
"%.16g ",
sv_coef[j][i]);
2885 fprintf(fp,
"0:%d ",(
int)(p->
value));
2887 while(p->
index != -1)
2895 setlocale(LC_ALL, old_locale);
2898 if (ferror(fp) != 0 || fclose(fp) != 0)
return -1;
2912 while(strrchr(
line,
'\n') == NULL)
2916 len = (int) strlen(
line);
2925 FILE *fp = fopen(model_file_name,
"rb");
2926 if(fp==NULL)
return NULL;
2928 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2929 setlocale(LC_ALL,
"C");
2936 model->
probA = NULL;
2937 model->
probB = NULL;
2938 model->
label = NULL;
2944 fscanf(fp,
"%80s",cmd);
2946 if(strcmp(cmd,
"svm_type")==0)
2948 fscanf(fp,
"%80s",cmd);
2960 fprintf(stderr,
"unknown svm type.\n");
2962 setlocale(LC_ALL, old_locale);
2971 else if(strcmp(cmd,
"kernel_type")==0)
2973 fscanf(fp,
"%80s",cmd);
2985 fprintf(stderr,
"unknown kernel function.\n");
2987 setlocale(LC_ALL, old_locale);
2996 else if(strcmp(cmd,
"degree")==0)
2998 else if(strcmp(cmd,
"gamma")==0)
3000 else if(strcmp(cmd,
"coef0")==0)
3002 else if(strcmp(cmd,
"nr_class")==0)
3004 else if(strcmp(cmd,
"total_sv")==0)
3005 fscanf(fp,
"%d",&model->
l);
3006 else if(strcmp(cmd,
"rho")==0)
3010 for(
int i=0;i<n;i++)
3011 fscanf(fp,
"%lf",&model->
rho[i]);
3013 else if(strcmp(cmd,
"label")==0)
3017 for(
int i=0;i<n;i++)
3018 fscanf(fp,
"%d",&model->
label[i]);
3020 else if(strcmp(cmd,
"probA")==0)
3024 for(
int i=0;i<n;i++)
3025 fscanf(fp,
"%lf",&model->
probA[i]);
3027 else if(strcmp(cmd,
"probB")==0)
3031 for(
int i=0;i<n;i++)
3032 fscanf(fp,
"%lf",&model->
probB[i]);
3034 else if(strcmp(cmd,
"nr_sv")==0)
3038 for(
int i=0;i<n;i++)
3039 fscanf(fp,
"%d",&model->
nSV[i]);
3041 else if(strcmp(cmd,
"SV")==0)
3046 if(c==EOF || c==
'\n')
break;
3052 fprintf(stderr,
"unknown text in model file: [%s]\n",cmd);
3054 setlocale(LC_ALL, old_locale);
3067 long pos = ftell(fp);
3071 char *p,*endptr,*idx,*val;
3075 p = strtok(
line,
":");
3078 p = strtok(NULL,
":");
3084 elements += model->
l;
3086 fseek(fp,pos,SEEK_SET);
3102 model->
SV[i] = &x_space[j];
3104 p = strtok(
line,
" \t");
3105 model->
sv_coef[0][i] = strtod(p,&endptr);
3106 for(
int k=1;k<m;k++)
3108 p = strtok(NULL,
" \t");
3109 model->
sv_coef[k][i] = strtod(p,&endptr);
3114 idx = strtok(NULL,
":");
3115 val = strtok(NULL,
" \t");
3119 x_space[j].
index = (int) strtol(idx,&endptr,10);
3120 x_space[j].
value = strtod(val,&endptr);
3124 x_space[j++].
index = -1;
3128 setlocale(LC_ALL, old_locale);
3131 if (ferror(fp) != 0 || fclose(fp) != 0)
3140 if(model_ptr->
free_sv && model_ptr->
l > 0 && model_ptr->
SV != NULL)
3141 free((
void *)(model_ptr->
SV[0]));
3144 for(
int i=0;i<model_ptr->
nr_class-1;i++)
3148 free(model_ptr->
SV);
3149 model_ptr->
SV = NULL;
3154 free(model_ptr->
rho);
3155 model_ptr->
rho = NULL;
3157 free(model_ptr->
label);
3158 model_ptr->
label= NULL;
3160 free(model_ptr->
probA);
3161 model_ptr->
probA = NULL;
3163 free(model_ptr->
probB);
3164 model_ptr->
probB= NULL;
3166 free(model_ptr->
nSV);
3167 model_ptr->
nSV = NULL;
3172 if(model_ptr_ptr != NULL && *model_ptr_ptr != NULL)
3175 free(*model_ptr_ptr);
3176 *model_ptr_ptr = NULL;
3191 if(svm_type !=
C_SVC &&
3196 return "unknown svm type";
3201 if(kernel_type !=
LINEAR &&
3202 kernel_type !=
POLY &&
3203 kernel_type !=
RBF &&
3206 return "unknown kernel type";
3212 return "degree of polynomial kernel < 0";
3217 return "cache_size <= 0";
3222 if(svm_type ==
C_SVC ||
3232 return "nu <= 0 or nu > 1";
3240 return "shrinking != 0 and shrinking != 1";
3244 return "probability != 0 and probability != 1";
3248 return "one-class SVM probability output not supported yet";
3256 int max_nr_class = 16;
3259 double *count =
Malloc(
double,max_nr_class);
3264 int this_label = (int)prob->
y[i];
3267 if(this_label ==
label[j])
3269 count[j] += prob->
W[i];
3277 label = (
int *)realloc(
label,max_nr_class*
sizeof(
int));
3278 count = (
double *)realloc(count,max_nr_class*
sizeof(
double));
3288 double n1 = count[i];
3291 double n2 = count[j];
3296 return "specified nu is infeasible";
3312 model->
probA!=NULL);
3317 if(print_func == NULL)