public class Neuron
{
public int
numOfInputs { get; set; }
private Random rnd = new Random();
public double[] Weights { get; set; }
public Neuron(int
numOfInputs)
{
this.numOfInputs = numOfInputs;
this.Weights = new double[numOfInputs];
initRandomWeights();
}
public Neuron(int
numOfInputs, double[]
Weights)
{
this.numOfInputs = numOfInputs;
this.Weights = Weights;
}
/// <summary>
/// This function init random number of weights
/// </summary>
private void
initRandomWeights()
{
for(int i = 0; i
< Weights.Length; i++)
{
Weights[i] = rnd.NextDouble();
}
}
/// <summary>
/// This function return the sum of multipicatin of a specific Neuron
/// </summary>
/// <param name="inputs"></param>
/// <returns></returns>
public double
Activate(double [] inputs)
{
double sum = 0;
for (int i = 0; i
< Weights.Length; i++)
{
sum += Weights[i] * inputs[i];
}
sum = tanh(sum);
return sum;
}
public double tanh(double x)
{
return Math.Tanh(x);
}
}
public class Layer
{
public int
numOfNeurons { get; set; }
public int
numOfInputs { get; set; }
public Neuron [] Neurons { get; set; }
public Layer(int
numOfNeurons,int
numOfInputs)
{
this.numOfNeurons = numOfNeurons;
this.numOfInputs = numOfInputs;
Neurons = new Neuron[numOfNeurons];
for (int i = 0; i
< Neurons.Length; i++)
{
Neurons[i] = new Neuron(numOfInputs);
}
}
public double []
Activate(double [] inputs)
{
double[] output = new double[numOfNeurons];
for (int i = 0; i
< Neurons.Length; i++)
{
output[i] =
Neurons[i].Activate(inputs);
}
return output;
}
}
}
public class NeuralNetwork
{
public int[]
networkShape { get; set; }
public Layer [] Layers { get; set; }
public NeuralNetwork(int[]
networkShape)
{
this.networkShape = networkShape;
this.Layers = new
Layer[networkShape.Length];
for(int i = 1; i
< Layers.Length; i++)
{
Layers[i] = new Layer(networkShape[i],
networkShape[i – 1]);
}
}
public double []
Activate(double [] inputs)
{
for (int i = 1; i
< Layers.Length; i++)
{
inputs = Layers[i].Activate(inputs);
}
return inputs;
}
}