template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }
#endif
template <class S, class T> static inline void clone(T*& dst, S* src, int n)
-{
+{
dst = new T[n];
memcpy((void *)dst,(void *)src,sizeof(T)*n);
}
int l=prob->l;
int w_size=get_nr_variable();
double d;
-
+
Xv(w, z);
for(i=0;i<w_size;i++)
f += w[i]*w[i];
- f /= 2;
+ f /= 2;
for(i=0;i<l;i++)
{
d = z[i] - y[i];
for(i=0;i<l;i++)
{
d = z[i] - y[i];
-
+
// generate index set I
if(d < -p)
{
I[sizeI] = i;
sizeI++;
}
-
+
}
subXTv(z, g);
alpha[i] = 0;
for(i=0;i<w_size*nr_class;i++)
- w[i] = 0;
+ w[i] = 0;
for(i=0;i<l;i++)
{
for(m=0;m<nr_class;m++)
{
double val = xi->value;
QD[i] += val*val;
-
+
// Uncomment the for loop if initial alpha isn't zero
// for(m=0; m<nr_class; m++)
// w[(xi->index-1)*nr_class+m] += alpha[i*nr_class+m]*val;
index[i] = i;
}
- while(iter < max_iter)
+ while(iter < max_iter)
{
double stopping = -INF;
for(i=0;i<active_size;i++)
active_size_i[i]--;
while(active_size_i[i]>m)
{
- if(!be_shrunk(i, active_size_i[i], y_index[i],
+ if(!be_shrunk(i, active_size_i[i], y_index[i],
alpha_i[alpha_index_i[active_size_i[i]]], minG))
{
swap(alpha_index_i[m], alpha_index_i[active_size_i[i]]);
swap(G[m], G[active_size_i[i]]);
if(y_index[i] == active_size_i[i])
y_index[i] = m;
- else if(y_index[i] == m)
+ else if(y_index[i] == m)
y_index[i] = active_size_i[i];
break;
}
{
active_size--;
swap(index[s], index[active_size]);
- s--;
+ s--;
continue;
}
// To support weights for instances, use GETI(i) (i)
static void solve_l2r_l1l2_svc(
- const problem *prob, double *w, double eps,
+ const problem *prob, double *w, double eps,
double Cp, double Cn, int solver_type)
{
int l = prob->l;
{
if(prob->y[i] > 0)
{
- y[i] = +1;
+ y[i] = +1;
}
else
{
// 0 <= alpha[i] <= upper_bound[GETI(i)]
for(i=0; i<l; i++)
alpha[i] = 0;
-
+
for(i=0; i<w_size; i++)
w[i] = 0;
for(i=0; i<l; i++)
int i, s, iter = 0;
double *xTx = new double[l];
int max_iter = 1000;
- int *index = new int[l];
+ int *index = new int[l];
double *alpha = new double[2*l]; // store alpha and C - alpha
- schar *y = new schar[l];
+ schar *y = new schar[l];
int max_inner_iter = 100; // for inner Newton
- double innereps = 1e-2;
+ double innereps = 1e-2;
double innereps_min = min(1e-8, eps);
double upper_bound[3] = {Cn, 0, Cp};
{
if(prob->y[i] > 0)
{
- y[i] = +1;
+ y[i] = +1;
}
else
{
y[i] = -1;
}
}
-
+
// Initial alpha can be set here. Note that
// 0 < alpha[i] < upper_bound[GETI(i)]
// alpha[2*i] + alpha[2*i+1] = upper_bound[GETI(i)]
// Decide to minimize g_1(z) or g_2(z)
int ind1 = 2*i, ind2 = 2*i+1, sign = 1;
- if(0.5*a*(alpha[ind2]-alpha[ind1])+b < 0)
+ if(0.5*a*(alpha[ind2]-alpha[ind1])+b < 0)
{
ind1 = 2*i+1;
ind2 = 2*i;
// g_t(z) = z*log(z) + (C-z)*log(C-z) + 0.5a(z-alpha_old)^2 + sign*b(z-alpha_old)
double alpha_old = alpha[ind1];
double z = alpha_old;
- if(C - z < 0.5 * C)
+ if(C - z < 0.5 * C)
z = 0.1*z;
double gp = a*(z-alpha_old)+sign*b+log(z/(C-z));
Gmax = max(Gmax, fabs(gp));
// Newton method on the sub-problem
const double eta = 0.1; // xi in the paper
int inner_iter = 0;
- while (inner_iter <= max_inner_iter)
+ while (inner_iter <= max_inner_iter)
{
if(fabs(gp) < innereps)
break;
double gpp = a + C/(C-z)/z;
double tmpz = z - gp/gpp;
- if(tmpz <= 0)
+ if(tmpz <= 0)
z *= eta;
else // tmpz in (0, C)
z = tmpz;
{
w[xi->index-1] += sign*(z-alpha_old)*yi*xi->value;
xi++;
- }
+ }
}
}
if(iter % 10 == 0)
info(".");
- if(Gmax < eps)
+ if(Gmax < eps)
break;
- if(newton_iter <= l/10)
+ if(newton_iter <= l/10)
innereps = max(innereps_min, 0.1*innereps);
}
info("\nWARNING: reaching max number of iterations\nUsing -s 0 may be faster (also see FAQ)\n\n");
// calculate objective value
-
+
double v = 0;
for(i=0; i<w_size; i++)
v += w[i] * w[i];
v *= 0.5;
for(i=0; i<l; i++)
- v += alpha[2*i] * log(alpha[2*i]) + alpha[2*i+1] * log(alpha[2*i+1])
+ v += alpha[2*i] * log(alpha[2*i]) + alpha[2*i+1] * log(alpha[2*i+1])
- upper_bound[GETI(i)] * log(upper_bound[GETI(i)]);
info("Objective value = %lf\n", v);
// To support weights for instances, use GETI(i) (i)
static void solve_l1r_l2_svc(
- problem *prob_col, double *w, double eps,
+ problem *prob_col, double *w, double eps,
double Cp, double Cn)
{
int l = prob_col->l;
// To support weights for instances, use GETI(i) (i)
static void solve_l1r_lr(
- const problem *prob_col, double *w, double eps,
+ const problem *prob_col, double *w, double eps,
double Cp, double Cn)
{
int l = prob_col->l;
info("WARNING: reaching max number of iterations\n");
// calculate objective value
-
+
double v = 0;
int nnz = 0;
for(j=0; j<w_size; j++)
if(prob->y[i] > 0)
pos++;
neg = prob->l - pos;
-
+
double primal_solver_tol = eps*max(min(pos,neg), 1)/prob->l;
function *fun_obj=NULL;
double *C = new double[prob->l];
for(int i = 0; i < prob->l; i++)
C[i] = param->C;
-
+
fun_obj=new l2r_l2_svr_fun(prob, C, param->p);
TRON tron_obj(fun_obj, param->eps);
tron_obj.set_print_string(liblinear_print_string);
prob_estimates[i]=prob_estimates[i]/sum;
}
- return label;
+ return label;
}
else
return 0;
fprintf(fp, "solver_type %s\n", solver_type_table[param.solver_type]);
fprintf(fp, "nr_class %d\n", model_->nr_class);
-
+
if(model_->label)
{
fprintf(fp, "label");
parameter& param = model_->param;
model_->label = NULL;
-
+
char *old_locale = strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "C");
if(solver_type_table[i] == NULL)
{
fprintf(stderr,"unknown solver type.\n");
-
+
setlocale(LC_ALL, old_locale);
free(model_->label);
free(model_);
fscanf(fp, "%lf ", &model_->w[i*nr_w+j]);
fscanf(fp, "\n");
}
-
+
setlocale(LC_ALL, old_locale);
free(old_locale);
-
+
if (ferror(fp) != 0 || fclose(fp) != 0) return NULL;
return model_;
if(param->C <= 0)
return "C <= 0";
-
+
if(param->p < 0)
return "p < 0";
void set_print_string_function(void (*print_func)(const char*))
{
- if (print_func == NULL)
+ if (print_func == NULL)
liblinear_print_string = &print_string_stdout;
else
liblinear_print_string = print_func;
"-s type : set type of solver (default 1)\n"
" for multi-class classification\n"
" 0 -- L2-regularized logistic regression (primal)\n"
- " 1 -- L2-regularized L2-loss support vector classification (dual)\n"
+ " 1 -- L2-regularized L2-loss support vector classification (dual)\n"
" 2 -- L2-regularized L2-loss support vector classification (primal)\n"
" 3 -- L2-regularized L1-loss support vector classification (dual)\n"
" 4 -- support vector classification by Crammer and Singer\n"
"-c cost : set the parameter C (default 1)\n"
"-p epsilon : set the epsilon in loss function of SVR (default 0.1)\n"
"-e epsilon : set tolerance of termination criterion\n"
- " -s 0 and 2\n"
- " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n"
- " where f is the primal function and pos/neg are # of\n"
+ " -s 0 and 2\n"
+ " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n"
+ " where f is the primal function and pos/neg are # of\n"
" positive/negative data (default 0.01)\n"
" -s 11\n"
- " |f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.001)\n"
+ " |f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.001)\n"
" -s 1, 3, 4, and 7\n"
" Dual maximal violation <= eps; similar to libsvm (default 0.1)\n"
" -s 5 and 6\n"
static char* readline(FILE *input)
{
int len;
-
+
if(fgets(line,max_line_len,input) == NULL)
return NULL;
double *target = Malloc(double, prob.l);
cross_validation(&prob,¶m,nr_fold,target);
- if(param.solver_type == L2R_L2LOSS_SVR ||
- param.solver_type == L2R_L1LOSS_SVR_DUAL ||
+ if(param.solver_type == L2R_L2LOSS_SVR ||
+ param.solver_type == L2R_L1LOSS_SVR_DUAL ||
param.solver_type == L2R_L2LOSS_SVR_DUAL)
{
for(i=0;i<prob.l;i++)
- {
- double y = prob.y[i];
- double v = target[i];
- total_error += (v-y)*(v-y);
- sumv += v;
- sumy += y;
- sumvv += v*v;
- sumyy += y*y;
- sumvy += v*y;
- }
- printf("Cross Validation Mean squared error = %g\n",total_error/prob.l);
- printf("Cross Validation Squared correlation coefficient = %g\n",
- ((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
- ((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
- );
+ {
+ double y = prob.y[i];
+ double v = target[i];
+ total_error += (v-y)*(v-y);
+ sumv += v;
+ sumy += y;
+ sumvv += v*v;
+ sumyy += y*y;
+ sumvy += v*y;
+ }
+ printf("Cross Validation Mean squared error = %g\n",total_error/prob.l);
+ printf("Cross Validation Squared correlation coefficient = %g\n",
+ ((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
+ ((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
+ );
}
else
{
{
switch(param.solver_type)
{
- case L2R_LR:
+ case L2R_LR:
case L2R_L2LOSS_SVC:
param.eps = 0.01;
break;
case L2R_L2LOSS_SVR:
param.eps = 0.001;
break;
- case L2R_L2LOSS_SVC_DUAL:
- case L2R_L1LOSS_SVC_DUAL:
- case MCSVM_CS:
- case L2R_LR_DUAL:
+ case L2R_L2LOSS_SVC_DUAL:
+ case L2R_L1LOSS_SVC_DUAL:
+ case MCSVM_CS:
+ case L2R_LR_DUAL:
param.eps = 0.1;
break;
- case L1R_L2LOSS_SVC:
+ case L1R_L2LOSS_SVC:
case L1R_LR:
param.eps = 0.01;
break;
{
prob.n=max_index+1;
for(i=1;i<prob.l;i++)
- (prob.x[i]-2)->index = prob.n;
+ (prob.x[i]-2)->index = prob.n;
x_space[j-2].index = prob.n;
}
else