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
;
21 static struct option long_options
[] = {
22 { "test-data-file", required_argument
, NULL
, 't' },
23 { "input-data-file", required_argument
, NULL
, 'i' },
24 { "help", no_argument
, NULL
, 'h' },
25 { "silent", no_argument
, NULL
, 's' },
26 { NULL
, no_argument
, NULL
, 0 }
29 while( (c
= getopt_long(argc
, argv
, "sht:i:", long_options
, &optind
)) != -1 ){
31 case 't': test_data_filename
= optarg
; break;
32 case 'i': input_data_filename
= optarg
; break;
33 case 'h': print_help(); exit(1); break;
34 case 's': silent
=1; break;
40 net_input_name
= argv
[optind
++];
43 if( ! net_input_name
){ fprintf(stderr
, "No network file specified, see --help.\n"); exit(1); }
46 printf("Test input file: %s\n", test_data_filename
);
47 printf("Input file: %s\n", input_data_filename
);
48 printf("Net input file: %s\n", net_input_name
);
53 struct fann
*ann
= fann_create_from_file(net_input_name
);
54 if( ! ann
){ fprintf(stderr
, "Error reading network file '%s'.\n", net_input_name
); exit(1); }
56 /* Test the net if applicable */
57 if( test_data_filename
){
58 struct fann_train_data
*test_data
;
60 test_data
= fann_read_train_from_file(test_data_filename
);
62 fprintf(stderr
, "Error reading file '%s'.\n", test_data_filename
);
66 for(i
= 0; i
< fann_length_train_data(test_data
); i
++) {
67 fann_test(ann
, test_data
->input
[i
], test_data
->output
[i
]);
69 printf("MSE error on test data: %f\n\n", fann_get_MSE(ann
));
70 fann_destroy_train(test_data
);
72 if( ! input_data_filename
){
78 if( input_data_filename
){
79 // if I should not read from the stdin
80 if( strcmp(input_data_filename
,"-") ){
81 input_file
= fopen(input_data_filename
, "r");
82 if( ! input_file
){ fprintf(stderr
, "Error reading input file '%s'.\n", input_data_filename
); exit(1); }
86 unsigned int input_vector_len
= fann_get_num_input(ann
);
87 unsigned int output_vector_len
= fann_get_num_output(ann
);
89 fann_type
* input_vector
= (fann_type
*) malloc( input_vector_len
* sizeof(fann_type
));
90 fann_type
* output_vector
;
93 printf( "Reading input:\n");
95 /* Read the input vector */
97 for(i
=0; i
<input_vector_len
; i
++){
98 if(fscanf(input_file
, FANNSCANF
, &input_vector
[i
]) != 1){
102 /* Process the input vector */
104 // TODO possible leak?? Should I free it after use? Inspect fannsource.
105 output_vector
= fann_run( ann
, input_vector
);
107 /* Write the output */
108 for(i
=0; i
<output_vector_len
; i
++){
109 printf(FANNPRINTF
" ", output_vector
[i
]);
122 void print_help(void){
123 printf("Usage: gnet_run [OPTIONS] NETWORK_FILENAME\n\
125 Trains a neural network from the NETWORK_FILENAME saved by gnet_train.\n\
130 Prints this ultimate help\n\
133 Do not print unnecessary info\n\
135 --test-data_file=filename\n\
136 Runs the network against test data and outputs MSE.\n\
138 --input-data-file=filename\n\
139 Input file to read the data from. If filename == '-' then reads from the stdin.\n\
140 The file (or stdin) must have a precise FORMAT, and the vector lengths must agree with\n\
141 those in NETWORK_FILENAME.\n\
143 Input file FORMAT:\n\
144 first_number_from_the_first_vector second third ...\n\
145 first_number_from_the_second_vector second third ...\n\