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;
29 static struct option long_options
[] = {
30 { "layers", required_argument
, NULL
, 'l' },
31 { "neurons-hidden", required_argument
, NULL
, 'n' },
32 { "max-epochs", required_argument
, NULL
, 'p' },
33 { "desired-error", required_argument
, NULL
, 'e' },
34 { "learning-momentum", required_argument
, NULL
, 'm' },
35 { "net-output-file", required_argument
, NULL
, 'o' },
36 { "help", no_argument
, NULL
, 'h' },
37 { "activation-function", no_argument
, NULL
, 'a' },
38 { NULL
, no_argument
, NULL
, 0 }
41 while( (c
= getopt_long(argc
, argv
, "hl:n:p:e:m:o:a:", long_options
, &optind
)) != -1 ){
43 case 'l': num_layers
= atoi(optarg
); break;
44 case 'n': num_neurons_hidden
= atoi(optarg
); break;
45 case 'p': max_epochs
= atoi(optarg
); break;
46 case 'e': desired_error
= atof(optarg
); break;
47 case 'm': learning_momentum
= atof(optarg
); break;
48 case 'o': net_output_name
= optarg
; break;
49 case 'h': print_help(); exit(1); break;
50 case 'a': activation_function
= optarg
; break;
56 train_data_name
= argv
[optind
++];
59 if( ! train_data_name
){ fprintf(stderr
, "No training data file specified.\n"); exit(1); }
60 if( num_layers
<= 0 ){ fprintf(stderr
, "Number of layers must be positive.\n"); exit(1); }
61 if( num_neurons_hidden
<= 0 ){ fprintf(stderr
, "Number of neurons in the hidden layer must be positive.\n"); exit(1); }
62 if( max_epochs
<= 0 ){ fprintf(stderr
, "Max number of epochs must be positive.\n"); exit(1); }
63 if( desired_error
<= 0 ){ fprintf(stderr
, "Desired error must be positive.\n"); exit(1); }
64 if( learning_momentum
<= 0 ){ fprintf(stderr
, "Learning momentum be positive.\n"); exit(1); }
65 if( !strcmp(activation_function
, "linear") )
67 else if( !strcmp(activation_function
, "sigmoid") )
69 else{ fprintf(stderr
, "Activation function must be either 'linear' or 'sigmoid' (default).\n"); exit(1);}
73 printf("Layers: %u\n", num_layers
);
74 printf("Neurons hidden: %u\n", num_neurons_hidden
);
75 printf("Max epochs: %u\n", max_epochs
);
76 printf("Desired error: %f\n", desired_error
);
77 printf("Learning momentum: %f\n", learning_momentum
);
78 printf("Net output file: %s\n", net_output_name
);
79 printf("Train data file: %s\n", train_data_name
);
80 printf("Activation function: %s\n", activation_function
);
86 struct fann_train_data
*train_data
=0;
88 //printf("Loading training data file.\n");
89 train_data
= fann_read_train_from_file(train_data_name
);
92 printf( "Error reading file '%s'.\n", train_data_name
);
96 fprintf(stderr
, "Warning, network has only one layer.\n");
98 fprintf(stderr
, "Warning, network has more than 10 layers.\n");
100 unsigned int * layers
= ( unsigned int * ) malloc( num_layers
* sizeof(unsigned int) );
103 layers
[0] = train_data
->num_input
;
105 for( i
= 1 ; i
< num_layers
- 1 ; i
++)
106 layers
[i
] = num_neurons_hidden
;
107 layers
[num_layers
- 1] = train_data
->num_output
;
110 printf("Network architecture:\n ->");
111 for( i
= 0 ; i
< num_layers
; i
++)
112 printf("%d-", layers
[i
]);
116 //printf("Creating network.\n");
117 ann
= fann_create_standard_array(num_layers
, layers
);
119 //fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
123 fann_set_activation_function_output(ann
, FANN_LINEAR
);
124 fann_set_activation_function_hidden(ann
, FANN_LINEAR
);
127 fann_set_activation_function_output(ann
, FANN_SIGMOID_SYMMETRIC
);
128 fann_set_activation_function_hidden(ann
, FANN_SIGMOID_SYMMETRIC
);
131 fprintf(stderr
, "Activation function must be either 'linear' or 'sigmoid' (default).\n");
136 printf("Training network:\n");
137 //fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL);
138 //fann_set_training_algorithm(ann, FANN_TRAIN_QUICKPROP);
140 fann_set_learning_momentum(ann
, learning_momentum
);
141 fann_train_on_data(ann
, train_data
, max_epochs
, 50, desired_error
);
143 // fann_set_activation_function_hidden(ann, FANN_THRESHOLD_SYMMETRIC);
144 // fann_set_activation_function_output(ann, FANN_THRESHOLD_SYMMETRIC);
149 fann_save(ann
, net_output_name
);
150 printf("\nNetwork saved.\n");
154 fann_destroy_train(train_data
);
160 void print_help(void){
161 printf("Usage: gnet_train [OPTIONS] TRAIN_DATA_FILENAME\n\
163 Trains a neural network from the TRAIN_DATA_FILENAME.\n\
165 TRAIN_DATA_FILENAME format:\n\
166 number_of_pairs length_of_input_vector length_of_output_vector\n\
169 another_input_vector\n\
170 another_output_vector\n\
176 --layers=int_number\n\
177 Number of network layers\n\
179 --neurons-hidden=int_number\n\
180 Number of neurons in hidden layers.\n\
182 --max-epochs=int_number\n\
183 Maximal number of epochs.\n\
185 --desired-error=float_number\n\
186 Desired error when to stop training.\n\
188 --learning-momentum=float_number\n\
191 --net-output-file=filename\n\
192 Where to save the net.\n\
194 --activation-function=FUNCTION\n\
195 Sets activation function for layers.\n\
196 Possibilities are 'sigmoid' (default) or 'linear' (without apostrophes).\n\
199 gnet_train -l 3 -n 666 -p 1000 -e 0.00666 -o net.net dataset.data\n");