void ComputeDeltas(int layer, INeuralNetAccessor outputGradient) { float sigmoidDash; float error; int numberOfNodes = neuralNetAccessor.NodesInLayer(layer); int numberOfNextLayerNodes = neuralNetAccessor.NodesInLayer(layer + 1); NeuralLayer neuralLayer = neuralNetAccessor.GetNeuralLayer(layer); for (int node = 0; node < numberOfNodes; node++) { // Compute sigmoidDash //sigmoidDash = neuralLayer.GetNode(node).Compute(weightedInputs[layer][node] + smallDelta) - activations[layer][node]; sigmoidDash = activations[layer][node]; sigmoidDash = sigmoidDash * (1 - sigmoidDash); // Compute error error = 0; for (int nextNode = 0; nextNode < numberOfNextLayerNodes; nextNode++) { error += outputGradient.BiasAccessor[layer + 1, nextNode] * outputGradient.WeightAccessor[layer, nextNode, node]; } // Comput delta outputGradient.BiasAccessor[layer, node] = sigmoidDash * error; } }
void ComputeLastDeltas(float[] learningExampleOutput, INeuralNetAccessor outputGradient) { float sigmoidDash; float deltaCa; int lastLayer = neuralNetAccessor.NumberOfLayers - 1; int numberOfFinalNodes = neuralNetAccessor.NodesInLayer(lastLayer); NeuralLayer lastNeuralLayer = neuralNetAccessor.GetNeuralLayer(lastLayer); for (int i = 0; i < numberOfFinalNodes; i++) { //sigmoidDash = lastNeuralLayer.GetNode(i).Compute(weightedInputs[lastLayer][i] + smallDelta) - activations[lastLayer][i]; sigmoidDash = activations[lastLayer][i]; sigmoidDash = sigmoidDash * (1 - sigmoidDash); deltaCa = activations[lastLayer][i] - learningExampleOutput[i]; outputGradient.BiasAccessor[lastLayer, i] = sigmoidDash * deltaCa; } }
public NeuralNetAccessor(params int[] layersNodes) { if (layersNodes.Length < 2) { throw new System.ArgumentException(); } int inputSize = layersNodes[0]; neuralLayers = new NeuralLayer[layersNodes.Length - 1]; int currentNumberOfWeights = inputSize; for (int i = 0; i < neuralLayers.Length; i++) { neuralLayers[i] = new NeuralLayer(currentNumberOfWeights, layersNodes[i + 1]); currentNumberOfWeights = layersNodes[i + 1]; } initializedParameters = new int[layersNodes.Length]; for (int i = 0; i < layersNodes.Length; i++) { initializedParameters[i] = layersNodes[i]; } }