Here's the complete code:
//
FANN_TEST.C:
FANN Test
//
Download the Fann library to the following Web site:
Fast Artificial Neural Network Library (Fann)
http://leenissen.dk/fann/
Download links: Download FANN---> C Source Code and Windows DLL files
//
#include ". /fann-2.0.0/src/include/doublefann.h "
#ifdef _DEBUG
#pragma comment (lib, "...). /fann-2.0.0/microsoftwindowsdll/bin/fanndoublemtd.lib ")
#else
#pragma comment (lib, "...). /fann-2.0.0/microsoftwindowsdll/bin/fanndoublemt.lib ")
#endif
Training:
Additive neural network
c = a+b;
void Train ()
{
const unsigned int num_input = 2; Number of items entered
const unsigned int num_output = 1; Number of output items
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 3;
const FLOAT Desired_error = (const float) 0.00000001;
const unsigned int max_epochs = 500000; Maximum execution times
const unsigned int epochs_between_reports = 10000; Reporting frequency
struct Fann *ann;
int Num = 200;
float Mf = num*3.f;
int i;
Double A, B, C;
FILE *FP;
Fopen_s (&FP, "Add.fann", "w");
fprintf_s (FP, "%d 2 1\n", Num);
Generate Training Files
For (I=1 i<=num; i++) {
Generates 2 numbers, required between (0,1)
A = I/MF;
b = (i+1)/mf;
c = a+b; Requirements between (0,1)
The input is written to the training file
fprintf_s (FP, "%lf%lf\n%lf\n", A, B, c);
}
Fclose (FP);
Sample Training
Ann = Fann_create_standard (num_layers, Num_input, Num_neurons_hidden, num_output);
Fann_set_activation_function_hidden (Ann, Fann_linear);
Fann_set_activation_function_output (Ann, Fann_linear);
Fann_train_on_file (Ann, "Add.fann", Max_epochs, Epochs_between_reports, Desired_error);
Fann_save (Ann, "Add.fann.net");
Fann_destroy (ANN);
}
Perform:
Test
void exec (double A, double b)
{
struct Fann *ann;
Fann_type *calc_out;
Fann_type input[2];
Ann = Fann_create_from_file ("add.fann.net");
Input[0] = A;
INPUT[1] = b;
Calc_out = Fann_run (ann, input);
Fann_destroy (ANN);
printf ("A=%f\nb=%f\nc=%f\n expected c=%f\n\n", input[0], input[1], calc_out[0], input[0]+input[1]);
}
//
Main program
//
int main ()
{
The following method only needs to be called once, then comment out
Train ();
EXEC (0.354,0.58934);
EXEC (0.21469,0.3914968);
EXEC (0.130,0.44);
EXEC ( -0.3654,0.58455);
EXEC (0.365420,-0.95);
return 0;
}