private void CalculateUpdates( double[] input )
{
// current neuron
Neuron neuron;
// current and previous layers
Layer layer, layerPrev;
// layer's weights updates
double[][] layerWeightsUpdates;
// layer's thresholds updates
double[] layerThresholdUpdates;
// layer's error
double[] errors;
// neuron's weights updates
double[] neuronWeightUpdates;
// error value
// double error;
// 1 - calculate updates for the first layer
layer = network.Layers[0];
errors = neuronErrors[0];
layerWeightsUpdates = weightsUpdates[0];
layerThresholdUpdates = thresholdsUpdates[0];
// cache for frequently used values
double cachedMomentum = learningRate * momentum;
double cached1mMomentum = learningRate * ( 1 - momentum );
double cachedError;
// for each neuron of the layer
for ( int i = 0; i < layer.Neurons.Length; i++ )
{
neuron = layer.Neurons[i];
cachedError = errors[i] * cached1mMomentum;
neuronWeightUpdates = layerWeightsUpdates[i];
// for each weight of the neuron
for ( int j = 0; j < neuronWeightUpdates.Length; j++ )
{
// calculate weight update
neuronWeightUpdates[j] = cachedMomentum * neuronWeightUpdates[j] + cachedError * input[j];
}
// calculate treshold update
layerThresholdUpdates[i] = cachedMomentum * layerThresholdUpdates[i] + cachedError;
}
// 2 - for all other layers
for ( int k = 1; k < network.Layers.Length; k++ )
{
layerPrev = network.Layers[k - 1];
layer = network.Layers[k];
errors = neuronErrors[k];
layerWeightsUpdates = weightsUpdates[k];
layerThresholdUpdates = thresholdsUpdates[k];
// for each neuron of the layer
for ( int i = 0; i < layer.Neurons.Length; i++ )
{
neuron = layer.Neurons[i];
cachedError = errors[i] * cached1mMomentum;
neuronWeightUpdates = layerWeightsUpdates[i];
// for each synapse of the neuron
for ( int j = 0; j < neuronWeightUpdates.Length; j++ )
{
// calculate weight update
neuronWeightUpdates[j] = cachedMomentum * neuronWeightUpdates[j] + cachedError * layerPrev.Neurons[j].Output;
}
// calculate treshold update
layerThresholdUpdates[i] = cachedMomentum * layerThresholdUpdates[i] + cachedError;
}
}
}