tex: Move Expert Knowledge out of Data Extraction section
[gostyle.git] / gnet / gnet_train.c
bloba0f5879693dce84f944a71144515418786be66a1
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 unsigned int num_layers = 3;
14 unsigned int num_neurons_hidden = 30;
15 unsigned int max_epochs = 5000;
16 float desired_error = 0.0001;
17 float learning_momentum = 0.2;
19 char * net_output_name = "go_net.net";
20 char * train_data_name=0;
22 /* Parse input */
24 static struct option long_options[] = {
25 { "layers", required_argument, NULL, 'l' },
26 { "neurons-hidden", required_argument, NULL, 'n' },
27 { "max-epochs", required_argument, NULL, 'p' },
28 { "desired-error", required_argument, NULL, 'e' },
29 { "learning-momentum", required_argument, NULL, 'm' },
30 { "net-output-file", required_argument, NULL, 'o' },
31 { "help", no_argument, NULL, 'h' },
32 { NULL, no_argument, NULL, 0 }
34 int c;
35 while( (c = getopt_long(argc, argv, "hl:n:p:e:m:o:", long_options, &optind)) != -1 ){
36 switch (c){
37 case 'l': num_layers = atoi(optarg); break;
38 case 'n': num_neurons_hidden = atoi(optarg); break;
39 case 'p': max_epochs = atoi(optarg); break;
40 case 'e': desired_error = atof(optarg); break;
41 case 'm': learning_momentum = atof(optarg); break;
42 case 'o': net_output_name = optarg; break;
43 case 'h': print_help(); exit(1); break;
44 case '?': break;
45 default: exit(1);
48 while (optind < argc)
49 train_data_name= argv[optind++];
52 if( ! train_data_name ){ fprintf(stderr, "No training data file specified.\n"); exit(1); }
53 if( num_layers <= 0 ){ fprintf(stderr, "Number of layers must be positive.\n"); exit(1); }
54 if( num_neurons_hidden <= 0 ){ fprintf(stderr, "Number of neurons in the hidden layer must be positive.\n"); exit(1); }
55 if( max_epochs <= 0 ){ fprintf(stderr, "Max number of epochs must be positive.\n"); exit(1); }
56 if( desired_error <= 0 ){ fprintf(stderr, "Desired error must be positive.\n"); exit(1); }
57 if( learning_momentum <= 0 ){ fprintf(stderr, "Learning momentum be positive.\n"); exit(1); }
58 //#ifdef DEBUG
59 #if 1
60 printf("Layers: %u\n", num_layers);
61 printf("Neurons hidden: %u\n", num_neurons_hidden);
62 printf("Max epochs: %u\n", max_epochs);
63 printf("Desired error: %f\n", desired_error);
64 printf("Learning momentum: %f\n", learning_momentum);
65 printf("Net output file: %s\n", net_output_name);
66 printf("Train data file: %s\n", train_data_name);
67 printf("\n");
68 #endif
70 /* Create the net */
71 struct fann *ann=0;
72 struct fann_train_data *train_data=0;
74 //printf("Loading training data file.\n");
75 train_data = fann_read_train_from_file(train_data_name);
76 /* Die if error */
77 if( ! train_data){
78 fprintf(stderr, "Error reading file '%s'.\n", train_data_name);
79 exit(1);
81 if( num_layers == 1)
82 fprintf(stderr, "Warning, network has only one layer.\n");
83 if( num_layers >= 10)
84 fprintf(stderr, "Warning, network has more than 10 layers.\n");
86 unsigned int * layers = ( unsigned int * ) malloc( num_layers * sizeof(unsigned int) );
89 layers[0] = train_data->num_input;
90 unsigned i;
91 for( i = 1 ; i < num_layers - 1 ; i++)
92 layers[i] = num_neurons_hidden;
93 layers[num_layers - 1] = train_data->num_output;
96 printf( "Network architecture:\n ->");
97 for( i = 0 ; i < num_layers ; i++)
98 printf( "%d-", layers[i]);
99 printf( ">\n\n");
102 //printf("Creating network.\n");
103 ann = fann_create_standard_array(num_layers, layers);
105 fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
106 fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
108 /* Train the net */
109 printf("Training network:\n");
110 //fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL);
111 //fann_set_training_algorithm(ann, FANN_TRAIN_QUICKPROP);
113 fann_set_learning_momentum(ann, learning_momentum);
114 fann_train_on_data(ann, train_data, max_epochs, 50, desired_error);
116 // fann_set_activation_function_hidden(ann, FANN_THRESHOLD_SYMMETRIC);
117 // fann_set_activation_function_output(ann, FANN_THRESHOLD_SYMMETRIC);
121 /* Save the net */
122 fann_save(ann, net_output_name);
123 printf("\nNetwork saved.\n");
126 /* Clean the net */
127 fann_destroy_train(train_data);
128 fann_destroy(ann);
130 return 0;
133 void print_help(void){
134 printf("Usage: gnet_train [OPTIONS] TRAIN_DATA_FILENAME\n\
136 Trains a neural network from the TRAIN_DATA_FILENAME.\n\
138 TRAIN_DATA_FILENAME format:\n\
139 number_of_pairs length_of_input_vector length_of_output_vector\n\
140 input_vector\n\
141 output_vector\n\
142 another_input_vector\n\
143 another_output_vector\n\
144 ...\n\
145 EOF\n\
147 OPTIONS\n\
148 -l int_number\n\
149 --layers=int_number\n\
150 Number of network layers\n\
151 -n int_number\n\
152 --neurons-hidden=int_number\n\
153 Number of neurons in hidden layers.\n\
154 -p int_number\n\
155 --max-epochs=int_number\n\
156 Maximal number of epochs.\n\
157 -e float_number\n\
158 --desired-error=float_number\n\
159 Desired error when to stop training.\n\
160 -m float_number\n\
161 --learning-momentum=float_number\n\
162 Learning momentum\n\
163 -o filename\n\
164 --net-output-file=filename\n\
165 Where to save the net.\n\
167 EXAMPLE\n\
168 gnet_train -l 3 -n 666 -p 1000 -e 0.00666 -o net.net dataset.data\n");