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;
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 }
35 while( (c
= getopt_long(argc
, argv
, "hl:n:p:e:m:o:", long_options
, &optind
)) != -1 ){
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;
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); }
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
);
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
);
78 fprintf(stderr
, "Error reading file '%s'.\n", train_data_name
);
82 fprintf(stderr
, "Warning, network has only one layer.\n");
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
;
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
]);
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
);
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);
122 fann_save(ann
, net_output_name
);
123 printf("\nNetwork saved.\n");
127 fann_destroy_train(train_data
);
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\
142 another_input_vector\n\
143 another_output_vector\n\
149 --layers=int_number\n\
150 Number of network layers\n\
152 --neurons-hidden=int_number\n\
153 Number of neurons in hidden layers.\n\
155 --max-epochs=int_number\n\
156 Maximal number of epochs.\n\
158 --desired-error=float_number\n\
159 Desired error when to stop training.\n\
161 --learning-momentum=float_number\n\
164 --net-output-file=filename\n\
165 Where to save the net.\n\
168 gnet_train -l 3 -n 666 -p 1000 -e 0.00666 -o net.net dataset.data\n");