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
|
/**
* @file trainMSVMMajdataset.c
* @author Gertjan van den Burg
* @date January, 2014
* @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_training_from_file() for documentation on the training file.
*
* The program runs a grid search as specified in the training 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.
*
*/
#include <time.h>
#include "crossval.h"
#include "msvmmaj.h"
#include "msvmmaj_pred.h"
#include "msvmmaj_train.h"
#include "msvmmaj_train_dataset.h"
#include "strutil.h"
#include "util.h"
#define MINARGS 2
extern FILE *MSVMMAJ_OUTPUT_FILE;
// function declarations
void print_null(const char *s) {}
void exit_with_help();
void parse_command_line(int argc, char **argv, char *input_filename);
void read_training_from_file(char *input_filename, struct Training *training);
/**
* @brief Help function
*/
void exit_with_help()
{
printf("This is MSVMMaj, version %1.1f\n\n", VERSION);
printf("Usage: trainMSVMMajdataset [options] training_file\n");
printf("Options:\n");
printf("-h | -help : print this help.\n");
printf("-q : quiet mode (no output)\n");
exit(0);
}
/**
* @brief Main interface function for trainMSVMMajdataset
*
* @details
* Main interface for the command line program. A given training 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
*
*/
int main(int argc, char **argv)
{
char input_filename[MAX_LINE_LENGTH];
struct Training *training = Malloc(struct Training, 1);
struct MajData *train_data = Malloc(struct MajData, 1);
struct MajData *test_data = Malloc(struct MajData, 1);
if (argc < MINARGS || msvmmaj_check_argv(argc, argv, "-help")
|| msvmmaj_check_argv_eq(argc, argv, "-h") )
exit_with_help();
parse_command_line(argc, argv, input_filename);
training->repeats = 0;
note("Reading training file\n");
read_training_from_file(input_filename, training);
note("Reading data from %s\n", training->train_data_file);
msvmmaj_read_data(train_data, training->train_data_file);
if (training->traintype == TT) {
note("Reading data from %s\n", training->test_data_file);
msvmmaj_read_data(test_data, training->test_data_file);
}
note("Creating queue\n");
struct Queue *q = Malloc(struct Queue, 1);
make_queue(training, q, train_data, test_data);
srand(time(NULL));
note("Starting training\n");
if (training->traintype == TT)
start_training_tt(q);
else
start_training_cv(q);
note("Training finished\n");
if (training->repeats > 0) {
consistency_repeats(q, training->repeats, training->traintype);
}
free_queue(q);
free(training);
msvmmaj_free_data(train_data);
msvmmaj_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 training file is
* read from the arguments. Parsing of the training file is done separately in
* read_training_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 training
* filename.
*
*/
void parse_command_line(int argc, char **argv, char *input_filename)
{
int i;
MSVMMAJ_OUTPUT_FILE = stdout;
for (i=1; i<argc; i++) {
if (argv[i][0] != '-') break;
if (++i>=argc)
exit_with_help();
switch (argv[i-1][1]) {
case 'q':
MSVMMAJ_OUTPUT_FILE = NULL;
i--;
break;
default:
fprintf(stderr, "Unknown option: -%c\n",
argv[i-1][1]);
exit_with_help();
}
}
if (i >= argc)
exit_with_help();
strcpy(input_filename, argv[i]);
}
/**
* @brief Read the Training struct from file
*
* @details
* Read the Training struct from a file. The training file follows a specific
* format specified in @ref spec_training_file.
*
* Commonly used string functions in this function are all_doubles_str() and
* all_longs_str().
*
* @param[in] input_filename filename of the training file
* @param[in] training Training structure to place the parsed
* parameter grid.
*
*/
void read_training_from_file(char *input_filename, struct Training *training)
{
long i, nr = 0;
FILE *fid;
char buffer[MAX_LINE_LENGTH];
char train_filename[MAX_LINE_LENGTH];
char test_filename[MAX_LINE_LENGTH];
double *params = Calloc(double, MAX_LINE_LENGTH);
long *lparams = Calloc(long, MAX_LINE_LENGTH);
fid = fopen(input_filename, "r");
if (fid == NULL) {
fprintf(stderr, "Error opening training file %s\n",
input_filename);
exit(1);
}
training->traintype = CV;
while ( fgets(buffer, MAX_LINE_LENGTH, fid) != NULL ) {
Memset(params, double, MAX_LINE_LENGTH);
Memset(lparams, long, MAX_LINE_LENGTH);
if (str_startswith(buffer, "train:")) {
sscanf(buffer, "train: %s\n", train_filename);
training->train_data_file = Calloc(char,
MAX_LINE_LENGTH);
strcpy(training->train_data_file, train_filename);
} else if (str_startswith(buffer, "test:")) {
sscanf(buffer, "test: %s\n", test_filename);
training->test_data_file = Calloc(char,
MAX_LINE_LENGTH);
strcpy(training->test_data_file, test_filename);
training->traintype = TT;
} else if (str_startswith(buffer, "p:")) {
nr = all_doubles_str(buffer, 2, params);
training->ps = Calloc(double, nr);
for (i=0; i<nr; i++)
training->ps[i] = params[i];
training->Np = nr;
} else if (str_startswith(buffer, "lambda:")) {
nr = all_doubles_str(buffer, 7, params);
training->lambdas = Calloc(double, nr);
for (i=0; i<nr; i++)
training->lambdas[i] = params[i];
training->Nl = nr;
} else if (str_startswith(buffer, "kappa:")) {
nr = all_doubles_str(buffer, 6, params);
training->kappas = Calloc(double, nr);
for (i=0; i<nr; i++)
training->kappas[i] = params[i];
training->Nk = nr;
} else if (str_startswith(buffer, "epsilon:")) {
nr = all_doubles_str(buffer, 8, params);
training->epsilons = Calloc(double, nr);
for (i=0; i<nr; i++)
training->epsilons[i] = params[i];
training->Ne = nr;
} else if (str_startswith(buffer, "weight:")) {
nr = all_longs_str(buffer, 7, lparams);
training->weight_idxs = Calloc(int, nr);
for (i=0; i<nr; i++)
training->weight_idxs[i] = lparams[i];
training->Nw = nr;
} else if (str_startswith(buffer, "folds:")) {
nr = all_longs_str(buffer, 6, lparams);
training->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);
training->repeats = lparams[0];
if (nr > 1)
fprintf(stderr, "Field \"repeats\" only "
"takes one value. Additional "
"fields are ignored.\n");
} else if (str_startswith(buffer, "kernel:")) {
nr = all_longs_str(buffer, 7, lparams);
if (nr > 1)
fprintf(stderr, "Field \"kernel\" only takes "
"one value. Additional "
"fields are ignored.\n");
switch (lparams[0]) {
case 0:
training->kerneltype = K_LINEAR;
break;
case 1:
training->kerneltype = K_POLY;
break;
case 2:
training->kerneltype = K_RBF;
break;
case 3:
training->kerneltype = K_SIGMOID;
break;
}
} else if (str_startswith(buffer, "gamma:")) {
nr = all_doubles_str(buffer, 6, params);
if (training->kerneltype == K_LINEAR) {
fprintf(stderr, "Field \"gamma\" ignored, "
"linear kernel is used.\n");
training->Ng = 0;
break;
}
training->gammas = Calloc(double, nr);
for (i=0; i<nr; i++)
training->gammas[i] = params[i];
training->Ng = nr;
} else if (str_startswith(buffer, "coef:")) {
nr = all_doubles_str(buffer, 5, params);
if (training->kerneltype == K_LINEAR ||
training->kerneltype == K_RBF) {
fprintf(stderr, "Field \"coef\" ignored with"
"specified kernel.\n");
training->Nc = 0;
break;
}
training->coefs = Calloc(double, nr);
for (i=0; i<nr; i++)
training->coefs[i] = params[i];
training->Nc = nr;
} else if (str_startswith(buffer, "degree:")) {
nr = all_doubles_str(buffer, 7, params);
if (training->kerneltype != K_POLY) {
fprintf(stderr, "Field \"degree\" ignored "
"with specified kernel.\n");
training->Nd = 0;
break;
}
training->degrees = Calloc(double, nr);
for (i=0; i<nr; i++)
training->degrees[i] = params[i];
training->Nd = nr;
} else {
fprintf(stderr, "Cannot find any parameters on line: "
"%s\n", buffer);
}
}
free(params);
free(lparams);
fclose(fid);
}
|