51 void CLibLinear::init()
85 SG_ERROR(
"Specified features are not of type CDotFeatures\n")
99 if (num_feat!=num_train_labels)
101 SG_ERROR(
"L1 methods require the data to be transposed: "
102 "number of features %d does not match number of "
103 "training labels %d\n",
104 num_feat, num_train_labels);
111 if (num_vec!=num_train_labels)
113 SG_ERROR(
"number of vectors %d does not match "
114 "number of training labels %d\n",
115 num_vec, num_train_labels);
123 liblinear_problem prob;
139 prob.y=SG_MALLOC(
double, prob.l);
145 for (int32_t i=0; i<prob.l; i++)
150 else if (prob.y[i] == -1)
153 SG_ERROR(
"labels should be +1/-1 only\n")
158 for(
int i=0;i<prob.l;i++)
165 SG_INFO(
"%d training points %d dims\n", prob.l, prob.n)
167 function *fun_obj=NULL;
172 fun_obj=
new l2r_lr_fun(&prob, Cs);
174 SG_DEBUG(
"starting L2R_LR training via tron\n")
182 fun_obj=
new l2r_l2_svc_fun(&prob, Cs);
208 solve_l2r_lr_dual(&prob,
epsilon, Cp, Cn);
212 SG_ERROR(
"Error: unknown solver_type\n")
252 #define GETI(i) (y[i]+1)
255 void CLibLinear::solve_l2r_l1l2_svc(
259 int w_size = prob->n;
262 double *QD = SG_MALLOC(
double, l);
263 int *index = SG_MALLOC(
int, l);
264 double *alpha = SG_MALLOC(
double, l);
265 int32_t *y = SG_MALLOC(int32_t, l);
272 double PGmax_new, PGmin_new;
275 double diag[3] = {0.5/Cn, 0, 0.5/Cp};
290 for(i=0; i<w_size; i++)
304 QD[i] = diag[
GETI(i)];
306 QD[i] += prob->x->dot(i, prob->x,i);
320 for (i=0; i<active_size; i++)
326 for (s=0;s<active_size;s++)
331 G = prob->x->dense_dot(i,
w.
vector, n);
340 C = upper_bound[
GETI(i)];
341 G += alpha[i]*diag[
GETI(i)];
356 else if (alpha[i] == C)
374 if(fabs(PG) > 1.0e-12)
376 double alpha_old = alpha[i];
378 d = (alpha[i] - alpha_old)*yi;
380 prob->x->add_to_dense_vec(d, i,
w.
vector, n);
403 PGmax_old = PGmax_new;
404 PGmin_old = PGmin_new;
412 SG_INFO("optimization finished,
#iter = %d\n",iter)
415 SG_WARNING(
"reaching max number of iterations\nUsing -s 2 may be faster"
416 "(also see liblinear FAQ)\n\n");
423 for(i=0; i<w_size; i++)
427 v += alpha[i]*(alpha[i]*diag[
GETI(i)] - 2);
431 SG_INFO(
"Objective value = %lf\n",v/2)
452 #define GETI(i) (y[i]+1)
455 void CLibLinear::solve_l1r_l2_svc(
456 liblinear_problem *prob_col,
double eps,
double Cp,
double Cn)
459 int w_size = prob_col->n;
461 int active_size = w_size;
462 int max_num_linesearch = 20;
465 double d, G_loss, G,
H;
469 double d_old, d_diff;
470 double loss_old=0, loss_new;
471 double appxcond, cond;
473 int *index = SG_MALLOC(
int, w_size);
474 int32_t *y = SG_MALLOC(int32_t, l);
475 double *b = SG_MALLOC(
double, l);
476 double *xj_sq = SG_MALLOC(
double, w_size);
483 double C[3] = {Cn,0,Cp};
486 if (prob_col->use_bias)
492 if(prob_col->y[j] > 0)
498 for(j=0; j<w_size; j++)
506 for (ind=0; ind<l; ind++)
507 xj_sq[n] += C[
GETI(ind)];
511 iterator=x->get_feature_iterator(j);
512 while (x->get_next_feature(ind, val, iterator))
513 xj_sq[j] += C[
GETI(ind)]*val*val;
514 x->free_feature_iterator(iterator);
527 for(j=0; j<active_size; j++)
533 for(s=0; s<active_size; s++)
541 for (ind=0; ind<l; ind++)
545 double tmp = C[
GETI(ind)]*y[ind];
546 G_loss -= tmp*b[ind];
553 iterator=x->get_feature_iterator(j);
555 while (x->get_next_feature(ind, val, iterator))
559 double tmp = C[
GETI(ind)]*val*y[ind];
560 G_loss -= tmp*b[ind];
564 x->free_feature_iterator(iterator);
575 double violation = 0;
582 else if(Gp>Gmax_old/l && Gn<-Gmax_old/l)
591 violation = fabs(Gp);
593 violation = fabs(Gn);
605 if(fabs(d) < 1.0e-12)
611 for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
616 appxcond = xj_sq[j]*d*d + G_loss*d + cond;
621 for (ind=0; ind<l; ind++)
622 b[ind] += d_diff*y[ind];
627 iterator=x->get_feature_iterator(j);
628 while (x->get_next_feature(ind, val, iterator))
629 b[ind] += d_diff*val*y[ind];
631 x->free_feature_iterator(iterator);
636 if(num_linesearch == 0)
643 for (ind=0; ind<l; ind++)
646 loss_old += C[
GETI(ind)]*b[ind]*b[ind];
647 double b_new = b[ind] + d_diff*y[ind];
650 loss_new += C[
GETI(ind)]*b_new*b_new;
655 iterator=x->get_feature_iterator(j);
656 while (x->get_next_feature(ind, val, iterator))
659 loss_old += C[
GETI(ind)]*b[ind]*b[ind];
660 double b_new = b[ind] + d_diff*val*y[ind];
663 loss_new += C[
GETI(ind)]*b_new*b_new;
665 x->free_feature_iterator(iterator);
673 for (ind=0; ind<l; ind++)
675 double b_new = b[ind] + d_diff*y[ind];
678 loss_new += C[
GETI(ind)]*b_new*b_new;
683 iterator=x->get_feature_iterator(j);
684 while (x->get_next_feature(ind, val, iterator))
686 double b_new = b[ind] + d_diff*val*y[ind];
689 loss_new += C[
GETI(ind)]*b_new*b_new;
691 x->free_feature_iterator(iterator);
695 cond = cond + loss_new - loss_old;
709 if(num_linesearch >= max_num_linesearch)
712 for(
int i=0; i<l; i++)
715 for(
int i=0; i<n; i++)
720 iterator=x->get_feature_iterator(i);
721 while (x->get_next_feature(ind, val, iterator))
722 b[ind] -=
w.
vector[i]*val*y[ind];
723 x->free_feature_iterator(iterator);
728 for (ind=0; ind<l; ind++)
735 Gmax_init = Gmax_new;
741 if(Gmax_new <= eps*Gmax_init)
743 if(active_size == w_size)
747 active_size = w_size;
757 SG_INFO("optimization finished,
#iter = %d\n", iter)
759 SG_WARNING(
"\nWARNING: reaching max number of iterations\n")
765 for(j=0; j<w_size; j++)
775 v += C[
GETI(j)]*b[j]*b[j];
777 SG_INFO(
"Objective value = %lf\n", v)
778 SG_INFO("
#nonzeros/#features = %d/%d\n", nnz, w_size)
798 #define GETI(i) (y[i]+1)
801 void CLibLinear::solve_l1r_lr(
802 const liblinear_problem *prob_col,
double eps,
803 double Cp,
double Cn)
806 int w_size = prob_col->n;
808 int active_size = w_size;
809 int max_num_linesearch = 20;
817 double sum1, appxcond1;
818 double sum2, appxcond2;
821 int *index = SG_MALLOC(
int, w_size);
822 int32_t *y = SG_MALLOC(int32_t, l);
823 double *exp_wTx = SG_MALLOC(
double, l);
824 double *exp_wTx_new = SG_MALLOC(
double, l);
825 double *xj_max = SG_MALLOC(
double, w_size);
826 double *C_sum = SG_MALLOC(
double, w_size);
827 double *xjneg_sum = SG_MALLOC(
double, w_size);
828 double *xjpos_sum = SG_MALLOC(
double, w_size);
835 double C[3] = {Cn,0,Cp};
838 if (prob_col->use_bias)
844 if(prob_col->y[j] > 0)
849 for(j=0; j<w_size; j++)
860 for (ind=0; ind<l; ind++)
864 C_sum[j] += C[
GETI(ind)];
866 xjneg_sum[j] += C[
GETI(ind)];
868 xjpos_sum[j] += C[
GETI(ind)];
878 C_sum[j] += C[
GETI(ind)];
880 xjneg_sum[j] += C[
GETI(ind)]*val;
882 xjpos_sum[j] += C[
GETI(ind)]*val;
896 for(j=0; j<active_size; j++)
902 for(s=0; s<active_size; s++)
911 for (ind=0; ind<l; ind++)
913 double exp_wTxind = exp_wTx[ind];
914 double tmp1 = 1.0/(1+exp_wTxind);
915 double tmp2 = C[
GETI(ind)]*tmp1;
916 double tmp3 = tmp2*exp_wTxind;
927 double exp_wTxind = exp_wTx[ind];
928 double tmp1 = val/(1+exp_wTxind);
929 double tmp2 = C[
GETI(ind)]*tmp1;
930 double tmp3 = tmp2*exp_wTxind;
938 G = -sum2 + xjneg_sum[j];
942 double violation = 0;
949 else if(Gp>Gmax_old/l && Gn<-Gmax_old/l)
958 violation = fabs(Gp);
960 violation = fabs(Gn);
972 if(fabs(d) < 1.0e-12)
979 for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
985 double tmp = exp(d*xj_max[j]);
986 appxcond1 = log(1+sum1*(tmp-1)/xj_max[j]/C_sum[j])*C_sum[j] + cond - d*xjpos_sum[j];
987 appxcond2 = log(1+sum2*(1/tmp-1)/xj_max[j]/C_sum[j])*C_sum[j] + cond + d*xjneg_sum[j];
992 for (ind=0; ind<l; ind++)
993 exp_wTx[ind] *= exp(d);
1000 exp_wTx[ind] *= exp(d*val);
1007 cond += d*xjneg_sum[j];
1013 for (ind=0; ind<l; ind++)
1015 double exp_dx = exp(d);
1016 exp_wTx_new[i] = exp_wTx[ind]*exp_dx;
1017 cond += C[
GETI(ind)]*log((1+exp_wTx_new[i])/(exp_dx+exp_wTx_new[i]));
1027 double exp_dx = exp(d*val);
1028 exp_wTx_new[i] = exp_wTx[ind]*exp_dx;
1029 cond += C[
GETI(ind)]*log((1+exp_wTx_new[i])/(exp_dx+exp_wTx_new[i]));
1040 for (ind=0; ind<l; ind++)
1042 exp_wTx[ind] = exp_wTx_new[i];
1051 exp_wTx[ind] = exp_wTx_new[i];
1068 if(num_linesearch >= max_num_linesearch)
1071 for(
int i=0; i<l; i++)
1074 for(
int i=0; i<w_size; i++)
1080 for (ind=0; ind<l; ind++)
1087 exp_wTx[ind] +=
w.
vector[i]*val;
1092 for(
int i=0; i<l; i++)
1093 exp_wTx[i] = exp(exp_wTx[i]);
1098 Gmax_init = Gmax_new;
1102 if(Gmax_new <= eps*Gmax_init)
1104 if(active_size == w_size)
1108 active_size = w_size;
1114 Gmax_old = Gmax_new;
1118 SG_INFO("optimization finished,
#iter = %d\n", iter)
1120 SG_WARNING(
"\nWARNING: reaching max number of iterations\n")
1126 for(j=0; j<w_size; j++)
1127 if(
w.vector[j] != 0)
1134 v += C[
GETI(j)]*log(1+1/exp_wTx[j]);
1136 v += C[
GETI(j)]*log(1+exp_wTx[j]);
1138 SG_INFO(
"Objective value = %lf\n", v)
1139 SG_INFO("
#nonzeros/#features = %d/%d\n", nnz, w_size)
1144 SG_FREE(exp_wTx_new);
1170 #define GETI(i) (y[i]+1)
1173 void CLibLinear::solve_l2r_lr_dual(
const liblinear_problem *prob,
double eps,
double Cp,
double Cn)
1176 int w_size = prob->n;
1178 double *xTx =
new double[l];
1179 int max_iter = 1000;
1180 int *index =
new int[l];
1181 double *alpha =
new double[2*l];
1182 int32_t *y =
new int32_t[l];
1183 int max_inner_iter = 100;
1184 double innereps = 1e-2;
1186 double upper_bound[3] = {Cn, 0, Cp};
1187 double Gmax_init = 0;
1207 alpha[2*i+1] = upper_bound[
GETI(i)] - alpha[2*i];
1210 for(i=0; i<w_size; i++)
1214 xTx[i] = prob->x->dot(i, prob->x,i);
1215 prob->x->add_to_dense_vec(y[i]*alpha[2*i], i,
w.
vector, w_size);
1219 w.
vector[w_size]+=y[i]*alpha[2*i];
1225 while (iter < max_iter)
1232 int newton_iter = 0;
1238 double C = upper_bound[
GETI(i)];
1239 double ywTx = 0, xisq = xTx[i];
1241 ywTx = prob->x->dense_dot(i,
w.
vector, w_size);
1246 double a = xisq, b = ywTx;
1249 int ind1 = 2*i, ind2 = 2*i+1, sign = 1;
1250 if(0.5*a*(alpha[ind2]-alpha[ind1])+b < 0)
1258 double alpha_old = alpha[ind1];
1259 double z = alpha_old;
1262 double gp = a*(z-alpha_old)+sign*b+
CMath::log(z/(C-z));
1266 const double eta = 0.1;
1268 while (inner_iter <= max_inner_iter)
1270 if(fabs(gp) < innereps)
1272 double gpp = a + C/(C-z)/z;
1273 double tmpz = z - gp/gpp;
1278 gp = a*(z-alpha_old)+sign*b+log(z/(C-z));
1288 prob->x->add_to_dense_vec(sign*(z-alpha_old)*yi, i,
w.
vector, w_size);
1291 w.
vector[w_size]+=sign*(z-alpha_old)*yi;
1304 if(newton_iter <= l/10)
1305 innereps =
CMath::max(innereps_min, 0.1*innereps);
1310 SG_INFO(
"optimization finished, #iter = %d\n",iter)
1311 if (iter >= max_iter)
1312 SG_WARNING(
"reaching max number of iterations\nUsing -s 0 may be faster (also see FAQ)\n\n")
1317 for(i=0; i<w_size; i++)
1321 v += alpha[2*i] * log(alpha[2*i]) + alpha[2*i+1] * log(alpha[2*i+1])
1322 - upper_bound[
GETI(i)] * log(upper_bound[
GETI(i)]);
1323 SG_INFO(
"Objective value = %lf\n", v)
1335 SG_ERROR(
"Please assign labels first!\n")
1339 if (num_labels!=linear_term.
vlen)
1341 SG_ERROR(
"Number of labels (%d) does not match number"
1342 " of entries (%d) in linear term \n", num_labels,
1352 SG_ERROR(
"Please assign linear term first!\n")
1360 SG_ERROR(
"Please assign labels first!\n")
Class Time that implements a stopwatch based on either cpu time or wall clock time.
static void fill_vector(T *vec, int32_t len, T value)
virtual ELabelType get_label_type() const =0
L2 regularized linear logistic regression via dual.
The class Labels models labels, i.e. class assignments of objects.
static const float64_t INFTY
infinity
virtual int32_t get_num_labels() const =0
static float64_t log10(float64_t v)
L2 regularized SVM with L2-loss using newton in the primal.
L1 regularized logistic regression.
virtual int32_t get_num_vectors() const =0
void tron(float64_t *w, float64_t max_train_time)
float64_t m_max_train_time
L1 regularized SVM with L2-loss using dual coordinate descent.
Features that support dot products among other operations.
SGVector< float64_t > get_linear_term()
virtual int32_t get_dim_feature_space() const =0
float64_t cur_time_diff(bool verbose=false)
void set_linear_term(const SGVector< float64_t > linear_term)
void set_max_iterations(int32_t max_iter=1000)
static void clear_cancel()
L2 regularized linear logistic regression.
virtual void free_feature_iterator(void *iterator)=0
L2 regularized SVM with L2-loss using dual coordinate descent.
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
LIBLINEAR_SOLVER_TYPE liblinear_solver_type
static bool cancel_computations()
virtual void * get_feature_iterator(int32_t vector_index)=0
virtual bool get_next_feature(int32_t &index, float64_t &value, void *iterator)=0
all of classes and functions are contained in the shogun namespace
The class Features is the base class of all feature objects.
static float64_t log(float64_t v)
virtual bool train_machine(CFeatures *data=NULL)
Binary Labels for binary classification.
static void swap(T &a, T &b)
virtual void set_bias(float64_t b)
L2 regularized linear SVM with L1-loss using dual coordinate descent.
bool has_property(EFeatureProperty p) const
virtual void set_labels(CLabels *lab)
#define SG_SABS_PROGRESS(...)
SGVector< float64_t > m_linear_term