private double CalculateError(double[] desiredOutput)
{
double sumOfSquaredErrors = 0.0;
int layersCount = network.Layers.Length;
double[][] networkErrors = this.networkErrors.Value;
double[][] networkOutputs = this.networkOutputs.Value;
// Assume that all network neurons have the same activation function
var function = (this.network.Layers[0].Neurons[0] as ActivationNeuron)
.ActivationFunction;
// 1. Calculate error values for last layer first.
double[] layerOutputs = networkOutputs[layersCount - 1];
double[] errors = networkErrors[layersCount - 1];
for (int i = 0; i < errors.Length; i++)
{
double output = layerOutputs[i];
double e = output - desiredOutput[i];
errors[i] = e * function.Derivative2(output);
sumOfSquaredErrors += e * e;
}
// 2. Calculate errors for all other layers
for (int j = layersCount - 2; j >= 0; j--)
{
errors = networkErrors[j];
layerOutputs = networkOutputs[j];
ActivationLayer nextLayer = network.Layers[j + 1] as ActivationLayer;
double[] nextErrors = networkErrors[j + 1];
// For all neurons of this layer
for (int i = 0; i < errors.Length; i++)
{
double sum = 0.0;
// For all neurons of the next layer
for (int k = 0; k < nextErrors.Length; k++)
sum += nextErrors[k] * nextLayer.Neurons[k].Weights[i];
errors[i] = sum * function.Derivative2(layerOutputs[i]);
}
}
return sumOfSquaredErrors / 2.0;
}