using System;
namespace Neural_Networks
{
///
/// Summary description for Nnetwork.
///
public class Nnetwork
{
private int i;
private int o;
private int h;
private double LH=0.15f; // learning parameter of the hidden
private double LO=0.2f; // learning parameter of the output
private double []inputsToNetwork;// array of double to take training data "inputs"
private double []desiredOutputs;// array of double to take training data "desired outputs"
private node []input; // array of input neurons
private node []output;// array of output neurons
private node []hidden;// array of hidden units neurons
private double total_error;
public double error_compared_to_tolerance=0;
///
/// this function to calculate the sigmoid of a double
///
/// double number
///
private double sigmoid(double x)
{
return 1/(1+Math.Exp(-x));
}
#region these functions must be called before callind training
///
/// Constructor of the neural network
///
/// integer : number of input neurons
/// integer : number of hidden units neurons
/// integer : number of output neurons
public Nnetwork(int inp , int hide , int outp )
{
i = inp ; o = outp ; h = hide;
int ahm = 0;
input = new node[i];
output = new node[o];
hidden = new node[h];
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
// initialize the network arrays and the weights
for(int x=0 ; x
/// This function to set the threshold of the neruons in the hidden layer and the output layer
///
public void FirstTimeSettings()
{
Random x = new Random(unchecked((int)DateTime.Now.Ticks));
for(int i=0 ; i
/// This function passes training data to the network , inputs and desired ouptus
///
///
public void BeforeTraining(params double []list)
{
int counter=0;
int j=0;
int k=0;
inputsToNetwork = new double[this.i];
desiredOutputs = new double[this.o];
while(counter < list.Length)
{
if( counter < this.i)
{
inputsToNetwork[j] = list [counter];
j++;
}
else
{
desiredOutputs[k] = list[counter];
k++;
}
counter++;
}
}
#endregion
///
/// a function to calculate the activation of the hidden layer and the output layer
///
private void Calc_Activation()
{
// a loop to set the activations of the hidden layer
int ch=0;
while(ch
/// print output of the network
///
public string print_output()
{
string outp="";
// calcaulate the output of the output layer
for(int x=0 ; x0.5)
outp += "1" + " ";
else
outp += "0" + " " ;
}
return outp;
}
///
/// a function to calculate the error of each output neuron
///
private void Calc_error_output()
{
for(int x=0 ; x
/// a function to calculate the error of each hidden neuron
///
private void Calc_error_hidden()
{
int y=0;
while(y
/// a function to calculate the new thresholds for each neuron
///
private void Calc_new_Thresholds()
{
// computing the thresholds for next itration for hidden layer
for(int x=0 ; x
/// a function to calculate the new weights between hidden and output
///
private void Calc_new_weights_in_hidden()
{
int x=0;
double temp=0.0f;
while(x < this.h)
{
temp = hidden[x].Activation * this.LO;
for(int y=0 ; y
/// a function to calculate the new weights between input and hidden
///
private void Calc_new_weights_in_input()
{
int x=0;
double temp=0.0f;
while(x < this.i)
{
temp = inputsToNetwork[x] * this.LH;
for(int y=0 ; y
/// the function that returns the total error
///
///
public double Calc_total_error_in_pattern()
{
double temp=0.0f;
for(int x=0 ; x
/// set total error = 0 after one cycle
///
public void reset_total_error()
{
this.total_error = 0;
}
///
/// the function that trains the network
///
///
public void Training_for_one_pattern()
{
this.Calc_Activation();
this.Calc_error_output();
this.Calc_error_hidden();
this.Calc_new_Thresholds();
this.Calc_new_weights_in_hidden();
this.Calc_new_weights_in_input();
}
///
/// test the network after training
///
public string Test_Drive()
{
this.Calc_Activation();
string temp = this.print_output();
return temp;
}
}
}