Listing 1: Code for training a neural network
/*********************************
*
* Training
*
**********************************/
if( (strncmp(mode, "training", 3) == 0)){
in_file = open_file(in_file_name, "r+b");
target_file = open_file(target_file_name,"r+b");
win_file = open_file(win_file_name, "w+b");
whid_file = open_file(whid_file_name, "w+b");
wout_file = open_file(wout_file_name, "w+b");
read_from_file(tmp_x,
m*data_sets,
in_file);
read_from_file(tmp_target,
o*data_sets,
target_file);
initialize_2d_weights(win, m, p);
initialize_2d_weights(wout, p, o);
initialize_3d_weights(whid, (n-1), p, p);
changing_weights = 1;
while(changing_weights){
printf("\n\n\nbig pass %d training pass %ld ----------------------------",
big_pass++, training_pass);
changing_weights = 0;
this_data_set = 0;
training_all_data_sets = 1;
while(training_all_data_sets){
for(i=0; i<m; i++)
x[i] = tmp_x[i + m*this_data_set];
for(i=0; i<o; i++)
target[i] =
tmp_target[i + o*this_data_set];
still_training = 1;
while(still_training){
input_layer(h, x, win, m, p, n, type);
inner_layers(h, whid, p, n, type);
output_layer(h, wout, y, p, n, o,type);
output_layer_error(y, target, delta,o);
if( (error_is_acceptable(delta, o)) ||
(training_pass > TRAINING_LIMIT))
still_training = 0;
else{
changing_weights = 1;
training_pass++;
neuron_deltas(h, h_delta, delta,
whid, wout, n, o, p);
change_output_weights(wout, delta,
h, n, o, p);
change_hidden_weights(whid, h,
h_delta, n,p);
change_input_weights(x, win,h_delta,
m, n, p);
} /* ends else still_training */
} /* ends while still_training */
printf("\n\nFinished training data set %d --- training pass %ld",
this_data_set, training_pass);
this_data_set++;
if(this_data_set >= data_sets)
training_all_data_sets = 0;
} /* ends while training_all_data_sets */
} /* ends while changing_weights */
write_to_file(win, m*p, win_file);
write_to_file(whid, (n-1)*p*p, whid_file);
write_to_file(wout, p*o, wout_file);
fclose(in_file);
fclose(target_file);
fclose(win_file);
fclose(whid_file);
fclose(wout_file);
} /* ends if training */