11 int main(int argc
, char ** argv
) {
12 /* Default network params */
13 char * net_input_name
=0;
14 char * test_data_filename
=0;
15 char * input_data_filename
=0;
16 FILE * input_file
= stdin
;
20 static struct option long_options
[] = {
21 { "test-data-file", required_argument
, NULL
, 't' },
22 { "input-data-file", required_argument
, NULL
, 'i' },
23 { "help", no_argument
, NULL
, 'h' },
24 { NULL
, no_argument
, NULL
, 0 }
27 while( (c
= getopt_long(argc
, argv
, "ht:i:", long_options
, &optind
)) != -1 ){
29 case 't': test_data_filename
= optarg
; break;
30 case 'i': input_data_filename
= optarg
; break;
31 case 'h': print_help(); exit(1); break;
37 net_input_name
= argv
[optind
++];
40 if( ! net_input_name
){ fprintf(stderr
, "No network file specified, see --help.\n"); exit(1); }
43 fprintf(stderr
,"Test input file: %s\n", test_data_filename
);
44 fprintf(stderr
,"Input file: %s\n", input_data_filename
);
45 fprintf(stderr
,"Net input file: %s\n", net_input_name
);
49 struct fann
*ann
= fann_create_from_file(net_input_name
);
50 if( ! ann
){ fprintf(stderr
, "Error reading network file '%s'.\n", net_input_name
); exit(1); }
52 /* Test the net if applicable */
53 if( test_data_filename
){
54 struct fann_train_data
*test_data
;
56 fprintf(stderr
,"Testing network.\n");
57 test_data
= fann_read_train_from_file(test_data_filename
);
59 fprintf(stderr
, "Error reading file '%s'.\n", test_data_filename
);
63 for(i
= 0; i
< fann_length_train_data(test_data
); i
++) {
64 fann_test(ann
, test_data
->input
[i
], test_data
->output
[i
]);
66 fprintf(stderr
,"MSE error on test data: %f\n\n", fann_get_MSE(ann
));
67 fann_destroy_train(test_data
);
69 if( ! input_data_filename
){
75 if( input_data_filename
){
76 // if I should not read from the stdin
77 if( strcmp(input_data_filename
,"-") ){
78 input_file
= fopen(input_data_filename
, "r");
79 if( ! input_file
){ fprintf(stderr
, "Error reading input file '%s'.\n", input_data_filename
); exit(1); }
83 unsigned int input_vector_len
= fann_get_num_input(ann
);
84 unsigned int output_vector_len
= fann_get_num_output(ann
);
86 fann_type
* input_vector
= (fann_type
*) malloc( input_vector_len
* sizeof(fann_type
));
87 fann_type
* output_vector
;
89 fprintf(stderr
, "Reading input:\n");
91 /* Read the input vector */
93 for(i
=0; i
<input_vector_len
; i
++){
94 if(fscanf(input_file
, FANNSCANF
, &input_vector
[i
]) != 1){
98 /* Process the input vector */
100 // TODO possible leak?? Should I free it after use? Inspect fannsource.
101 output_vector
= fann_run( ann
, input_vector
);
103 /* Write the output */
104 for(i
=0; i
<output_vector_len
; i
++){
105 printf(FANNPRINTF
" ", output_vector
[i
]);
118 void print_help(void){
119 printf("Usage: gnet_run [OPTIONS] NETWORK_FILENAME\n\
121 Trains a neural network from the NETWORK_FILENAME saved by gnet_train.\n\
125 --test-data_file=filename\n\
126 Runs the network against test data and outputs MSE.\n\
128 --input-data-file=filename\n\
129 Input file to read the data from. If filename == '-' then reads from the stdin.\n\
130 The file (or stdin) must have a precise FORMAT, and the vector lengths must agree with\n\
131 those in NETWORK_FILENAME.\n\
133 Input file FORMAT:\n\
134 first_number_from_the_first_vector second third ...\n\
135 first_number_from_the_second_vector second third ...\n\