48 char **model_inputfile,
char **training_inputfile,
49 char **testing_inputfile,
char **model_outputfile,
50 char **prediction_outputfile);
65 printf(
"Copyright (C) 2016, G.J.J. van den Burg.\n");
66 printf(
"This program is free software, see the LICENSE file " 68 printf(
"Usage: %s [options] training_data [test_data]\n\n", argv[0]);
71 printf(
"-c coef : coefficient for the polynomial and " 73 printf(
"-d degree : degree for the polynomial kernel\n");
74 printf(
"-e epsilon : set the value of the stopping " 75 "criterion (epsilon > 0)\n");
76 printf(
"-g gamma : parameter for the rbf, polynomial or " 78 printf(
"-h | -help : print this help.\n");
79 printf(
"-k kappa : set the value of kappa used in the " 80 "Huber hinge (kappa > -1.0)\n");
81 printf(
"-l lambda : set the value of lambda " 83 printf(
"-m model_output_file : write model output to file " 84 "(not saved if no file provided)\n");
85 printf(
"-o prediction_output : write predictions of test data to " 86 "file (uses stdout if not provided)\n");
87 printf(
"-p p-value : set the value of p in the lp norm " 88 "(1.0 <= p <= 2.0)\n");
89 printf(
"-q : quiet mode (no output, not even " 91 printf(
"-r rho : choose the weigth specification " 92 "(1 = unit, 2 = group)\n");
93 printf(
"-s seed_model_file : use previous model as seed for V\n");
94 printf(
"-t type : kerneltype (0=LINEAR, 1=POLY, 2=RBF, " 96 printf(
"-x : data files are in LibSVM/SVMlight " 114 int main(
int argc,
char **argv)
116 bool libsvm_format =
false;
117 long i, *predy = NULL;
120 char *training_inputfile = NULL,
121 *testing_inputfile = NULL,
122 *model_inputfile = NULL,
123 *model_outputfile = NULL,
124 *prediction_outputfile = NULL;
137 &training_inputfile, &testing_inputfile,
138 &model_outputfile, &prediction_outputfile);
149 err(
"[GenSVM Error]: Class labels should start from 1 and " 150 "have no gaps. Please reformat your data.\n");
156 strcpy(model->
data_file, training_inputfile);
160 err(
"[GenSVM Warning]: Sparse matrices with nonlinear kernels " 161 "are not yet supported. Dense matrices will " 164 traindata->
Z = traindata->
RAW;
182 if (testing_inputfile != NULL) {
191 err(
"[GenSVM Warning]: Sparse matrices with nonlinear " 192 "kernels are not yet supported. Dense " 193 "matrices will be used.\n");
201 predy =
Calloc(
long, testdata->
n);
204 if (testdata->
y != NULL) {
206 note(
"Predictive performance: %3.2f%%\n", performance);
212 prediction_outputfile);
213 note(
"Prediction written to: %s\n",
214 prediction_outputfile);
216 for (i=0; i<testdata->
n; i++)
217 printf(
"%li ", predy[i]);
225 note(
"Model written to: %s\n", model_outputfile);
233 free(training_inputfile);
234 free(testing_inputfile);
235 free(model_inputfile);
236 free(model_outputfile);
237 free(prediction_outputfile);
252 fprintf(stderr,
"Invalid parameter value for %s.\n\n", label);
276 char **model_inputfile,
char **training_inputfile,
277 char **testing_inputfile,
char **model_outputfile,
278 char **prediction_outputfile)
287 for (i=1; i<argc; i++) {
288 if (argv[i][0] !=
'-')
break;
292 switch (argv[i-1][1]) {
294 model->
coef = atof(argv[i]);
297 model->
degree = atof(argv[i]);
300 model->
epsilon = atof(argv[i]);
305 model->
gamma = atof(argv[i]);
308 model->
kappa = atof(argv[i]);
309 if (model->
kappa <= -1.0)
313 model->
lambda = atof(argv[i]);
318 (*model_inputfile) =
Malloc(
char,
320 strcpy((*model_inputfile), argv[i]);
323 (*model_outputfile) =
Malloc(
char,
325 strcpy((*model_outputfile), argv[i]);
328 (*prediction_outputfile) =
Malloc(
char,
330 strcpy((*prediction_outputfile), argv[i]);
333 model->
p = atof(argv[i]);
334 if (model->
p < 1.0 || model->
p > 2.0)
355 fprintf(stderr,
"Unknown option: -%c\n",
363 (*training_inputfile) =
Malloc(
char, strlen(argv[i])+1);
364 strcpy((*training_inputfile), argv[i]);
366 (*testing_inputfile) =
Malloc(
char, strlen(argv[i])+1);
367 strcpy((*testing_inputfile), argv[i+1]);
#define Calloc(type, size)
void gensvm_write_predictions(struct GenData *data, long *predy, char *output_filename)
Write predictions to file.
Header file for gensvm_predict.c.
double epsilon
stopping criterion for the IM algorithm.
void err(const char *fmt,...)
Parse a formatted string and write it to standard error.
void gensvm_read_model(struct GenModel *model, char *model_filename)
Read model from file.
double p
parameter for the L-p norm in the loss function
void gensvm_read_data(struct GenData *dataset, char *data_file)
Read data from file.
#define GENSVM_MAX_LINE_LENGTH
int gensvm_check_argv_eq(int argc, char **argv, char *str)
Check if a command line argument equals a string.
void gensvm_free_sparse(struct GenSparse *sp)
Free an allocated GenSparse structure.
double gensvm_prediction_perf(struct GenData *data, long *perdy)
Calculate the predictive performance (percentage correct)
void parse_command_line(int argc, char **argv, struct GenModel *model, char **model_inputfile, char **training_inputfile, char **testing_inputfile, char **model_outputfile, char **prediction_outputfile)
Parse the command line arguments.
void gensvm_free_model(struct GenModel *model)
Free allocated GenModel struct.
long i
index used for keeping track of the queue
int weight_idx
which weights to use (1 = unit, 2 = group)
FILE * GENSVM_OUTPUT_FILE
#define Malloc(type, size)
bool gensvm_check_outcome_contiguous(struct GenData *data)
Check if the labels are contiguous on [1 .. K].
Header file for gensvm_checks.c.
long * y
array of class labels, 1..K
struct GenModel * gensvm_init_model(void)
Initialize a GenModel structure.
A structure to represent the data.
void gensvm_train(struct GenModel *model, struct GenData *data, struct GenModel *seed_model)
Utility function for training a GenSVM model.
Header file for gensvm_train.c.
A structure to represent a single GenSVM model.
void gensvm_write_model(struct GenModel *model, char *output_filename)
Write model to file.
void gensvm_predict_labels(struct GenData *testdata, struct GenModel *model, long *predy)
Predict class labels of data given and output in predy.
Header file for gensvm_cmdarg.c.
char * data_file
filename of the data
void exit_with_help(char **argv)
Help function.
int main(int argc, char **argv)
Main interface function for GenSVMtraintest.
void gensvm_free_data(struct GenData *data)
Free allocated GenData struct.
Header file for gensvm_io.c.
double * gensvm_sparse_to_dense(struct GenSparse *A)
Convert a GenSparse structure to a dense matrix.
double kappa
parameter for the Huber hinge function
double degree
kernel parameter for poly
int gensvm_check_argv(int argc, char **argv, char *str)
Check if any command line arguments contain string.
double coef
kernel parameter for poly and sigmoid
KernelType kerneltype
type of kernel used in the model
double gamma
kernel parameter for RBF, poly, and sigmoid
void exit_invalid_param(const char *label, char **argv)
Exit with warning about invalid parameter value.
void gensvm_kernel_postprocess(struct GenModel *model, struct GenData *traindata, struct GenData *testdata)
Compute the kernel postprocessing factor.
long n
number of instances
struct GenData * gensvm_init_data(void)
Initialize a GenData structure.
void gensvm_read_data_libsvm(struct GenData *dataset, char *data_file)
Read data from a file in LibSVM/SVMlight format.
double * RAW
augmented raw data matrix
struct GenSparse * spZ
sparse representation of the augmented data matrix
double lambda
regularization parameter in the loss function
void note(const char *fmt,...)
Parse a formatted string and write to the output stream.