10 void print_help(void);
13 int main(int argc
, char ** argv
) {
14 /* Default network params */
15 unsigned int num_layers
= 3;
16 unsigned int num_neurons_hidden
= 30;
17 unsigned int max_epochs
= 5000;
18 float desired_error
= 0.0001;
19 float learning_momentum
= 0.2;
21 char * activation_function
= "sigmoid";
24 char * net_output_name
= "go_net.net";
25 char * train_data_name
=0;
30 static struct option long_options
[] = {
31 { "layers", required_argument
, NULL
, 'l' },
32 { "neurons-hidden", required_argument
, NULL
, 'n' },
33 { "max-epochs", required_argument
, NULL
, 'p' },
34 { "desired-error", required_argument
, NULL
, 'e' },
35 { "learning-momentum", required_argument
, NULL
, 'm' },
36 { "net-output-file", required_argument
, NULL
, 'o' },
37 { "help", no_argument
, NULL
, 'h' },
38 { "silent", no_argument
, NULL
, 's' },
39 { "activation-function", no_argument
, NULL
, 'a' },
40 { NULL
, no_argument
, NULL
, 0 }
43 while( (c
= getopt_long(argc
, argv
, "hsl:n:p:e:m:o:a:", long_options
, &optind
)) != -1 ){
45 case 'l': num_layers
= atoi(optarg
); break;
46 case 'n': num_neurons_hidden
= atoi(optarg
); break;
47 case 'p': max_epochs
= atoi(optarg
); break;
48 case 'e': desired_error
= atof(optarg
); break;
49 case 'm': learning_momentum
= atof(optarg
); break;
50 case 'o': net_output_name
= optarg
; break;
51 case 'h': print_help(); exit(1); break;
52 case 's': silent
= 1; break;
53 case 'a': activation_function
= optarg
; break;
59 train_data_name
= argv
[optind
++];
62 if( ! train_data_name
){ fprintf(stderr
, "No training data file specified.\n"); exit(1); }
63 if( num_layers
<= 0 ){ fprintf(stderr
, "Number of layers must be positive.\n"); exit(1); }
64 if( num_neurons_hidden
<= 0 ){ fprintf(stderr
, "Number of neurons in the hidden layer must be positive.\n"); exit(1); }
65 if( max_epochs
<= 0 ){ fprintf(stderr
, "Max number of epochs must be positive.\n"); exit(1); }
66 if( desired_error
<= 0 ){ fprintf(stderr
, "Desired error must be positive.\n"); exit(1); }
67 if( learning_momentum
<= 0 ){ fprintf(stderr
, "Learning momentum be positive.\n"); exit(1); }
68 if( !strcmp(activation_function
, "linear") )
70 else if( !strcmp(activation_function
, "sigmoid") )
72 else{ fprintf(stderr
, "Activation function must be either 'linear' or 'sigmoid' (default).\n"); exit(1);}
75 printf("Layers: %u\n", num_layers
);
76 printf("Neurons hidden: %u\n", num_neurons_hidden
);
77 printf("Max epochs: %u\n", max_epochs
);
78 printf("Desired error: %f\n", desired_error
);
79 printf("Learning momentum: %f\n", learning_momentum
);
80 printf("Net output file: %s\n", net_output_name
);
81 printf("Train data file: %s\n", train_data_name
);
82 printf("Activation function: %s\n", activation_function
);
88 struct fann_train_data
*train_data
=0;
90 //printf("Loading training data file.\n");
91 train_data
= fann_read_train_from_file(train_data_name
);
94 fprintf(stderr
, "Error reading file '%s'.\n", train_data_name
);
99 fprintf(stderr
, "Warning, network has only one layer.\n");
100 if( num_layers
>= 10)
101 fprintf(stderr
, "Warning, network has more than 10 layers.\n");
104 unsigned int * layers
= ( unsigned int * ) malloc( num_layers
* sizeof(unsigned int) );
107 layers
[0] = train_data
->num_input
;
109 for( i
= 1 ; i
< num_layers
- 1 ; i
++)
110 layers
[i
] = num_neurons_hidden
;
111 layers
[num_layers
- 1] = train_data
->num_output
;
115 printf("Network architecture:\n ->");
116 for( i
= 0 ; i
< num_layers
; i
++)
117 printf("%d-", layers
[i
]);
122 //printf("Creating network.\n");
123 ann
= fann_create_standard_array(num_layers
, layers
);
125 //fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
129 fann_set_activation_function_output(ann
, FANN_LINEAR
);
130 fann_set_activation_function_hidden(ann
, FANN_LINEAR
);
133 fann_set_activation_function_output(ann
, FANN_SIGMOID_SYMMETRIC
);
134 fann_set_activation_function_hidden(ann
, FANN_SIGMOID_SYMMETRIC
);
137 fprintf(stderr
, "Activation function must be either 'linear' or 'sigmoid' (default).\n");
143 printf("Training network:\n");
144 //fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL);
145 //fann_set_training_algorithm(ann, FANN_TRAIN_QUICKPROP);
147 fann_set_learning_momentum(ann
, learning_momentum
);
148 fann_train_on_data(ann
, train_data
, max_epochs
, 50, desired_error
);
150 // fann_set_activation_function_hidden(ann, FANN_THRESHOLD_SYMMETRIC);
151 // fann_set_activation_function_output(ann, FANN_THRESHOLD_SYMMETRIC);
156 fann_save(ann
, net_output_name
);
158 printf("\nNetwork saved.\n");
162 fann_destroy_train(train_data
);
168 void print_help(void){
169 printf("Usage: gnet_train [OPTIONS] TRAIN_DATA_FILENAME\n\
171 Trains a neural network from the TRAIN_DATA_FILENAME.\n\
173 TRAIN_DATA_FILENAME format:\n\
174 number_of_pairs length_of_input_vector length_of_output_vector\n\
177 another_input_vector\n\
178 another_output_vector\n\
185 Prints this ultimate help\n\
188 Do not print unnecessary info\n\
190 --layers=int_number\n\
191 Number of network layers\n\
193 --neurons-hidden=int_number\n\
194 Number of neurons in hidden layers.\n\
196 --max-epochs=int_number\n\
197 Maximal number of epochs.\n\
199 --desired-error=float_number\n\
200 Desired error when to stop training.\n\
202 --learning-momentum=float_number\n\
205 --net-output-file=filename\n\
206 Where to save the net.\n\
208 --activation-function=FUNCTION\n\
209 Sets activation function for layers.\n\
210 Possibilities are 'sigmoid' (default) or 'linear' (without apostrophes).\n\
213 gnet_train -l 3 -n 666 -p 1000 -e 0.00666 -o net.net dataset.data\n");