]> granicus.if.org Git - liblinear/commitdiff
Fix some indention
authorleepei <leepei@16e7d947-dcc2-db11-b54a-0017319806e7>
Sun, 28 Oct 2012 16:46:32 +0000 (16:46 +0000)
committerleepei <leepei@16e7d947-dcc2-db11-b54a-0017319806e7>
Sun, 28 Oct 2012 16:46:32 +0000 (16:46 +0000)
linear.cpp
predict.c
train.c

index 5a6345da470ce2ec2bab032b47819fc958544fd5..1dc8056f156af1a120305908c1b98aba3eab22bc 100644 (file)
@@ -15,7 +15,7 @@ template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
 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);
 }
@@ -378,12 +378,12 @@ double l2r_l2_svr_fun::fun(double *w)
        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];
@@ -408,7 +408,7 @@ void l2r_l2_svr_fun::grad(double *w, double *g)
        for(i=0;i<l;i++)
        {
                d = z[i] - y[i];
-               
+
                // generate index set I
                if(d < -p)
                {
@@ -422,7 +422,7 @@ void l2r_l2_svr_fun::grad(double *w, double *g)
                        I[sizeI] = i;
                        sizeI++;
                }
-       
+
        }
        subXTv(z, g);
 
@@ -559,7 +559,7 @@ void Solver_MCSVM_CS::Solve(double *w)
                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++)
@@ -570,7 +570,7 @@ void Solver_MCSVM_CS::Solve(double *w)
                {
                        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;
@@ -581,7 +581,7 @@ void Solver_MCSVM_CS::Solve(double *w)
                index[i] = i;
        }
 
-       while(iter < max_iter) 
+       while(iter < max_iter)
        {
                double stopping = -INF;
                for(i=0;i<active_size;i++)
@@ -632,14 +632,14 @@ void Solver_MCSVM_CS::Solve(double *w)
                                                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;
                                                        }
@@ -652,7 +652,7 @@ void Solver_MCSVM_CS::Solve(double *w)
                                {
                                        active_size--;
                                        swap(index[s], index[active_size]);
-                                       s--;    
+                                       s--;
                                        continue;
                                }
 
@@ -776,7 +776,7 @@ void Solver_MCSVM_CS::Solve(double *w)
 // 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;
@@ -811,7 +811,7 @@ static void solve_l2r_l1l2_svc(
        {
                if(prob->y[i] > 0)
                {
-                       y[i] = +1; 
+                       y[i] = +1;
                }
                else
                {
@@ -823,7 +823,7 @@ static void solve_l2r_l1l2_svc(
        // 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++)
@@ -1227,11 +1227,11 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
        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};
 
@@ -1239,14 +1239,14 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
        {
                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)]
@@ -1298,7 +1298,7 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
 
                        // 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;
@@ -1308,7 +1308,7 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
                        //  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));
@@ -1316,13 +1316,13 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
                        // 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;
@@ -1340,7 +1340,7 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
                                {
                                        w[xi->index-1] += sign*(z-alpha_old)*yi*xi->value;
                                        xi++;
-                               }  
+                               }
                        }
                }
 
@@ -1348,10 +1348,10 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
                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);
 
        }
@@ -1361,13 +1361,13 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
                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);
 
@@ -1395,7 +1395,7 @@ void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, do
 // 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;
@@ -1682,7 +1682,7 @@ static void solve_l1r_l2_svc(
 // 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;
@@ -2014,7 +2014,7 @@ static void solve_l1r_lr(
                info("WARNING: reaching max number of iterations\n");
 
        // calculate objective value
-       
+
        double v = 0;
        int nnz = 0;
        for(j=0; j<w_size; j++)
@@ -2169,7 +2169,7 @@ static void train_one(const problem *prob, const parameter *param, double *w, do
                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;
@@ -2247,7 +2247,7 @@ static void train_one(const problem *prob, const parameter *param, double *w, do
                        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);
@@ -2544,7 +2544,7 @@ double predict_probability(const struct model *model_, const struct feature_node
                                prob_estimates[i]=prob_estimates[i]/sum;
                }
 
-               return label;           
+               return label;
        }
        else
                return 0;
@@ -2584,7 +2584,7 @@ int save_model(const char *model_file_name, const struct model *model_)
 
        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");
@@ -2627,7 +2627,7 @@ struct model *load_model(const char *model_file_name)
        parameter& param = model_->param;
 
        model_->label = NULL;
-       
+
        char *old_locale = strdup(setlocale(LC_ALL, NULL));
        setlocale(LC_ALL, "C");
 
@@ -2650,7 +2650,7 @@ struct model *load_model(const char *model_file_name)
                        if(solver_type_table[i] == NULL)
                        {
                                fprintf(stderr,"unknown solver type.\n");
-                               
+
                                setlocale(LC_ALL, old_locale);
                                free(model_->label);
                                free(model_);
@@ -2715,10 +2715,10 @@ struct model *load_model(const char *model_file_name)
                        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_;
@@ -2774,7 +2774,7 @@ const char *check_parameter(const problem *prob, const parameter *param)
 
        if(param->C <= 0)
                return "C <= 0";
-       
+
        if(param->p < 0)
                return "p < 0";
 
@@ -2803,7 +2803,7 @@ int check_probability_model(const struct model *model_)
 
 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;
index c0b635f7299660fc35f75c076865ec68d3723a70..48b65d9151ca49b4c6454a164652084978c62bef 100644 (file)
--- a/predict.c
+++ b/predict.c
@@ -167,7 +167,7 @@ void do_predict(FILE *input, FILE *output)
                        ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
                        ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt))
                        );
-        }
+       }
        else
                info("Accuracy = %g%% (%d/%d)\n",(double) correct/total*100,correct,total);
        if(flag_predict_probability)
diff --git a/train.c b/train.c
index c145c4b13b0aeef18402f8a8f97e63c1f7f38b0d..d3881757eaea96268b75cd079a97a94806e4123e 100644 (file)
--- a/train.c
+++ b/train.c
@@ -18,7 +18,7 @@ void exit_with_help()
        "-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"
@@ -32,12 +32,12 @@ void exit_with_help()
        "-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"
@@ -66,7 +66,7 @@ static int max_line_len;
 static char* readline(FILE *input)
 {
        int len;
-       
+
        if(fgets(line,max_line_len,input) == NULL)
                return NULL;
 
@@ -141,26 +141,26 @@ void do_cross_validation()
        double *target = Malloc(double, prob.l);
 
        cross_validation(&prob,&param,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
        {
@@ -271,20 +271,20 @@ void parse_command_line(int argc, char **argv, char *input_file_name, char *mode
        {
                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;
@@ -389,7 +389,7 @@ void read_problem(const char *filename)
        {
                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