aboutsummaryrefslogtreecommitdiff
path: root/src/GenSVMgrid.c
blob: 6f5843d51f11cf6a01ea3421ff471a14d546493c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
 * @file GenSVMgrid.c
 * @author G.J.J. van den Burg
 * @date 2014-01-07
 * @brief Command line interface for the grid search program
 *
 * @details
 * This is a command line interface to the parameter grid search functionality
 * of the algorithm. The grid search is specified in a separate file, thereby
 * reducing the number of command line arguments. See
 * read_grid_from_file() for documentation on the grid file.
 *
 * The program runs a grid search as specified in the grid file. If
 * desired the grid search can incorporate consistency checks to find the
 * configuration among the best configurations which scores consistently high.
 * All output is written to stdout, unless the quiet mode is specified.
 *
 * For further usage information, see the program help function.
 *
 * @copyright
 Copyright 2016, G.J.J. van den Burg.

 This file is part of GenSVM.

 GenSVM is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 GenSVM is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GenSVM. If not, see <http://www.gnu.org/licenses/>.

 */

#include "gensvm_checks.h"
#include "gensvm_cmdarg.h"
#include "gensvm_io.h"
#include "gensvm_gridsearch.h"
#include "gensvm_consistency.h"

/**
 * Minimal number of command line arguments
 */
#define MINARGS 2

extern FILE *GENSVM_OUTPUT_FILE;
extern FILE *GENSVM_ERROR_FILE;

// function declarations
void exit_with_help(char **argv);
void parse_command_line(int argc, char **argv, char *input_filename);
void read_grid_from_file(char *input_filename, struct GenGrid *grid);

/**
 * @brief Help function
 *
 * @details
 * Print help for this program and exit. Note that VERSION is provided by the
 * Makefile.
 *
 * @param[in] 	argv 	command line arguments
 *
 */
void exit_with_help(char **argv)
{
	printf("This is GenSVM, version %1.1f.\n", VERSION);
	printf("Copyright (C) 2016, G.J.J. van den Burg.\n");
	printf("This program is free software, see the LICENSE file "
			"for details.\n\n");
	printf("Usage: %s [options] grid_file\n", argv[0]);
	printf("Options:\n");
	printf("-h | -help : print this help.\n");
	printf("-q         : quiet mode (no output, not even errors!)\n");
	printf("-x         : data files are in LibSVM/SVMlight format\n");

	exit(EXIT_FAILURE);
}

/**
 * @brief Main interface function for GenSVMgrid
 *
 * @details
 * Main interface for the command line program. A given grid file which
 * specifies a grid search over a single dataset is read. From this, a Queue
 * is created containing all Task instances that need to be performed in the
 * search. Depending on the type of dataset, either cross validation or
 * train/test split training is performed for all tasks. If specified,
 * consistency repeats are done at the end of the grid search. Note that
 * currently no output is produced other than what is written to stdout.
 *
 * @param[in] 	argc 	number of command line arguments
 * @param[in] 	argv 	array of command line arguments
 *
 * @return 		exit status
 *
 */
int main(int argc, char **argv)
{
	bool libsvm_format = false;
	char input_filename[GENSVM_MAX_LINE_LENGTH];

	struct GenGrid *grid = gensvm_init_grid();
	struct GenData *train_data = gensvm_init_data();
	struct GenData *test_data = gensvm_init_data();
	struct GenQueue *q = gensvm_init_queue();

	if (argc < MINARGS || gensvm_check_argv(argc, argv, "-help")
			|| gensvm_check_argv_eq(argc, argv, "-h") )
		exit_with_help(argv);
	parse_command_line(argc, argv, input_filename);
	libsvm_format = gensvm_check_argv(argc, argv, "-x");

	note("Reading grid file\n");
	read_grid_from_file(input_filename, grid);

	note("Reading data from %s\n", grid->train_data_file);
	if (libsvm_format)
		gensvm_read_data_libsvm(train_data, grid->train_data_file);
	else
		gensvm_read_data(train_data, grid->train_data_file);

	// check labels of training data
	gensvm_check_outcome_contiguous(train_data);
	if (!gensvm_check_outcome_contiguous(train_data)) {
		err("[GenSVM Error]: Class labels should start from 1 and "
				"have no gaps. Please reformat your data.\n");
		exit(EXIT_FAILURE);
	}

	// check if we are sparse and want nonlinearity
	if (train_data->Z == NULL && grid->kerneltype != K_LINEAR) {
		err("[GenSVM Warning]: Sparse matrices with nonlinear kernels "
				"are not yet supported. Dense matrices will "
				"be used.\n");
		train_data->RAW = gensvm_sparse_to_dense(train_data->spZ);
		train_data->Z = train_data->RAW;
		gensvm_free_sparse(train_data->spZ);
	}

	if (grid->traintype == TT) {
		err("[GenSVM Warning]: Using test datasets in a grid search "
				"is not yet supported in GenSVM.\n"
				"                  The test dataset will be "
				"ignored during training.\n");
		//note("Reading data from %s\n", grid->test_data_file);
		//gensvm_read_data(test_data, grid->test_data_file);
	}

	note("Creating queue\n");
	gensvm_fill_queue(grid, q, train_data, test_data);

	srand(time(NULL));

	note("Starting training\n");
	gensvm_train_queue(q);
	note("Training finished\n");

	if (grid->repeats > 0) {
		gensvm_consistency_repeats(q, grid->repeats, grid->percentile);
	}

	gensvm_free_queue(q);
	gensvm_free_grid(grid);
	gensvm_free_data(train_data);
	gensvm_free_data(test_data);

	note("Done.\n");
	return 0;
}

/**
 * @brief Parse command line arguments
 *
 * @details
 * Few arguments can be supplied to the command line. Only quiet mode can be
 * specified, or help can be requested. The filename of the grid file is
 * read from the arguments. Parsing of the grid file is done separately in
 * read_grid_from_file().
 *
 * @param[in] 	argc 		number of command line arguments
 * @param[in] 	argv 		array of command line arguments
 * @param[in] 	input_filename 	pre-allocated buffer for the grid
 * 				filename.
 *
 */
void parse_command_line(int argc, char **argv, char *input_filename)
{
	int i;

	GENSVM_OUTPUT_FILE = stdout;
	GENSVM_ERROR_FILE = stderr;

	for (i=1; i<argc; i++) {
		if (argv[i][0] != '-') break;
		if (++i>=argc)
			exit_with_help(argv);
		switch (argv[i-1][1]) {
			case 'q':
				GENSVM_OUTPUT_FILE = NULL;
				GENSVM_ERROR_FILE = NULL;
				i--;
				break;
			case 'x':
				i--;
				break;
			default:
				fprintf(stderr, "Unknown option: -%c\n",
						argv[i-1][1]);
				exit_with_help(argv);
		}
	}

	if (i >= argc)
		exit_with_help(argv);

	strcpy(input_filename, argv[i]);
}

/**
 * @brief Parse the kernel string from the training file
 *
 * @details
 * This is a utility function for the read_grid_from_file() function, to keep
 * the main code a bit shorter. It reads the line from the given buffer and
 * returns the corresponding KernelType.
 *
 * @param[in] 	kernel_line 	line from the file with the kernel
 * 				specification
 * @return 	the corresponding kerneltype
 */
KernelType parse_kernel_str(char *kernel_line)
{
	if (str_endswith(kernel_line, "LINEAR\n")) {
		return K_LINEAR;
	} else if (str_endswith(kernel_line, "POLY\n")) {
		return K_POLY;
	} else if (str_endswith(kernel_line, "RBF\n")) {
		return K_RBF;
	} else if (str_endswith(kernel_line, "SIGMOID\n")) {
		return K_SIGMOID;
	} else {
		fprintf(stderr, "Unknown kernel specified on line: %s\n",
				kernel_line);
		exit(EXIT_FAILURE);
	}
}

/**
 * @brief Read the GenGrid struct from file
 *
 * @details
 * Read the GenGrid struct from a file. The grid file follows a specific
 * format specified in @ref spec_grid_file.
 *
 * Commonly used string functions in this function are all_doubles_str() and
 * all_longs_str().
 *
 * @param[in] 	input_filename 	filename of the grid file
 * @param[in] 	grid 	GenGrid structure to place the parsed
 * 				parameter grid.
 *
 */
void read_grid_from_file(char *input_filename, struct GenGrid *grid)
{
	long i, nr = 0;
	FILE *fid;
	char buffer[GENSVM_MAX_LINE_LENGTH];
	char train_filename[GENSVM_MAX_LINE_LENGTH];
	char test_filename[GENSVM_MAX_LINE_LENGTH];
	double *params = Calloc(double, GENSVM_MAX_LINE_LENGTH);
	long *lparams = Calloc(long, GENSVM_MAX_LINE_LENGTH);

	fid = fopen(input_filename, "r");
	if (fid == NULL) {
		fprintf(stderr, "Error opening grid file %s\n",
				input_filename);
		exit(EXIT_FAILURE);
	}
	grid->traintype = CV;
	while ( fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid) != NULL ) {
		Memset(params, double,  GENSVM_MAX_LINE_LENGTH);
		Memset(lparams, long, GENSVM_MAX_LINE_LENGTH);
		if (str_startswith(buffer, "train:")) {
			sscanf(buffer, "train: %s\n", train_filename);
			grid->train_data_file = Calloc(char,
					GENSVM_MAX_LINE_LENGTH);
			strcpy(grid->train_data_file, train_filename);
		} else if (str_startswith(buffer, "test:")) {
			sscanf(buffer, "test: %s\n", test_filename);
			grid->test_data_file = Calloc(char,
					GENSVM_MAX_LINE_LENGTH);
			strcpy(grid->test_data_file, test_filename);
			grid->traintype = TT;
		} else if (str_startswith(buffer, "p:")) {
			nr = all_doubles_str(buffer, 2, params);
			grid->ps = Calloc(double, nr);
			for (i=0; i<nr; i++)
				grid->ps[i] = params[i];
			grid->Np = nr;
		} else if (str_startswith(buffer, "lambda:")) {
			nr = all_doubles_str(buffer, 7, params);
			grid->lambdas = Calloc(double, nr);
			for (i=0; i<nr; i++)
				grid->lambdas[i] = params[i];
			grid->Nl = nr;
		} else if (str_startswith(buffer, "kappa:")) {
			nr = all_doubles_str(buffer, 6, params);
			grid->kappas = Calloc(double, nr);
			for (i=0; i<nr; i++)
				grid->kappas[i] = params[i];
			grid->Nk = nr;
		} else if (str_startswith(buffer, "epsilon:")) {
			nr = all_doubles_str(buffer, 8, params);
			grid->epsilons = Calloc(double, nr);
			for (i=0; i<nr; i++)
				grid->epsilons[i] = params[i];
			grid->Ne = nr;
		} else if (str_startswith(buffer, "weight:")) {
			nr = all_longs_str(buffer, 7, lparams);
			grid->weight_idxs = Calloc(int, nr);
			for (i=0; i<nr; i++)
				grid->weight_idxs[i] = lparams[i];
			grid->Nw = nr;
		} else if (str_startswith(buffer, "folds:")) {
			nr = all_longs_str(buffer, 6, lparams);
			grid->folds = lparams[0];
			if (nr > 1)
				fprintf(stderr, "Field \"folds\" only takes "
						"one value. Additional "
						"fields are ignored.\n");
		} else if (str_startswith(buffer, "repeats:")) {
			nr = all_longs_str(buffer, 8, lparams);
			grid->repeats = lparams[0];
			if (nr > 1)
				fprintf(stderr, "Field \"repeats\" only "
						"takes one value. Additional "
						"fields are ignored.\n");
		} else if (str_startswith(buffer, "percentile:")) {
			nr = all_doubles_str(buffer, 11, params);
			grid->percentile = params[0];
			if (nr > 1)
				fprintf(stderr, "Field \"percentile\" only "
						"takes one value. Additional "
						"fields are ignored.\n");
		} else if (str_startswith(buffer, "kernel:")) {
			grid->kerneltype = parse_kernel_str(buffer);
		} else if (str_startswith(buffer, "gamma:")) {
			nr = all_doubles_str(buffer, 6, params);
			if (grid->kerneltype == K_LINEAR) {
				fprintf(stderr, "Field \"gamma\" ignored, "
						"linear kernel is used.\n");
				grid->Ng = 0;
				break;
			}
			grid->gammas = Calloc(double, nr);
			for (i=0; i<nr; i++)
				grid->gammas[i] = params[i];
			grid->Ng = nr;
		} else if (str_startswith(buffer, "coef:")) {
			nr = all_doubles_str(buffer, 5, params);
			if (grid->kerneltype == K_LINEAR ||
				grid->kerneltype == K_RBF) {
				fprintf(stderr, "Field \"coef\" ignored with "
						"specified kernel.\n");
				grid->Nc = 0;
				break;
			}
			grid->coefs = Calloc(double, nr);
			for (i=0; i<nr; i++)
				grid->coefs[i] = params[i];
			grid->Nc = nr;
		} else if (str_startswith(buffer, "degree:")) {
			nr = all_doubles_str(buffer, 7, params);
			if (grid->kerneltype != K_POLY) {
				fprintf(stderr, "Field \"degree\" ignored "
						"with specified kernel.\n");
				grid->Nd = 0;
				break;
			}
			grid->degrees = Calloc(double, nr);
			for (i=0; i<nr; i++)
				grid->degrees[i] = params[i];
			grid->Nd = nr;
		} else {
			fprintf(stderr, "Cannot find any parameters on line: "
					"%s\n", buffer);
		}
	}

	free(params);
	free(lparams);
	fclose(fid);
}