svm_train.java
Go to the documentation of this file.
00001 import libsvm.*;
00002 import java.io.*;
00003 import java.util.*;
00004 
00005 class svm_train {
00006         private svm_parameter param;            // set by parse_command_line
00007         private svm_problem prob;               // set by read_problem
00008         private svm_model model;
00009         private String input_file_name;         // set by parse_command_line
00010         private String model_file_name;         // set by parse_command_line
00011         private String error_msg;
00012         private int cross_validation;
00013         private int nr_fold;
00014 
00015         private static svm_print_interface svm_print_null = new svm_print_interface()
00016         {
00017                 public void print(String s) {}
00018         };
00019 
00020         private static void exit_with_help()
00021         {
00022                 System.out.print(
00023                  "Usage: svm_train [options] training_set_file [model_file]\n"
00024                 +"options:\n"
00025                 +"-s svm_type : set type of SVM (default 0)\n"
00026                 +"      0 -- C-SVC\n"
00027                 +"      1 -- nu-SVC\n"
00028                 +"      2 -- one-class SVM\n"
00029                 +"      3 -- epsilon-SVR\n"
00030                 +"      4 -- nu-SVR\n"
00031                 +"-t kernel_type : set type of kernel function (default 2)\n"
00032                 +"      0 -- linear: u'*v\n"
00033                 +"      1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
00034                 +"      2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
00035                 +"      3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
00036                 +"      4 -- precomputed kernel (kernel values in training_set_file)\n"
00037                 +"-d degree : set degree in kernel function (default 3)\n"
00038                 +"-g gamma : set gamma in kernel function (default 1/num_features)\n"
00039                 +"-r coef0 : set coef0 in kernel function (default 0)\n"
00040                 +"-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n"
00041                 +"-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n"
00042                 +"-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n"
00043                 +"-m cachesize : set cache memory size in MB (default 100)\n"
00044                 +"-e epsilon : set tolerance of termination criterion (default 0.001)\n"
00045                 +"-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n"
00046                 +"-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n"
00047                 +"-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n"
00048                 +"-v n : n-fold cross validation mode\n"
00049                 +"-q : quiet mode (no outputs)\n"
00050                 );
00051                 System.exit(1);
00052         }
00053 
00054         private void do_cross_validation()
00055         {
00056                 int i;
00057                 int total_correct = 0;
00058                 double total_error = 0;
00059                 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
00060                 double[] target = new double[prob.l];
00061 
00062                 svm.svm_cross_validation(prob,param,nr_fold,target);
00063                 if(param.svm_type == svm_parameter.EPSILON_SVR ||
00064                    param.svm_type == svm_parameter.NU_SVR)
00065                 {
00066                         for(i=0;i<prob.l;i++)
00067                         {
00068                                 double y = prob.y[i];
00069                                 double v = target[i];
00070                                 total_error += (v-y)*(v-y);
00071                                 sumv += v;
00072                                 sumy += y;
00073                                 sumvv += v*v;
00074                                 sumyy += y*y;
00075                                 sumvy += v*y;
00076                         }
00077                         System.out.print("Cross Validation Mean squared error = "+total_error/prob.l+"\n");
00078                         System.out.print("Cross Validation Squared correlation coefficient = "+
00079                                 ((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
00080                                 ((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))+"\n"
00081                                 );
00082                 }
00083                 else
00084                 {
00085                         for(i=0;i<prob.l;i++)
00086                                 if(target[i] == prob.y[i])
00087                                         ++total_correct;
00088                         System.out.print("Cross Validation Accuracy = "+100.0*total_correct/prob.l+"%\n");
00089                 }
00090         }
00091         
00092         private void run(String argv[]) throws IOException
00093         {
00094                 parse_command_line(argv);
00095                 read_problem();
00096                 error_msg = svm.svm_check_parameter(prob,param);
00097 
00098                 if(error_msg != null)
00099                 {
00100                         System.err.print("ERROR: "+error_msg+"\n");
00101                         System.exit(1);
00102                 }
00103 
00104                 if(cross_validation != 0)
00105                 {
00106                         do_cross_validation();
00107                 }
00108                 else
00109                 {
00110                         model = svm.svm_train(prob,param);
00111                         svm.svm_save_model(model_file_name,model);
00112                 }
00113         }
00114 
00115         public static void main(String argv[]) throws IOException
00116         {
00117                 svm_train t = new svm_train();
00118                 t.run(argv);
00119         }
00120 
00121         private static double atof(String s)
00122         {
00123                 double d = Double.valueOf(s).doubleValue();
00124                 if (Double.isNaN(d) || Double.isInfinite(d))
00125                 {
00126                         System.err.print("NaN or Infinity in input\n");
00127                         System.exit(1);
00128                 }
00129                 return(d);
00130         }
00131 
00132         private static int atoi(String s)
00133         {
00134                 return Integer.parseInt(s);
00135         }
00136 
00137         private void parse_command_line(String argv[])
00138         {
00139                 int i;
00140                 svm_print_interface print_func = null;  // default printing to stdout
00141 
00142                 param = new svm_parameter();
00143                 // default values
00144                 param.svm_type = svm_parameter.C_SVC;
00145                 param.kernel_type = svm_parameter.RBF;
00146                 param.degree = 3;
00147                 param.gamma = 0;        // 1/num_features
00148                 param.coef0 = 0;
00149                 param.nu = 0.5;
00150                 param.cache_size = 100;
00151                 param.C = 1;
00152                 param.eps = 1e-3;
00153                 param.p = 0.1;
00154                 param.shrinking = 1;
00155                 param.probability = 0;
00156                 param.nr_weight = 0;
00157                 param.weight_label = new int[0];
00158                 param.weight = new double[0];
00159                 cross_validation = 0;
00160 
00161                 // parse options
00162                 for(i=0;i<argv.length;i++)
00163                 {
00164                         if(argv[i].charAt(0) != '-') break;
00165                         if(++i>=argv.length)
00166                                 exit_with_help();
00167                         switch(argv[i-1].charAt(1))
00168                         {
00169                                 case 's':
00170                                         param.svm_type = atoi(argv[i]);
00171                                         break;
00172                                 case 't':
00173                                         param.kernel_type = atoi(argv[i]);
00174                                         break;
00175                                 case 'd':
00176                                         param.degree = atoi(argv[i]);
00177                                         break;
00178                                 case 'g':
00179                                         param.gamma = atof(argv[i]);
00180                                         break;
00181                                 case 'r':
00182                                         param.coef0 = atof(argv[i]);
00183                                         break;
00184                                 case 'n':
00185                                         param.nu = atof(argv[i]);
00186                                         break;
00187                                 case 'm':
00188                                         param.cache_size = atof(argv[i]);
00189                                         break;
00190                                 case 'c':
00191                                         param.C = atof(argv[i]);
00192                                         break;
00193                                 case 'e':
00194                                         param.eps = atof(argv[i]);
00195                                         break;
00196                                 case 'p':
00197                                         param.p = atof(argv[i]);
00198                                         break;
00199                                 case 'h':
00200                                         param.shrinking = atoi(argv[i]);
00201                                         break;
00202                                 case 'b':
00203                                         param.probability = atoi(argv[i]);
00204                                         break;
00205                                 case 'q':
00206                                         print_func = svm_print_null;
00207                                         i--;
00208                                         break;
00209                                 case 'v':
00210                                         cross_validation = 1;
00211                                         nr_fold = atoi(argv[i]);
00212                                         if(nr_fold < 2)
00213                                         {
00214                                                 System.err.print("n-fold cross validation: n must >= 2\n");
00215                                                 exit_with_help();
00216                                         }
00217                                         break;
00218                                 case 'w':
00219                                         ++param.nr_weight;
00220                                         {
00221                                                 int[] old = param.weight_label;
00222                                                 param.weight_label = new int[param.nr_weight];
00223                                                 System.arraycopy(old,0,param.weight_label,0,param.nr_weight-1);
00224                                         }
00225 
00226                                         {
00227                                                 double[] old = param.weight;
00228                                                 param.weight = new double[param.nr_weight];
00229                                                 System.arraycopy(old,0,param.weight,0,param.nr_weight-1);
00230                                         }
00231 
00232                                         param.weight_label[param.nr_weight-1] = atoi(argv[i-1].substring(2));
00233                                         param.weight[param.nr_weight-1] = atof(argv[i]);
00234                                         break;
00235                                 default:
00236                                         System.err.print("Unknown option: " + argv[i-1] + "\n");
00237                                         exit_with_help();
00238                         }
00239                 }
00240 
00241                 svm.svm_set_print_string_function(print_func);
00242 
00243                 // determine filenames
00244 
00245                 if(i>=argv.length)
00246                         exit_with_help();
00247 
00248                 input_file_name = argv[i];
00249 
00250                 if(i<argv.length-1)
00251                         model_file_name = argv[i+1];
00252                 else
00253                 {
00254                         int p = argv[i].lastIndexOf('/');
00255                         ++p;    // whew...
00256                         model_file_name = argv[i].substring(p)+".model";
00257                 }
00258         }
00259 
00260         // read in a problem (in svmlight format)
00261 
00262         private void read_problem() throws IOException
00263         {
00264                 BufferedReader fp = new BufferedReader(new FileReader(input_file_name));
00265                 Vector<Double> vy = new Vector<Double>();
00266                 Vector<svm_node[]> vx = new Vector<svm_node[]>();
00267                 int max_index = 0;
00268 
00269                 while(true)
00270                 {
00271                         String line = fp.readLine();
00272                         if(line == null) break;
00273 
00274                         StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
00275 
00276                         vy.addElement(atof(st.nextToken()));
00277                         int m = st.countTokens()/2;
00278                         svm_node[] x = new svm_node[m];
00279                         for(int j=0;j<m;j++)
00280                         {
00281                                 x[j] = new svm_node();
00282                                 x[j].index = atoi(st.nextToken());
00283                                 x[j].value = atof(st.nextToken());
00284                         }
00285                         if(m>0) max_index = Math.max(max_index, x[m-1].index);
00286                         vx.addElement(x);
00287                 }
00288 
00289                 prob = new svm_problem();
00290                 prob.l = vy.size();
00291                 prob.x = new svm_node[prob.l][];
00292                 for(int i=0;i<prob.l;i++)
00293                         prob.x[i] = vx.elementAt(i);
00294                 prob.y = new double[prob.l];
00295                 for(int i=0;i<prob.l;i++)
00296                         prob.y[i] = vy.elementAt(i);
00297 
00298                 if(param.gamma == 0 && max_index > 0)
00299                         param.gamma = 1.0/max_index;
00300 
00301                 if(param.kernel_type == svm_parameter.PRECOMPUTED)
00302                         for(int i=0;i<prob.l;i++)
00303                         {
00304                                 if (prob.x[i][0].index != 0)
00305                                 {
00306                                         System.err.print("Wrong kernel matrix: first column must be 0:sample_serial_number\n");
00307                                         System.exit(1);
00308                                 }
00309                                 if ((int)prob.x[i][0].value <= 0 || (int)prob.x[i][0].value > max_index)
00310                                 {
00311                                         System.err.print("Wrong input format: sample_serial_number out of range\n");
00312                                         System.exit(1);
00313                                 }
00314                         }
00315 
00316                 fp.close();
00317         }
00318 }


haf_grasping
Author(s): David Fischinger
autogenerated on Wed Jan 11 2017 03:48:49