46 #include <hugin_config.h>
48 #define log1p(x) log(1+x)
62 template <
class T>
static inline T
min(T x,T y) {
return (x<y)?x:y; }
65 template <
class T>
static inline T
max(T x,T y) {
return (x>y)?x:y; }
67 template <
class T>
static inline void swap(T& x, T& y) { T t=x; x=y; y=t; }
68 template <
class S,
class T>
static inline void clone(T*& dst, S* src,
int n)
71 memcpy((
void *)dst,(
void *)src,
sizeof(T)*n);
73 static inline double powi(
double base,
int times)
75 double tmp = base, ret = 1.0;
77 for(
int t=times; t>0; t/=2)
86 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
95 static void info(
const char *fmt,...)
100 vsprintf(buf,fmt,ap);
102 (*svm_print_string)(buf);
105 static void info(
const char *fmt,...) {}
177 int more = len - h->
len;
220 swap(
h->data[i],
h->data[j]);
243 virtual Qfloat *
get_Q(
int column,
int len)
const = 0;
244 virtual double *
get_QD()
const = 0;
245 virtual void swap_index(
int i,
int j)
const = 0;
256 virtual Qfloat *
get_Q(
int column,
int len)
const = 0;
257 virtual double *
get_QD()
const = 0;
280 return dot(x[i],x[j]);
296 return x[i][(int)(x[j][0].value)].
value;
301 :kernel_type(param.kernel_type), degree(param.degree),
302 gamma(param.gamma), coef0(param.coef0)
399 while(x->
index != -1)
405 while(y->
index != -1)
411 return exp(-param.
gamma*sum);
454 double *alpha_,
double Cp,
double Cn,
double eps,
475 return (
y[i] > 0)?
Cp :
Cn;
481 else if(
alpha[i] <= 0)
494 bool be_shrunk(
int i,
double Gmax1,
double Gmax2);
525 if(2*nr_free < active_size)
526 info(
"\nWARNING: using -h 0 may be faster\n");
528 if (nr_free*l > 2*active_size*(l-active_size))
530 for(i=active_size;i<
l;i++)
535 G[i] +=
alpha[j] * Q_i[j];
544 double alpha_i =
alpha[i];
545 for(j=active_size;j<
l;j++)
546 G[j] += alpha_i * Q_i[j];
552 double *alpha_,
double Cp,
double Cn,
double eps,
595 double alpha_i =
alpha[i];
598 G[j] += alpha_i*Q_i[j];
608 int max_iter =
max(10000000, l>INT_MAX/100 ? INT_MAX : 100*l);
609 int counter =
min(l,1000)+1;
611 while(iter < max_iter)
617 counter =
min(l,1000);
643 double C_i =
get_C(i);
644 double C_j =
get_C(j);
646 double old_alpha_i =
alpha[i];
647 double old_alpha_j =
alpha[j];
651 double quad_coef =
QD[i]+
QD[j]+2*Q_i[j];
654 double delta = (-
G[i]-
G[j])/quad_coef;
680 alpha[j] = C_i - diff;
688 alpha[i] = C_j + diff;
694 double quad_coef =
QD[i]+
QD[j]-2*Q_i[j];
697 double delta = (
G[i]-
G[j])/quad_coef;
707 alpha[j] = sum - C_i;
723 alpha[i] = sum - C_j;
738 double delta_alpha_i =
alpha[i] - old_alpha_i;
739 double delta_alpha_j =
alpha[j] - old_alpha_j;
743 G[k] += Q_i[k]*delta_alpha_i + Q_j[k]*delta_alpha_j;
759 G_bar[k] -= C_i * Q_i[k];
762 G_bar[k] += C_i * Q_i[k];
770 G_bar[k] -= C_j * Q_j[k];
773 G_bar[k] += C_j * Q_j[k];
787 fprintf(stderr,
"\nWARNING: reaching max number of iterations\n");
821 info(
"\noptimization finished, #iter = %d\n",iter);
845 double obj_diff_min =
INF;
870 Q_i =
Q->
get_Q(i,active_size);
878 double grad_diff=Gmax+
G[j];
884 double quad_coef =
QD[i]+
QD[j]-2.0*
y[i]*Q_i[j];
886 obj_diff = -(grad_diff*grad_diff)/quad_coef;
888 obj_diff = -(grad_diff*grad_diff)/
TAU;
890 if (obj_diff <= obj_diff_min)
893 obj_diff_min = obj_diff;
902 double grad_diff= Gmax-
G[j];
908 double quad_coef =
QD[i]+
QD[j]+2.0*
y[i]*Q_i[j];
910 obj_diff = -(grad_diff*grad_diff)/quad_coef;
912 obj_diff = -(grad_diff*grad_diff)/
TAU;
914 if (obj_diff <= obj_diff_min)
917 obj_diff_min = obj_diff;
937 return(-
G[i] > Gmax1);
939 return(-
G[i] > Gmax2);
944 return(
G[i] > Gmax2);
946 return(
G[i] > Gmax1);
1001 while (active_size > i)
1003 if (!
be_shrunk(active_size, Gmax1, Gmax2))
1017 double ub =
INF, lb = -
INF, sum_free = 0;
1020 double yG =
y[i]*
G[i];
1044 r = sum_free/nr_free;
1071 bool be_shrunk(
int i,
double Gmax1,
double Gmax2,
double Gmax3,
double Gmax4);
1084 double Gmaxp = -
INF;
1085 double Gmaxp2 = -
INF;
1088 double Gmaxn = -
INF;
1089 double Gmaxn2 = -
INF;
1093 double obj_diff_min =
INF;
1117 const Qfloat *Q_ip = NULL;
1118 const Qfloat *Q_in = NULL;
1120 Q_ip =
Q->
get_Q(ip,active_size);
1122 Q_in =
Q->
get_Q(in,active_size);
1130 double grad_diff=Gmaxp+
G[j];
1136 double quad_coef =
QD[ip]+
QD[j]-2*Q_ip[j];
1138 obj_diff = -(grad_diff*grad_diff)/quad_coef;
1140 obj_diff = -(grad_diff*grad_diff)/
TAU;
1142 if (obj_diff <= obj_diff_min)
1145 obj_diff_min = obj_diff;
1154 double grad_diff=Gmaxn-
G[j];
1155 if (-G[j] >= Gmaxn2)
1160 double quad_coef =
QD[in]+
QD[j]-2*Q_in[j];
1162 obj_diff = -(grad_diff*grad_diff)/quad_coef;
1164 obj_diff = -(grad_diff*grad_diff)/
TAU;
1166 if (obj_diff <= obj_diff_min)
1169 obj_diff_min = obj_diff;
1176 if(
max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) <
eps)
1179 if (
y[Gmin_idx] == +1)
1193 return(-
G[i] > Gmax1);
1195 return(-
G[i] > Gmax4);
1200 return(
G[i] > Gmax2);
1202 return(
G[i] > Gmax3);
1210 double Gmax1 = -
INF;
1211 double Gmax2 = -
INF;
1212 double Gmax3 = -
INF;
1213 double Gmax4 = -
INF;
1223 if(-
G[i] > Gmax1) Gmax1 = -
G[i];
1225 else if(-
G[i] > Gmax4) Gmax4 = -
G[i];
1231 if(
G[i] > Gmax2) Gmax2 =
G[i];
1233 else if(
G[i] > Gmax3) Gmax3 =
G[i];
1245 if (
be_shrunk(i, Gmax1, Gmax2, Gmax3, Gmax4))
1248 while (active_size > i)
1250 if (!
be_shrunk(active_size, Gmax1, Gmax2, Gmax3, Gmax4))
1262 int nr_free1 = 0,nr_free2 = 0;
1263 double ub1 =
INF, ub2 =
INF;
1264 double lb1 = -
INF, lb2 = -
INF;
1265 double sum_free1 = 0, sum_free2 = 0;
1272 lb1 =
max(lb1,
G[i]);
1274 ub1 =
min(ub1,
G[i]);
1284 lb2 =
max(lb2,
G[i]);
1286 ub2 =
min(ub2,
G[i]);
1297 r1 = sum_free1/nr_free1;
1302 r2 = sum_free2/nr_free2;
1317 :
Kernel(prob.l, prob.
x, param)
1321 QD =
new double[prob.
l];
1322 for(
int i=0;i<prob.
l;i++)
1332 for(
int j=start;j<len;j++)
1367 :
Kernel(prob.l, prob.
x, param)
1370 QD =
new double[prob.
l];
1371 for(
int i=0;i<prob.
l;i++)
1381 for(
int j=start;j<len;j++)
1417 QD =
new double[2*
l];
1420 for(
int k=0;k<
l;k++)
1444 int j, real_i =
index[i];
1492 double *minus_ones =
new double[l];
1501 if(prob->
y[i] > 0) y[i] = +1;
else y[i] = -1;
1505 s.
Solve(l,
SVC_Q(*prob,*param,y), minus_ones, y,
1510 sum_alpha += alpha[i];
1513 info(
"nu = %f\n", sum_alpha/(Cp*prob->
l));
1518 delete[] minus_ones;
1528 double nu = param->
nu;
1538 double sum_pos = nu*l/2;
1539 double sum_neg = nu*l/2;
1544 alpha[i] =
min(1.0,sum_pos);
1545 sum_pos -= alpha[i];
1549 alpha[i] =
min(1.0,sum_neg);
1550 sum_neg -= alpha[i];
1553 double *zeros =
new double[l];
1563 info(
"C = %f\n",1/r);
1582 double *zeros =
new double[l];
1586 int n = (int)(param->
nu*prob->
l);
1591 alpha[n] = param->
nu * prob->
l - n;
1614 double *alpha2 =
new double[2*l];
1615 double *linear_term =
new double[2*l];
1622 linear_term[i] = param->
p - prob->
y[i];
1626 linear_term[i+l] = param->
p + prob->
y[i];
1631 s.
Solve(2*l,
SVR_Q(*prob,*param), linear_term, y,
1634 double sum_alpha = 0;
1637 alpha[i] = alpha2[i] - alpha2[i+l];
1638 sum_alpha += fabs(alpha[i]);
1640 info(
"nu = %f\n",sum_alpha/(param->
C*l));
1643 delete[] linear_term;
1652 double C = param->
C;
1653 double *alpha2 =
new double[2*l];
1654 double *linear_term =
new double[2*l];
1658 double sum = C * param->
nu * l / 2;
1661 alpha2[i] = alpha2[i+l] =
min(sum,C);
1664 linear_term[i] = - prob->
y[i];
1667 linear_term[i+l] = prob->
y[i];
1672 s.
Solve(2*l,
SVR_Q(*prob,*param), linear_term, y,
1675 info(
"epsilon = %f\n",-si->
r);
1678 alpha[i] = alpha2[i] - alpha2[i+l];
1681 delete[] linear_term;
1696 double Cp,
double Cn)
1698 double *alpha =
Malloc(
double,prob->
l);
1725 for(
int i=0;i<prob->
l;i++)
1727 if(fabs(alpha[i]) > 0)
1743 info(
"nSV = %d, nBSV = %d\n",nSV,nBSV);
1753 int l,
const double *dec_values,
const double *labels,
1754 double&
A,
double& B)
1756 double prior1=0, prior0 = 0;
1760 if (labels[i] > 0) prior1+=1;
1764 double min_step=1e-10;
1767 double hiTarget=(prior1+1.0)/(prior1+2.0);
1768 double loTarget=1/(prior0+2.0);
1769 double *t=
Malloc(
double,l);
1771 double newA,newB,newf,d1,d2;
1775 A=0.0; B=
log((prior0+1.0)/(prior1+1.0));
1780 if (labels[i]>0) t[i]=hiTarget;
1782 fApB = dec_values[i]*A+B;
1784 fval += t[i]*fApB +
log1p(exp(-fApB));
1786 fval += (t[i] - 1)*fApB +
log1p(exp(fApB));
1788 for (iter=0;iter<max_iter;iter++)
1798 fApB = dec_values[i]*A+B;
1801 p=exp(-fApB)/(1.0+exp(-fApB));
1802 q=1.0/(1.0+exp(-fApB));
1806 p=1.0/(1.0+exp(fApB));
1807 q=exp(fApB)/(1.0+exp(fApB));
1810 h11+=dec_values[i]*dec_values[i]*d2;
1812 h21+=dec_values[i]*d2;
1814 g1+=dec_values[i]*d1;
1819 if (fabs(g1)<eps && fabs(g2)<eps)
1823 const double det=h11*h22-h21*h21;
1824 const double dA=-(h22*g1 - h21 * g2) / det;
1825 const double dB=-(-h21*g1+ h11 * g2) / det;
1826 const double gd=g1*dA+g2*dB;
1829 double stepsize = 1;
1830 while (stepsize >= min_step)
1832 newA = A + stepsize * dA;
1833 newB = B + stepsize * dB;
1839 fApB = dec_values[i]*newA+newB;
1841 newf += t[i]*fApB +
log1p(exp(-fApB));
1843 newf += (t[i] - 1)*fApB +
log1p(exp(fApB));
1846 if (newf<fval+0.0001*stepsize*gd)
1848 A=newA;B=newB;fval=newf;
1852 stepsize = stepsize / 2.0;
1855 if (stepsize < min_step)
1857 info(
"Line search fails in two-class probability estimates\n");
1863 info(
"Reaching maximal iterations in two-class probability estimates\n");
1869 double fApB = decision_value*A+B;
1872 return exp(-fApB)/(1.0+exp(-fApB));
1874 return 1.0/(1+exp(fApB)) ;
1881 int iter = 0, max_iter=
max(100,k);
1882 double **Q=
Malloc(
double *,k);
1883 double *Qp=
Malloc(
double,k);
1893 Q[t][t]+=r[j][t]*r[j][t];
1898 Q[t][t]+=r[j][t]*r[j][t];
1899 Q[t][j]=-r[j][t]*r[t][j];
1902 for (iter=0;iter<max_iter;iter++)
1910 Qp[t]+=Q[t][j]*p[j];
1916 double error=fabs(Qp[t]-pQp);
1917 if (error>max_error)
1920 if (max_error<eps)
break;
1924 double diff=(-Qp[t]+pQp)/Q[t][t];
1926 pQp=(pQp+diff*(diff*Q[t][t]+2*Qp[t]))/(1+diff)/(1+diff);
1929 Qp[j]=(Qp[j]+diff*Q[t][j])/(1+diff);
1935 info(
"Exceeds max_iter in multiclass_prob\n");
1936 for(t=0;t<k;t++) free(Q[t]);
1944 double Cp,
double Cn,
double& probA,
double& probB)
1948 int *perm =
Malloc(
int,prob->
l);
1949 double *dec_values =
Malloc(
double,prob->
l);
1952 for(i=0;i<prob->
l;i++) perm[i]=i;
1953 for(i=0;i<prob->
l;i++)
1955 int j = i+rand()%(prob->
l-i);
1956 swap(perm[i],perm[j]);
1958 for(i=0;i<nr_fold;i++)
1960 int begin = i*prob->
l/nr_fold;
1961 int end = (i+1)*prob->
l/nr_fold;
1965 subprob.
l = prob->
l-(end-begin);
1967 subprob.y =
Malloc(
double,subprob.l);
1970 for(j=0;j<begin;j++)
1972 subprob.x[k] = prob->
x[perm[j]];
1973 subprob.y[k] = prob->
y[perm[j]];
1976 for(j=end;j<prob->
l;j++)
1978 subprob.x[k] = prob->
x[perm[j]];
1979 subprob.y[k] = prob->
y[perm[j]];
1982 int p_count=0,n_count=0;
1989 if(p_count==0 && n_count==0)
1990 for(j=begin;j<end;j++)
1991 dec_values[perm[j]] = 0;
1992 else if(p_count > 0 && n_count == 0)
1993 for(j=begin;j<end;j++)
1994 dec_values[perm[j]] = 1;
1995 else if(p_count == 0 && n_count > 0)
1996 for(j=begin;j<end;j++)
1997 dec_values[perm[j]] = -1;
2011 for(j=begin;j<end;j++)
2015 dec_values[perm[j]] *= submodel->
label[0];
2034 double *ymv =
Malloc(
double,prob->
l);
2040 for(i=0;i<prob->
l;i++)
2042 ymv[i]=prob->
y[i]-ymv[i];
2043 mae += fabs(ymv[i]);
2046 double std=sqrt(2*mae*mae);
2049 for(i=0;i<prob->
l;i++)
2050 if (fabs(ymv[i]) > 5*std)
2054 mae /= (prob->
l-count);
2055 info(
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma= %g\n",mae);
2066 int max_nr_class = 16;
2068 int *label =
Malloc(
int,max_nr_class);
2069 int *count =
Malloc(
int,max_nr_class);
2070 int *data_label =
Malloc(
int,l);
2075 int this_label = (int)prob->
y[i];
2077 for(j=0;j<nr_class;j++)
2079 if(this_label == label[j])
2088 if(nr_class == max_nr_class)
2091 label = (
int *)realloc(label,max_nr_class*
sizeof(
int));
2092 count = (
int *)realloc(count,max_nr_class*
sizeof(
int));
2094 label[nr_class] = this_label;
2095 count[nr_class] = 1;
2105 if (nr_class == 2 && label[0] == -1 && label[1] == 1)
2107 swap(label[0],label[1]);
2108 swap(count[0],count[1]);
2111 if(data_label[i] == 0)
2118 int *start =
Malloc(
int,nr_class);
2120 for(i=1;i<nr_class;i++)
2121 start[i] = start[i-1]+count[i-1];
2124 perm[start[data_label[i]]] = i;
2125 ++start[data_label[i]];
2128 for(i=1;i<nr_class;i++)
2129 start[i] = start[i-1]+count[i-1];
2131 *nr_class_ret = nr_class;
2144 model->
param = *param;
2153 model->
label = NULL;
2172 for(i=0;i<prob->
l;i++)
2173 if(fabs(f.
alpha[i]) > 0) ++nSV;
2179 for(i=0;i<prob->
l;i++)
2180 if(fabs(f.
alpha[i]) > 0)
2182 model->
SV[j] = prob->
x[i];
2198 int *perm =
Malloc(
int,l);
2203 info(
"WARNING: training data in only one class. See README for details.\n");
2208 x[i] = prob->
x[perm[i]];
2212 double *weighted_C =
Malloc(
double, nr_class);
2213 for(i=0;i<nr_class;i++)
2214 weighted_C[i] = param->
C;
2215 for(i=0;i<param->nr_weight;i++)
2218 for(j=0;j<nr_class;j++)
2222 fprintf(stderr,
"WARNING: class label %d specified in weight is not found\n", param->
weight_label[i]);
2224 weighted_C[j] *= param->
weight[i];
2229 bool *nonzero =
Malloc(
bool,l);
2234 double *probA=NULL,*probB=NULL;
2237 probA=
Malloc(
double,nr_class*(nr_class-1)/2);
2238 probB=
Malloc(
double,nr_class*(nr_class-1)/2);
2242 for(i=0;i<nr_class;i++)
2243 for(
int j=i+1;j<nr_class;j++)
2246 int si = start[i], sj = start[j];
2247 int ci = count[i], cj = count[j];
2250 sub_prob.
y =
Malloc(
double,sub_prob.
l);
2254 sub_prob.
x[k] = x[si+k];
2259 sub_prob.
x[ci+k] = x[sj+k];
2260 sub_prob.
y[ci+k] = -1;
2266 f[p] =
svm_train_one(&sub_prob,param,weighted_C[i],weighted_C[j]);
2268 if(!nonzero[si+k] && fabs(f[p].alpha[k]) > 0)
2269 nonzero[si+k] =
true;
2271 if(!nonzero[sj+k] && fabs(f[p].alpha[ci+k]) > 0)
2272 nonzero[sj+k] =
true;
2283 for(i=0;i<nr_class;i++)
2284 model->
label[i] = label[i];
2286 model->
rho =
Malloc(
double,nr_class*(nr_class-1)/2);
2287 for(i=0;i<nr_class*(nr_class-1)/2;i++)
2288 model->
rho[i] = f[i].
rho;
2292 model->
probA =
Malloc(
double,nr_class*(nr_class-1)/2);
2293 model->
probB =
Malloc(
double,nr_class*(nr_class-1)/2);
2294 for(i=0;i<nr_class*(nr_class-1)/2;i++)
2296 model->
probA[i] = probA[i];
2297 model->
probB[i] = probB[i];
2307 int *nz_count =
Malloc(
int,nr_class);
2309 for(i=0;i<nr_class;i++)
2312 for(
int j=0;j<count[i];j++)
2313 if(nonzero[start[i]+j])
2318 model->
nSV[i] = nSV;
2322 info(
"Total nSV = %d\n",total_sv);
2324 model->
l = total_sv;
2331 model->
SV[p] = x[i];
2335 int *nz_start =
Malloc(
int,nr_class);
2337 for(i=1;i<nr_class;i++)
2338 nz_start[i] = nz_start[i-1]+nz_count[i-1];
2341 for(i=0;i<nr_class-1;i++)
2345 for(i=0;i<nr_class;i++)
2346 for(
int j=i+1;j<nr_class;j++)
2357 int q = nz_start[i];
2378 for(i=0;i<nr_class*(nr_class-1)/2;i++)
2393 int *perm =
Malloc(
int,l);
2398 fprintf(stderr,
"WARNING: # folds > # data. Will use # folds = # data instead (i.e., leave-one-out cross validation)\n");
2400 fold_start =
Malloc(
int,nr_fold+1);
2412 int *fold_count =
Malloc(
int,nr_fold);
2414 int *index =
Malloc(
int,l);
2417 for (c=0; c<nr_class; c++)
2418 for(i=0;i<count[c];i++)
2420 int j = i+rand()%(count[c]-i);
2421 swap(index[start[c]+j],index[start[c]+i]);
2423 for(i=0;i<nr_fold;i++)
2426 for (c=0; c<nr_class;c++)
2427 fold_count[i]+=(i+1)*count[c]/nr_fold-i*count[c]/nr_fold;
2430 for (i=1;i<=nr_fold;i++)
2431 fold_start[i] = fold_start[i-1]+fold_count[i-1];
2432 for (c=0; c<nr_class;c++)
2433 for(i=0;i<nr_fold;i++)
2435 int begin = start[c]+i*count[c]/nr_fold;
2436 int end = start[c]+(i+1)*count[c]/nr_fold;
2437 for(
int j=begin;j<end;j++)
2439 perm[fold_start[i]] = index[j];
2444 for (i=1;i<=nr_fold;i++)
2445 fold_start[i] = fold_start[i-1]+fold_count[i-1];
2454 for(i=0;i<l;i++) perm[i]=i;
2457 int j = i+rand()%(l-i);
2458 swap(perm[i],perm[j]);
2460 for(i=0;i<=nr_fold;i++)
2461 fold_start[i]=i*l/nr_fold;
2464 for(i=0;i<nr_fold;i++)
2466 int begin = fold_start[i];
2467 int end = fold_start[i+1];
2471 subprob.
l = l-(end-begin);
2473 subprob.
y =
Malloc(
double,subprob.
l);
2476 for(j=0;j<begin;j++)
2478 subprob.
x[k] = prob->
x[perm[j]];
2479 subprob.
y[k] = prob->
y[perm[j]];
2484 subprob.
x[k] = prob->
x[perm[j]];
2485 subprob.
y[k] = prob->
y[perm[j]];
2493 for(j=begin;j<end;j++)
2495 free(prob_estimates);
2498 for(j=begin;j<end;j++)
2499 target[perm[j]] =
svm_predict(submodel,prob->
x[perm[j]]);
2521 if (model->
label != NULL)
2523 label[i] = model->
label[i];
2529 for(
int i=0;i<model->
l;i++)
2542 return model->
probA[0];
2545 fprintf(stderr,
"Model doesn't contain information for SVR probability inference\n");
2557 double *sv_coef = model->
sv_coef[0];
2559 for(i=0;i<model->
l;i++)
2561 sum -= model->
rho[0];
2565 return (sum>0)?1:-1;
2574 double *kvalue =
Malloc(
double,l);
2578 int *start =
Malloc(
int,nr_class);
2580 for(i=1;i<nr_class;i++)
2581 start[i] = start[i-1]+model->
nSV[i-1];
2583 int *vote =
Malloc(
int,nr_class);
2584 for(i=0;i<nr_class;i++)
2588 for(i=0;i<nr_class;i++)
2589 for(
int j=i+1;j<nr_class;j++)
2594 int ci = model->
nSV[i];
2595 int cj = model->
nSV[j];
2598 double *coef1 = model->
sv_coef[j-1];
2599 double *coef2 = model->
sv_coef[i];
2601 sum += coef1[si+k] * kvalue[si+k];
2603 sum += coef2[sj+k] * kvalue[sj+k];
2604 sum -= model->
rho[p];
2605 dec_values[p] = sum;
2607 if(dec_values[p] > 0)
2614 int vote_max_idx = 0;
2615 for(i=1;i<nr_class;i++)
2616 if(vote[i] > vote[vote_max_idx])
2622 return model->
label[vote_max_idx];
2633 dec_values =
Malloc(
double, 1);
2635 dec_values =
Malloc(
double, nr_class*(nr_class-1)/2);
2649 double *dec_values =
Malloc(
double, nr_class*(nr_class-1)/2);
2652 double min_prob=1e-7;
2653 double **pairwise_prob=
Malloc(
double *,nr_class);
2654 for(i=0;i<nr_class;i++)
2655 pairwise_prob[i]=
Malloc(
double,nr_class);
2657 for(i=0;i<nr_class;i++)
2658 for(
int j=i+1;j<nr_class;j++)
2661 pairwise_prob[j][i]=1-pairwise_prob[i][j];
2666 int prob_max_idx = 0;
2667 for(i=1;i<nr_class;i++)
2668 if(prob_estimates[i] > prob_estimates[prob_max_idx])
2670 for(i=0;i<nr_class;i++)
2671 free(pairwise_prob[i]);
2673 free(pairwise_prob);
2674 return model->
label[prob_max_idx];
2682 "c_svc",
"nu_svc",
"one_class",
"epsilon_svr",
"nu_svr",NULL
2687 "linear",
"polynomial",
"rbf",
"sigmoid",
"precomputed",NULL
2692 FILE *fp = fopen(model_file_name,
"w");
2693 if(fp==NULL)
return -1;
2695 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2696 setlocale(LC_ALL,
"C");
2704 fprintf(fp,
"degree %d\n", param.
degree);
2707 fprintf(fp,
"gamma %g\n", param.
gamma);
2710 fprintf(fp,
"coef0 %g\n", param.
coef0);
2714 fprintf(fp,
"nr_class %d\n", nr_class);
2715 fprintf(fp,
"total_sv %d\n",l);
2719 for(
int i=0;i<nr_class*(nr_class-1)/2;i++)
2720 fprintf(fp,
" %g",model->
rho[i]);
2726 fprintf(fp,
"label");
2727 for(
int i=0;i<nr_class;i++)
2728 fprintf(fp,
" %d",model->
label[i]);
2734 fprintf(fp,
"probA");
2735 for(
int i=0;i<nr_class*(nr_class-1)/2;i++)
2736 fprintf(fp,
" %g",model->
probA[i]);
2741 fprintf(fp,
"probB");
2742 for(
int i=0;i<nr_class*(nr_class-1)/2;i++)
2743 fprintf(fp,
" %g",model->
probB[i]);
2749 fprintf(fp,
"nr_sv");
2750 for(
int i=0;i<nr_class;i++)
2751 fprintf(fp,
" %d",model->
nSV[i]);
2755 fprintf(fp,
"SV\n");
2756 const double *
const *sv_coef = model->
sv_coef;
2759 for(
int i=0;i<l;i++)
2761 for(
int j=0;j<nr_class-1;j++)
2762 fprintf(fp,
"%.16g ",sv_coef[j][i]);
2767 fprintf(fp,
"0:%d ",(
int)(p->
value));
2769 while(p->
index != -1)
2777 setlocale(LC_ALL, old_locale);
2780 if (ferror(fp) != 0 || fclose(fp) != 0)
return -1;
2792 while(strrchr(
line,
'\n') == NULL)
2796 int len = (int) strlen(
line);
2810 #define FSCANF(_stream, _format, _var) do{ if (fscanf(_stream, _format, _var) != 1) return false; }while(0)
2819 if(strcmp(cmd,
"svm_type")==0)
2833 fprintf(stderr,
"unknown svm type.\n");
2837 else if(strcmp(cmd,
"kernel_type")==0)
2851 fprintf(stderr,
"unknown kernel function.\n");
2855 else if(strcmp(cmd,
"degree")==0)
2857 else if(strcmp(cmd,
"gamma")==0)
2859 else if(strcmp(cmd,
"coef0")==0)
2861 else if(strcmp(cmd,
"nr_class")==0)
2866 fprintf(stderr,
"nr_class too big");
2870 else if(strcmp(cmd,
"total_sv")==0)
2872 else if(strcmp(cmd,
"rho")==0)
2876 for(
int i=0;i<n;i++)
2879 else if(strcmp(cmd,
"label")==0)
2883 for(
int i=0;i<n;i++)
2886 else if(strcmp(cmd,
"probA")==0)
2890 for(
int i=0;i<n;i++)
2893 else if(strcmp(cmd,
"probB")==0)
2897 for(
int i=0;i<n;i++)
2900 else if(strcmp(cmd,
"nr_sv")==0)
2904 for(
int i=0;i<n;i++)
2907 else if(strcmp(cmd,
"license")==0)
2913 else if(strcmp(cmd,
"SV")==0)
2918 if(c==EOF || c==
'\n')
break;
2924 fprintf(stderr,
"unknown text in model file: [%s]\n",cmd);
2935 FILE *fp = fopen(model_file_name,
"rb");
2936 if(fp==NULL)
return NULL;
2938 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2939 setlocale(LC_ALL,
"C");
2945 model->
probA = NULL;
2946 model->
probB = NULL;
2948 model->
label = NULL;
2954 fprintf(stderr,
"ERROR: fscanf failed to read model\n");
2955 setlocale(LC_ALL, old_locale);
2967 long pos = ftell(fp);
2971 char *p,*endptr,*idx,*val;
2975 p = strtok(
line,
":");
2978 p = strtok(NULL,
":");
2984 elements += model->
l;
2986 fseek(fp,pos,SEEK_SET);
3002 model->
SV[i] = &x_space[j];
3004 p = strtok(
line,
" \t");
3005 model->
sv_coef[0][i] = strtod(p,&endptr);
3006 for(
int k=1;k<m;k++)
3008 p = strtok(NULL,
" \t");
3009 model->
sv_coef[k][i] = strtod(p,&endptr);
3014 idx = strtok(NULL,
":");
3015 val = strtok(NULL,
" \t");
3019 x_space[j].
index = (int) strtol(idx,&endptr,10);
3020 x_space[j].
value = strtod(val,&endptr);
3024 x_space[j++].
index = -1;
3028 setlocale(LC_ALL, old_locale);
3031 if (ferror(fp) != 0 || fclose(fp) != 0)
3040 if(model_ptr->
free_sv && model_ptr->
l > 0 && model_ptr->
SV != NULL)
3041 free((
void *)(model_ptr->
SV[0]));
3044 for(
int i=0;i<model_ptr->
nr_class-1;i++)
3048 free(model_ptr->
SV);
3049 model_ptr->
SV = NULL;
3054 free(model_ptr->
rho);
3055 model_ptr->
rho = NULL;
3057 free(model_ptr->
label);
3058 model_ptr->
label= NULL;
3060 free(model_ptr->
probA);
3061 model_ptr->
probA = NULL;
3063 free(model_ptr->
probB);
3064 model_ptr->
probB= NULL;
3069 free(model_ptr->
nSV);
3070 model_ptr->
nSV = NULL;
3075 if(model_ptr_ptr != NULL && *model_ptr_ptr != NULL)
3078 free(*model_ptr_ptr);
3079 *model_ptr_ptr = NULL;
3094 if(svm_type !=
C_SVC &&
3099 return "unknown svm type";
3104 if(kernel_type !=
LINEAR &&
3105 kernel_type !=
POLY &&
3106 kernel_type !=
RBF &&
3109 return "unknown kernel type";
3111 if(param->
gamma < 0)
3115 return "degree of polynomial kernel < 0";
3120 return "cache_size <= 0";
3125 if(svm_type ==
C_SVC ||
3134 if(param->
nu <= 0 || param->
nu > 1)
3135 return "nu <= 0 or nu > 1";
3143 return "shrinking != 0 and shrinking != 1";
3147 return "probability != 0 and probability != 1";
3151 return "one-class SVM probability output not supported yet";
3159 int max_nr_class = 16;
3161 int *label =
Malloc(
int,max_nr_class);
3162 int *count =
Malloc(
int,max_nr_class);
3167 int this_label = (int)prob->
y[i];
3169 for(j=0;j<nr_class;j++)
3170 if(this_label == label[j])
3177 if(nr_class == max_nr_class)
3180 label = (
int *)realloc(label,max_nr_class*
sizeof(
int));
3181 count = (
int *)realloc(count,max_nr_class*
sizeof(
int));
3183 label[nr_class] = this_label;
3184 count[nr_class] = 1;
3189 for(i=0;i<nr_class;i++)
3192 for(
int j=i+1;j<nr_class;j++)
3195 if(param->
nu*(n1+n2)/2 >
min(n1,n2))
3199 return "specified nu is infeasible";
3215 model->
probA!=NULL);
3220 if(print_func == NULL)
bool read_model_header(FILE *fp, svm_model *model)
Qfloat * get_Q(int i, int len) const
static void swap(T &x, T &y)
double kernel_linear(int i, int j) const
void lru_delete(head_t *h)
static double sigmoid_predict(double decision_value, double A, double B)
bool be_shrunk(int i, double Gmax1, double Gmax2, double Gmax3, double Gmax4)
int select_working_set(int &i, int &j)
double svm_predict_probability(const svm_model *model, const svm_node *x, double *prob_estimates)
static decision_function svm_train_one(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn)
static double k_function(const svm_node *x, const svm_node *y, const svm_parameter ¶m)
virtual void swap_index(int i, int j) const =0
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)
SVR_Q(const svm_problem &prob, const svm_parameter ¶m)
void svm_get_sv_indices(const svm_model *model, int *indices)
ONE_CLASS_Q(const svm_problem &prob, const svm_parameter ¶m)
static const double A(-0.75)
int svm_get_nr_sv(const svm_model *model)
void svm_set_print_string_function(void(*print_func)(const char *))
static const char * kernel_type_table[]
void svm_get_labels(const svm_model *model, int *label)
static char * readline(FILE *input)
void swap_index(int i, int j) const
Qfloat * get_Q(int i, int len) const
Cache(int l, long int size)
static void solve_one_class(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
virtual double calculate_rho()
void lru_insert(head_t *h)
static void clone(T *&dst, S *src, int n)
Qfloat * get_Q(int i, int len) const
void reconstruct_gradient()
static void solve_epsilon_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
virtual void swap_index(int i, int j) const
static void svm_binary_svc_probability(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, double &probA, double &probB)
svm_model * svm_train(const svm_problem *prob, const svm_parameter *param)
bool be_shrunk(int i, double Gmax1, double Gmax2)
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
int svm_get_svm_type(const svm_model *model)
void swap_index(int i, int j)
SVC_Q(const svm_problem &prob, const svm_parameter ¶m, const schar *y_)
svm_model * svm_load_model(const char *model_file_name)
virtual double * get_QD() const =0
virtual double * get_QD() const =0
double(Kernel::* kernel_function)(int i, int j) const
void svm_destroy_param(svm_parameter *param)
void update_alpha_status(int i)
static double dot(const svm_node *px, const svm_node *py)
static void solve_nu_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
const char * svm_check_parameter(const svm_problem *prob, const svm_parameter *param)
double svm_get_svr_probability(const svm_model *model)
bool is_lower_bound(int i)
int svm_get_nr_class(const svm_model *model)
vigra::RGBValue< T, RIDX, GIDX, BIDX > log(vigra::RGBValue< T, RIDX, GIDX, BIDX > const &v)
component-wise logarithm
#define FSCANF(_stream, _format, _var)
void svm_free_model_content(svm_model *model_ptr)
double kernel_poly(int i, int j) const
options wxIntPtr wxIntPtr sortData std::vector< PanoInfo > * data
static void solve_nu_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
static double powi(double base, int times)
static void print_string_stdout(const char *s)
static void multiclass_probability(int k, double **r, double *p)
double kernel_precomputed(int i, int j) const
virtual Qfloat * get_Q(int column, int len) const =0
static double svm_svr_probability(const svm_problem *prob, const svm_parameter *param)
int svm_save_model(const char *model_file_name, const svm_model *model)
static void info(const char *fmt,...)
virtual int select_working_set(int &i, int &j)
static void solve_c_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double Cp, double Cn)
void swap_index(int i, int j) const
void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target)
double kernel_rbf(int i, int j) const
virtual void do_shrinking()
void swap_index(int i, int j) const
int get_data(const int index, Qfloat **data, int len)
double svm_predict(const svm_model *model, const svm_node *x)
bool is_upper_bound(int i)
double kernel_sigmoid(int i, int j) const
struct svm_parameter param
int svm_check_probability_model(const svm_model *model)
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)
Kernel(int l, svm_node *const *x, const svm_parameter ¶m)
static void sigmoid_train(int l, const double *dec_values, const double *labels, double &A, double &B)
virtual Qfloat * get_Q(int column, int len) const =0
double svm_predict_values(const svm_model *model, const svm_node *x, double *dec_values)
static void(* svm_print_string)(const char *)
void swap_index(int i, int j)
static void svm_group_classes(const svm_problem *prob, int *nr_class_ret, int **label_ret, int **start_ret, int **count_ret, int *perm)