comparison toolboxes/SVM-light/src/svm_learn_main.c @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 /***********************************************************************/
2 /* */
3 /* svm_learn_main.c */
4 /* */
5 /* Command line interface to the learning module of the */
6 /* Support Vector Machine. */
7 /* */
8 /* Author: Thorsten Joachims */
9 /* Date: 02.07.02 */
10 /* */
11 /* Copyright (c) 2000 Thorsten Joachims - All rights reserved */
12 /* */
13 /* This software is available for non-commercial use only. It must */
14 /* not be modified and distributed without prior permission of the */
15 /* author. The author is not responsible for implications from the */
16 /* use of this software. */
17 /* */
18 /***********************************************************************/
19
20
21 /* uncomment, if you want to use svm-learn out of C++ */
22 /* extern "C" { */
23 # include "svm_common.h"
24 # include "svm_learn.h"
25 /* } */
26
27 char docfile[200]; /* file with training examples */
28 char modelfile[200]; /* file for resulting classifier */
29 char restartfile[200]; /* file with initial alphas */
30
31 void read_input_parameters(int, char **, char *, char *, char *, long *,
32 LEARN_PARM *, KERNEL_PARM *);
33 void wait_any_key();
34 void print_help();
35
36
37
38 int main (int argc, char* argv[])
39 {
40 DOC **docs; /* training examples */
41 long totwords,totdoc,i;
42 double *target;
43 double *alpha_in=NULL;
44 KERNEL_CACHE *kernel_cache;
45 LEARN_PARM learn_parm;
46 KERNEL_PARM kernel_parm;
47 MODEL *model=(MODEL *)my_malloc(sizeof(MODEL));
48
49 read_input_parameters(argc,argv,docfile,modelfile,restartfile,&verbosity,
50 &learn_parm,&kernel_parm);
51 read_documents(docfile,&docs,&target,&totwords,&totdoc);
52 if(restartfile[0]) alpha_in=read_alphas(restartfile,totdoc);
53
54 if(kernel_parm.kernel_type == LINEAR) { /* don't need the cache */
55 kernel_cache=NULL;
56 }
57 else {
58 /* Always get a new kernel cache. It is not possible to use the
59 same cache for two different training runs */
60 kernel_cache=kernel_cache_init(totdoc,learn_parm.kernel_cache_size);
61 }
62
63 if(learn_parm.type == CLASSIFICATION) {
64 svm_learn_classification(docs,target,totdoc,totwords,&learn_parm,
65 &kernel_parm,kernel_cache,model,alpha_in);
66 }
67 else if(learn_parm.type == REGRESSION) {
68 svm_learn_regression(docs,target,totdoc,totwords,&learn_parm,
69 &kernel_parm,&kernel_cache,model);
70 }
71 else if(learn_parm.type == RANKING) {
72 svm_learn_ranking(docs,target,totdoc,totwords,&learn_parm,
73 &kernel_parm,&kernel_cache,model);
74 }
75 else if(learn_parm.type == OPTIMIZATION) {
76 svm_learn_optimization(docs,target,totdoc,totwords,&learn_parm,
77 &kernel_parm,kernel_cache,model,alpha_in);
78 }
79
80 if(kernel_cache) {
81 /* Free the memory used for the cache. */
82 kernel_cache_cleanup(kernel_cache);
83 }
84
85 /* Warning: The model contains references to the original data 'docs'.
86 If you want to free the original data, and only keep the model, you
87 have to make a deep copy of 'model'. */
88 /* deep_copy_of_model=copy_model(model); */
89 write_model(modelfile,model);
90
91 free(alpha_in);
92 free_model(model,0);
93 for(i=0;i<totdoc;i++)
94 free_example(docs[i],1);
95 free(docs);
96 free(target);
97
98 return(0);
99 }
100
101 /*---------------------------------------------------------------------------*/
102
103 void read_input_parameters(int argc,char *argv[],char *docfile,char *modelfile,
104 char *restartfile,long *verbosity,
105 LEARN_PARM *learn_parm,KERNEL_PARM *kernel_parm)
106 {
107 long i;
108 char type[100];
109
110 /* set default */
111 strcpy (modelfile, "svm_model");
112 strcpy (learn_parm->predfile, "trans_predictions");
113 strcpy (learn_parm->alphafile, "");
114 strcpy (restartfile, "");
115 (*verbosity)=1;
116 learn_parm->biased_hyperplane=1;
117 learn_parm->sharedslack=0;
118 learn_parm->remove_inconsistent=0;
119 learn_parm->skip_final_opt_check=0;
120 learn_parm->svm_maxqpsize=10;
121 learn_parm->svm_newvarsinqp=0;
122 learn_parm->svm_iter_to_shrink=-9999;
123 learn_parm->maxiter=100000;
124 learn_parm->kernel_cache_size=40;
125 learn_parm->svm_c=0.0;
126 learn_parm->eps=0.1;
127 learn_parm->transduction_posratio=-1.0;
128 learn_parm->svm_costratio=1.0;
129 learn_parm->svm_costratio_unlab=1.0;
130 learn_parm->svm_unlabbound=1E-5;
131 learn_parm->epsilon_crit=0.001;
132 learn_parm->epsilon_a=1E-15;
133 learn_parm->compute_loo=0;
134 learn_parm->rho=1.0;
135 learn_parm->xa_depth=0;
136 kernel_parm->kernel_type=0;
137 kernel_parm->poly_degree=3;
138 kernel_parm->rbf_gamma=1.0;
139 kernel_parm->coef_lin=1;
140 kernel_parm->coef_const=1;
141 strcpy(kernel_parm->custom,"empty");
142 strcpy(type,"c");
143
144 for(i=1;(i<argc) && ((argv[i])[0] == '-');i++) {
145 switch ((argv[i])[1])
146 {
147 case '?': print_help(); exit(0);
148 case 'z': i++; strcpy(type,argv[i]); break;
149 case 'v': i++; (*verbosity)=atol(argv[i]); break;
150 case 'b': i++; learn_parm->biased_hyperplane=atol(argv[i]); break;
151 case 'i': i++; learn_parm->remove_inconsistent=atol(argv[i]); break;
152 case 'f': i++; learn_parm->skip_final_opt_check=!atol(argv[i]); break;
153 case 'q': i++; learn_parm->svm_maxqpsize=atol(argv[i]); break;
154 case 'n': i++; learn_parm->svm_newvarsinqp=atol(argv[i]); break;
155 case '#': i++; learn_parm->maxiter=atol(argv[i]); break;
156 case 'h': i++; learn_parm->svm_iter_to_shrink=atol(argv[i]); break;
157 case 'm': i++; learn_parm->kernel_cache_size=atol(argv[i]); break;
158 case 'c': i++; learn_parm->svm_c=atof(argv[i]); break;
159 case 'w': i++; learn_parm->eps=atof(argv[i]); break;
160 case 'p': i++; learn_parm->transduction_posratio=atof(argv[i]); break;
161 case 'j': i++; learn_parm->svm_costratio=atof(argv[i]); break;
162 case 'e': i++; learn_parm->epsilon_crit=atof(argv[i]); break;
163 case 'o': i++; learn_parm->rho=atof(argv[i]); break;
164 case 'k': i++; learn_parm->xa_depth=atol(argv[i]); break;
165 case 'x': i++; learn_parm->compute_loo=atol(argv[i]); break;
166 case 't': i++; kernel_parm->kernel_type=atol(argv[i]); break;
167 case 'd': i++; kernel_parm->poly_degree=atol(argv[i]); break;
168 case 'g': i++; kernel_parm->rbf_gamma=atof(argv[i]); break;
169 case 's': i++; kernel_parm->coef_lin=atof(argv[i]); break;
170 case 'r': i++; kernel_parm->coef_const=atof(argv[i]); break;
171 case 'u': i++; strcpy(kernel_parm->custom,argv[i]); break;
172 case 'l': i++; strcpy(learn_parm->predfile,argv[i]); break;
173 case 'a': i++; strcpy(learn_parm->alphafile,argv[i]); break;
174 case 'y': i++; strcpy(restartfile,argv[i]); break;
175 default: printf("\nUnrecognized option %s!\n\n",argv[i]);
176 print_help();
177 exit(0);
178 }
179 }
180 if(i>=argc) {
181 printf("\nNot enough input parameters!\n\n");
182 wait_any_key();
183 print_help();
184 exit(0);
185 }
186 strcpy (docfile, argv[i]);
187 if((i+1)<argc) {
188 strcpy (modelfile, argv[i+1]);
189 }
190 if(learn_parm->svm_iter_to_shrink == -9999) {
191 if(kernel_parm->kernel_type == LINEAR)
192 learn_parm->svm_iter_to_shrink=2;
193 else
194 learn_parm->svm_iter_to_shrink=100;
195 }
196 if(strcmp(type,"c")==0) {
197 learn_parm->type=CLASSIFICATION;
198 }
199 else if(strcmp(type,"r")==0) {
200 learn_parm->type=REGRESSION;
201 }
202 else if(strcmp(type,"p")==0) {
203 learn_parm->type=RANKING;
204 }
205 else if(strcmp(type,"o")==0) {
206 learn_parm->type=OPTIMIZATION;
207 }
208 else if(strcmp(type,"s")==0) {
209 learn_parm->type=OPTIMIZATION;
210 learn_parm->sharedslack=1;
211 }
212 else {
213 printf("\nUnknown type '%s': Valid types are 'c' (classification), 'r' regession, and 'p' preference ranking.\n",type);
214 wait_any_key();
215 print_help();
216 exit(0);
217 }
218 if((learn_parm->skip_final_opt_check)
219 && (kernel_parm->kernel_type == LINEAR)) {
220 printf("\nIt does not make sense to skip the final optimality check for linear kernels.\n\n");
221 learn_parm->skip_final_opt_check=0;
222 }
223 if((learn_parm->skip_final_opt_check)
224 && (learn_parm->remove_inconsistent)) {
225 printf("\nIt is necessary to do the final optimality check when removing inconsistent \nexamples.\n");
226 wait_any_key();
227 print_help();
228 exit(0);
229 }
230 if((learn_parm->svm_maxqpsize<2)) {
231 printf("\nMaximum size of QP-subproblems not in valid range: %ld [2..]\n",learn_parm->svm_maxqpsize);
232 wait_any_key();
233 print_help();
234 exit(0);
235 }
236 if((learn_parm->svm_maxqpsize<learn_parm->svm_newvarsinqp)) {
237 printf("\nMaximum size of QP-subproblems [%ld] must be larger than the number of\n",learn_parm->svm_maxqpsize);
238 printf("new variables [%ld] entering the working set in each iteration.\n",learn_parm->svm_newvarsinqp);
239 wait_any_key();
240 print_help();
241 exit(0);
242 }
243 if(learn_parm->svm_iter_to_shrink<1) {
244 printf("\nMaximum number of iterations for shrinking not in valid range: %ld [1,..]\n",learn_parm->svm_iter_to_shrink);
245 wait_any_key();
246 print_help();
247 exit(0);
248 }
249 if(learn_parm->svm_c<0) {
250 printf("\nThe C parameter must be greater than zero!\n\n");
251 wait_any_key();
252 print_help();
253 exit(0);
254 }
255 if(learn_parm->transduction_posratio>1) {
256 printf("\nThe fraction of unlabeled examples to classify as positives must\n");
257 printf("be less than 1.0 !!!\n\n");
258 wait_any_key();
259 print_help();
260 exit(0);
261 }
262 if(learn_parm->svm_costratio<=0) {
263 printf("\nThe COSTRATIO parameter must be greater than zero!\n\n");
264 wait_any_key();
265 print_help();
266 exit(0);
267 }
268 if(learn_parm->epsilon_crit<=0) {
269 printf("\nThe epsilon parameter must be greater than zero!\n\n");
270 wait_any_key();
271 print_help();
272 exit(0);
273 }
274 if(learn_parm->rho<0) {
275 printf("\nThe parameter rho for xi/alpha-estimates and leave-one-out pruning must\n");
276 printf("be greater than zero (typically 1.0 or 2.0, see T. Joachims, Estimating the\n");
277 printf("Generalization Performance of an SVM Efficiently, ICML, 2000.)!\n\n");
278 wait_any_key();
279 print_help();
280 exit(0);
281 }
282 if((learn_parm->xa_depth<0) || (learn_parm->xa_depth>100)) {
283 printf("\nThe parameter depth for ext. xi/alpha-estimates must be in [0..100] (zero\n");
284 printf("for switching to the conventional xa/estimates described in T. Joachims,\n");
285 printf("Estimating the Generalization Performance of an SVM Efficiently, ICML, 2000.)\n");
286 wait_any_key();
287 print_help();
288 exit(0);
289 }
290 }
291
292 void wait_any_key()
293 {
294 printf("\n(more)\n");
295 (void)getc(stdin);
296 }
297
298 void print_help()
299 {
300 printf("\nSVM-light %s: Support Vector Machine, learning module %s\n",VERSION,VERSION_DATE);
301 copyright_notice();
302 printf(" usage: svm_learn [options] example_file model_file\n\n");
303 printf("Arguments:\n");
304 printf(" example_file-> file with training data\n");
305 printf(" model_file -> file to store learned decision rule in\n");
306
307 printf("General options:\n");
308 printf(" -? -> this help\n");
309 printf(" -v [0..3] -> verbosity level (default 1)\n");
310 printf("Learning options:\n");
311 printf(" -z {c,r,p} -> select between classification (c), regression (r),\n");
312 printf(" and preference ranking (p) (default classification)\n");
313 printf(" -c float -> C: trade-off between training error\n");
314 printf(" and margin (default [avg. x*x]^-1)\n");
315 printf(" -w [0..] -> epsilon width of tube for regression\n");
316 printf(" (default 0.1)\n");
317 printf(" -j float -> Cost: cost-factor, by which training errors on\n");
318 printf(" positive examples outweight errors on negative\n");
319 printf(" examples (default 1) (see [4])\n");
320 printf(" -b [0,1] -> use biased hyperplane (i.e. x*w+b>0) instead\n");
321 printf(" of unbiased hyperplane (i.e. x*w>0) (default 1)\n");
322 printf(" -i [0,1] -> remove inconsistent training examples\n");
323 printf(" and retrain (default 0)\n");
324 printf("Performance estimation options:\n");
325 printf(" -x [0,1] -> compute leave-one-out estimates (default 0)\n");
326 printf(" (see [5])\n");
327 printf(" -o ]0..2] -> value of rho for XiAlpha-estimator and for pruning\n");
328 printf(" leave-one-out computation (default 1.0) (see [2])\n");
329 printf(" -k [0..100] -> search depth for extended XiAlpha-estimator \n");
330 printf(" (default 0)\n");
331 printf("Transduction options (see [3]):\n");
332 printf(" -p [0..1] -> fraction of unlabeled examples to be classified\n");
333 printf(" into the positive class (default is the ratio of\n");
334 printf(" positive and negative examples in the training data)\n");
335 printf("Kernel options:\n");
336 printf(" -t int -> type of kernel function:\n");
337 printf(" 0: linear (default)\n");
338 printf(" 1: polynomial (s a*b+c)^d\n");
339 printf(" 2: radial basis function exp(-gamma ||a-b||^2)\n");
340 printf(" 3: sigmoid tanh(s a*b + c)\n");
341 printf(" 4: user defined kernel from kernel.h\n");
342 printf(" -d int -> parameter d in polynomial kernel\n");
343 printf(" -g float -> parameter gamma in rbf kernel\n");
344 printf(" -s float -> parameter s in sigmoid/poly kernel\n");
345 printf(" -r float -> parameter c in sigmoid/poly kernel\n");
346 printf(" -u string -> parameter of user defined kernel\n");
347 printf("Optimization options (see [1]):\n");
348 printf(" -q [2..] -> maximum size of QP-subproblems (default 10)\n");
349 printf(" -n [2..q] -> number of new variables entering the working set\n");
350 printf(" in each iteration (default n = q). Set n<q to prevent\n");
351 printf(" zig-zagging.\n");
352 printf(" -m [5..] -> size of cache for kernel evaluations in MB (default 40)\n");
353 printf(" The larger the faster...\n");
354 printf(" -e float -> eps: Allow that error for termination criterion\n");
355 printf(" [y [w*x+b] - 1] >= eps (default 0.001)\n");
356 printf(" -y [0,1] -> restart the optimization from alpha values in file\n");
357 printf(" specified by -a option. (default 0)\n");
358 printf(" -h [5..] -> number of iterations a variable needs to be\n");
359 printf(" optimal before considered for shrinking (default 100)\n");
360 printf(" -f [0,1] -> do final optimality check for variables removed\n");
361 printf(" by shrinking. Although this test is usually \n");
362 printf(" positive, there is no guarantee that the optimum\n");
363 printf(" was found if the test is omitted. (default 1)\n");
364 printf(" -y string -> if option is given, reads alphas from file with given\n");
365 printf(" and uses them as starting point. (default 'disabled')\n");
366 printf(" -# int -> terminate optimization, if no progress after this\n");
367 printf(" number of iterations. (default 100000)\n");
368 printf("Output options:\n");
369 printf(" -l string -> file to write predicted labels of unlabeled\n");
370 printf(" examples into after transductive learning\n");
371 printf(" -a string -> write all alphas to this file after learning\n");
372 printf(" (in the same order as in the training set)\n");
373 wait_any_key();
374 printf("\nMore details in:\n");
375 printf("[1] T. Joachims, Making Large-Scale SVM Learning Practical. Advances in\n");
376 printf(" Kernel Methods - Support Vector Learning, B. Schölkopf and C. Burges and\n");
377 printf(" A. Smola (ed.), MIT Press, 1999.\n");
378 printf("[2] T. Joachims, Estimating the Generalization performance of an SVM\n");
379 printf(" Efficiently. International Conference on Machine Learning (ICML), 2000.\n");
380 printf("[3] T. Joachims, Transductive Inference for Text Classification using Support\n");
381 printf(" Vector Machines. International Conference on Machine Learning (ICML),\n");
382 printf(" 1999.\n");
383 printf("[4] K. Morik, P. Brockhausen, and T. Joachims, Combining statistical learning\n");
384 printf(" with a knowledge-based approach - A case study in intensive care \n");
385 printf(" monitoring. International Conference on Machine Learning (ICML), 1999.\n");
386 printf("[5] T. Joachims, Learning to Classify Text Using Support Vector\n");
387 printf(" Machines: Methods, Theory, and Algorithms. Dissertation, Kluwer,\n");
388 printf(" 2002.\n\n");
389 }
390
391