gostyle_old: res
[gostyle.git] / gnet / gnet_run.c
blob279b732eb53e77b6ebe5469c68d8aacec40acaec
1 #include <stdio.h>
2 #include <string.h>
3 #include <getopt.h>
5 #include "floatfann.h"
7 /* Prototypes */
8 void print_help(void);
10 /* There we go! */
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;
18 int silent = 0;
19 /* Parse input */
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 }
28 int c;
29 while( (c = getopt_long(argc, argv, "sht:i:", long_options, &optind)) != -1 ){
30 switch (c){
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;
35 case '?': break;
36 default: exit(1);
39 while (optind < argc)
40 net_input_name = argv[optind++];
43 if( ! net_input_name ){ fprintf(stderr, "No network file specified, see --help.\n"); exit(1); }
45 if(! silent){
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);
49 printf("\n");
52 /* Load Net */
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);
61 if( ! test_data){
62 fprintf(stderr, "Error reading file '%s'.\n", test_data_filename);
63 }else{
64 fann_reset_MSE(ann);
65 unsigned i;
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 ){
73 fann_destroy(ann);
74 return 0;
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;
92 if(! silent)
93 printf( "Reading input:\n");
94 while(1){
95 /* Read the input vector */
96 unsigned i;
97 for(i=0; i<input_vector_len; i++){
98 if(fscanf(input_file, FANNSCANF, &input_vector[i]) != 1){
99 goto end;
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]);
111 printf("\n");
112 fflush(stdout);
115 end:
116 fclose(input_file);
117 fann_destroy(ann);
119 return 0;
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\
127 OPTIONS\n\
128 -h\n\
129 --help\n\
130 Prints this ultimate help\n\
131 -s\n\
132 --silent\n\
133 Do not print unnecessary info\n\
134 -t filename\n\
135 --test-data_file=filename\n\
136 Runs the network against test data and outputs MSE.\n\
137 -i filename\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\
146 ...\n\
147 EOF\n");