From: biconnect Date: Wed, 1 Jul 2009 15:58:51 +0000 (+0000) Subject: Add option -q to disable the screen output from train X-Git-Tag: v134~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=82e8d9665945a5046383224261106d37c036a828;p=liblinear Add option -q to disable the screen output from train --- diff --git a/README b/README index 2440161..d1dbc32 100644 --- a/README +++ b/README @@ -110,6 +110,7 @@ options: -B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default 1) -wi weight: weights adjust the parameter C of different classes (see README for details) -v n: n-fold cross validation mode +-q : quiet mode (no outputs) Option -v randomly splits the data into n parts and calculates cross validation accuracy on them. @@ -371,6 +372,11 @@ Library Usage This function frees the memory used by a parameter set. +- Variable: extern void (*liblinear_print_string) (const char *); + + Users can specify their output format by + liblinear_print_string = &your_print_function; + Building Windows Binaries ========================= diff --git a/linear.cpp b/linear.cpp index 22373ed..afd4075 100644 --- a/linear.cpp +++ b/linear.cpp @@ -21,21 +21,26 @@ template inline void clone(T*& dst, S* src, int n) #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define INF HUGE_VAL +static void print_string_stdout(const char *s) +{ + fputs(s,stdout); + fflush(stdout); +} + +void (*liblinear_print_string) (const char *) = &print_string_stdout; + #if 1 static void info(const char *fmt,...) { + char buf[BUFSIZ]; va_list ap; va_start(ap,fmt); - vprintf(fmt,ap); + vsprintf(buf,fmt,ap); va_end(ap); -} -static void info_flush() -{ - fflush(stdout); + (*liblinear_print_string)(buf); } #else -static void info(char *fmt,...) {} -static void info_flush() {} +static void info(const char *fmt,...) {} #endif class l2_lr_fun : public function @@ -614,8 +619,7 @@ void Solver_MCSVM_CS::Solve(double *w) iter++; if(iter % 10 == 0) { - info("."); - info_flush(); + info("."); } if(stopping < eps_shrink) @@ -627,7 +631,7 @@ void Solver_MCSVM_CS::Solve(double *w) active_size = l; for(i=0;il); + tron_obj.set_print_string(liblinear_print_string); tron_obj.tron(w); delete fun_obj; break; @@ -968,6 +972,7 @@ void train_one(const problem *prob, const parameter *param, double *w, double Cp { fun_obj=new l2loss_svm_fun(prob, Cp, Cn); TRON tron_obj(fun_obj, eps*min(pos,neg)/prob->l); + tron_obj.set_print_string(liblinear_print_string); tron_obj.tron(w); delete fun_obj; break; diff --git a/linear.h b/linear.h index 5031d5a..ae85013 100644 --- a/linear.h +++ b/linear.h @@ -60,6 +60,7 @@ void get_labels(const struct model *model_, int* label); void destroy_model(struct model *model_); void destroy_param(struct parameter *param); const char *check_parameter(const struct problem *prob, const struct parameter *param); +extern void (*liblinear_print_string) (const char *); #ifdef __cplusplus } diff --git a/matlab/train.c b/matlab/train.c index 0777e10..c97ad7b 100644 --- a/matlab/train.c +++ b/matlab/train.c @@ -16,6 +16,10 @@ typedef int mwIndex; #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define INF HUGE_VAL +void print_null(const char *s){} + +void (*liblinear_default_print_string) (const char *); + void exit_with_help() { mexPrintf( @@ -89,6 +93,12 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name) col_format_flag = 0; bias = 1; + // train loaded only once under matlab + if(liblinear_default_print_string == NULL) + liblinear_default_print_string = liblinear_print_string; + else + liblinear_print_string = liblinear_default_print_string; + if(nrhs <= 1) return 1; @@ -112,7 +122,8 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name) for(i=1;i=argc) + ++i; + if(i>=argc && argv[i-1][1] != 'q') // since option -q has no parameter return 1; switch(argv[i-1][1]) { @@ -144,6 +155,10 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name) param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); param.weight[param.nr_weight-1] = atof(argv[i]); break; + case 'q': + liblinear_print_string = &print_null; + i--; + break; default: mexPrintf("unknown option\n"); return 1; diff --git a/train.c b/train.c index 62a8578..903ac35 100644 --- a/train.c +++ b/train.c @@ -8,6 +8,8 @@ #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define INF HUGE_VAL +void print_null(const char *s) {} + void exit_with_help() { printf( @@ -29,6 +31,7 @@ void exit_with_help() "-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default 1)\n" "-wi weight: weights adjust the parameter C of different classes (see README for details)\n" "-v n: n-fold cross validation mode\n" + "-q : quiet mode (no outputs)\n" ); exit(1); } @@ -179,6 +182,11 @@ void parse_command_line(int argc, char **argv, char *input_file_name, char *mode } break; + case 'q': + liblinear_print_string = &print_null; + i--; + break; + default: fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]); exit_with_help(); diff --git a/tron.cpp b/tron.cpp index 293370a..5cc829d 100644 --- a/tron.cpp +++ b/tron.cpp @@ -25,12 +25,28 @@ extern int dscal_(int *, double *, double *, int *); } #endif +void default_print(const char *buf) +{ + fputs(buf,stdout); + fflush(stdout); +} + +void TRON::info(const char *fmt,...) +{ + char buf[BUFSIZ]; + va_list ap; + va_start(ap,fmt); + vsprintf(buf,fmt,ap); + va_end(ap); + (*tron_print_string)(buf); +} TRON::TRON(const function *fun_obj, double eps, int max_iter) { this->fun_obj=const_cast(fun_obj); this->eps=eps; this->max_iter=max_iter; + tron_print_string = default_print; } TRON::~TRON() @@ -104,7 +120,7 @@ void TRON::tron(double *w) else delta = max(delta, min(alpha*snorm, sigma3*delta)); - printf("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter); + info("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter); if (actred > eta0*prered) { @@ -119,18 +135,18 @@ void TRON::tron(double *w) } if (f < -1.0e+32) { - printf("warning: f < -1.0e+32\n"); + info("warning: f < -1.0e+32\n"); break; } if (fabs(actred) <= 0 && prered <= 0) { - printf("warning: actred and prered <= 0\n"); + info("warning: actred and prered <= 0\n"); break; } if (fabs(actred) <= 1.0e-12*fabs(f) && fabs(prered) <= 1.0e-12*fabs(f)) { - printf("warning: actred and prered too small\n"); + info("warning: actred and prered too small\n"); break; } } @@ -171,7 +187,7 @@ int TRON::trcg(double delta, double *g, double *s, double *r) daxpy_(&n, &alpha, d, &inc, s, &inc); if (dnrm2_(&n, s, &inc) > delta) { - printf("cg reaches trust region boundary\n"); + info("cg reaches trust region boundary\n"); alpha = -alpha; daxpy_(&n, &alpha, d, &inc, s, &inc); @@ -212,3 +228,8 @@ double TRON::norm_inf(int n, double *x) dmax = fabs(x[i]); return(dmax); } + +void TRON::set_print_string(void (*print_string) (const char *buf)) +{ + tron_print_string = print_string; +} diff --git a/tron.h b/tron.h index fe6a96b..3045c2e 100644 --- a/tron.h +++ b/tron.h @@ -19,6 +19,7 @@ public: ~TRON(); void tron(double *w); + void set_print_string(void (*i_print) (const char *buf)); private: int trcg(double delta, double *g, double *s, double *r); @@ -27,6 +28,7 @@ private: double eps; int max_iter; function *fun_obj; + void info(const char *fmt,...); + void (*tron_print_string)(const char *buf); }; - #endif