This framework is designed for artificial neural network creation and use.
The used structure is multi-layer perceptrons (MLP) where each neuron computes either a sigmoid function or the hyperbolic tangent.
Each layer contains a bias neuron which always outputs the value "1"
The weight update is performed with the gradient descent backpropagation algorithm
First select the number of hidden layers, the number of neurons per layer and the function type (sigmoid or tanh).
Put your database into memory according to the main function example
Call the createNetworkStructure function, which will put the structure of the ANN into memory.
Train the network for a certain number of epochs using the trainNetwork function. You may also test the model at each epoch as depicted in the main function example.
For each prediction, call the forwardPass function
That's it!, you can now use a neural network !
A neural network with multi-layer perceptron is a graph structure where each node in an hidden layer is connected to every node in the following layer. But no connection exists between layers of a distance >= 2.
The network is trained using a backpropagation algorithm, here with the gradient descent. The concept is easy to understand : one forward pass is performed per object in the database. The output of the network is compared to what was expected, and the weights are updated to correct the output.
When should I use neural networks?
The use of ANN is recommended if :
If one of these conditions is not fulfulled, you should consider using another machine learning method, otherwise, go for ANNs !
#pragma argsused
int main(int argc, char* argv[])
{
int NbInput = 2; //Number of input variables
int NbHidden = 2; //Number of hidden layers
int NbNodes = 25; //Number of nodes in each hidden layer
int NbOutput = 1; //Number of output variables
int NbRuns = 500; //Number of epochs
double LearningRate = 0.1; //Learning rate (between 0 and 1)
int Type = 2; //0 = sigmoide, 1 = identity (DON'T USE THIS), 2 = tanh
int DatabaseSize = 7; //Number of objects in the database
NeuralNetwork nn;
double** TabOutput;
Elem* Database;
double* Outputs;
double* Inputs;
int i, j;
//Dynamic allocation
Database = (Elem*)malloc(DatabaseSize*sizeof(Elem));
for(i=0; i < DatabaseSize; i++)
{
Database[i].Att = (double*)malloc(NbInput*sizeof(double));
Database[i].GOAL = (double*)malloc(NbOutput*sizeof(double));
}
//Filling the database
Database[0].Att[0] = 0.1;
Database[0].Att[1] = 0.4;
Database[0].GOAL[0] = 0.25;
Database[1].Att[0] = 0.2;
Database[1].Att[1] = 1;
Database[1].GOAL[0] = 0.6;
Database[2].Att[0] = -0.3;
Database[2].Att[1] = 0.9;
Database[2].GOAL[0] = 0.3;
Database[3].Att[0] = 0.4;
Database[3].Att[1] = -0.4;
Database[3].GOAL[0] = 0.0;
Database[4].Att[0] = 0.9;
Database[4].Att[1] = 0.8;
Database[4].GOAL[0] = 0.85;
Database[5].Att[0] = 0.2;
Database[5].Att[1] = 0.7;
Database[5].GOAL[0] = 0.45;
Database[6].Att[0] = -0.1;
Database[6].Att[1] = 0.5;
Database[6].GOAL[0] = 0.2;
//Init the random seed
srand(time(NULL));
//Creating the network
nn = createNetworkStructure(NbInput, NbHidden, NbNodes, NbOutput, Type, LearningRate);
//Creating the array for output values
fprintf(stderr,"Creation of output array\n");
TabOutput = (double**)malloc((NbHidden)*sizeof(double*));
for(i = 0; i < NbHidden; i++)
{
TabOutput[i] = (double*)malloc((NbNodes+1)*sizeof(double));
}
//Network training
for(i = 0; i < NbRuns; i++)
{
trainNetwork(nn,TabOutput,1, Database, DatabaseSize);
//Test
Inputs = (double*)malloc((nn.NbInput+1)*sizeof(double));
Inputs[0] = -0.3;
Inputs[1] = 0.9;
Inputs[2] = 1;
Outputs = forwardPass(nn,TabOutput,Inputs);
fprintf(stderr,"Average of 0.9 and -0.3 = %f (Expected : 0.3) \n",Outputs[0]);
free(Outputs);
Inputs[0] = 0.8;
Inputs[1] = 0.9;
Inputs[2] = 1;
Outputs = forwardPass(nn,TabOutput,Inputs);
fprintf(stderr,"Average of 0.8 and 0.9 = %f (Expected : 0.85) \n",Outputs[0]);
free(Outputs);
free(Inputs);
}
//Destruction of output array
fprintf(stderr,"Destruction of output array\n");
for(i = 0; i < NbHidden; i++)
free(TabOutput[i]);
free(TabOutput);
//Destruction of the network
freeNetwork(nn);
//Destruction of the database
for(i = 0; i < DatabaseSize; i++)
{
free(Database[i].Att);
free(Database[i].GOAL);
}
free(Database);
printf("Press to quit...\n");
getchar();
return 0;
}
Questions & Comments