2.4. Training and Testing

Normally it will be sufficient to use the fann_train_on_file training function, but sometimes you want to have more control and you will have to write a custom training loop. This could be because you would like another stop criteria, or because you would like to adjust some of the parameters during training. Another stop criteria than the value of the combined mean square error could be that each of the training pairs should have a mean square error lower than a given value.

Example 2-1. The internals of the fann_train_on_file function, without writing the status line.


struct fann_train_data *data = fann_read_train_from_file(filename);
for(i = 1 ; i <= max_epochs ; i++) {
  fann_reset_MSE(ann);
  for (j = 0 ; j != data->num_data ; j++) {
    fann_train(ann, data->input[j], data->output[j]);
  }
  if ( fann_get_MSE(ann) < desired_error ) {
    break;
  }
}
fann_destroy_train(data);

        

This piece of code introduces the fann_train function, which trains the ANN for one iteration with one pair of inputs and outputs and also updates the mean square error. The fann_train_data structure is also introduced, this structure is a container for the training data in the file described in figure 10. The structure can be used to train the ANN, but it can also be used to test the ANN with data which it has not been trained with.

Example 2-2. Test all of the data in a file and calculates the mean square error.


struct fann_train_data *data = fann_read_train_from_file(filename);
fann_reset_MSE(ann);
for(i = 0 ; i != data->num_data ; i++ ) {
  fann_test(ann, data->input[i], data->output[i]);
}
printf("Mean Square Error: %f\n", fann_get_MSE(ann));
fann_destroy_train(data);

	

This piece of code introduces another useful function: fann_test function, which takes an input array and a desired output array as the parameters and returns the calculated output. It also updates the mean square error.


SourceForge.net Logo