private static Dictionary <Node, double> UpdateOutputLayer(Layer outputLayer, double[] targetOutputs, ErrorFunctionType errorFunctionType, double learningRate, double momentumMagnitude)
    {
        var errorFunctionDifferential = ErrorFunctionResolver.ResolveErrorFunctionDifferential(errorFunctionType);

        var deltas = new Dictionary <Node, double>();

        for (var i = 0; i < outputLayer.Nodes.Count; i++)
        {
            var node  = outputLayer.Nodes[i];
            var delta = Math.MinMagnitude(errorFunctionDifferential.Invoke(targetOutputs[i], node.Output), 100)
                        * outputLayer.ActivationFunctionDifferential(node.Output)
                        * learningRate;
            deltas.Add(node, delta);
            foreach (var(prevNode, weightForPrevNode) in node.Weights)
            {
                UpdateNodeWeight(prevNode, weightForPrevNode, delta, momentumMagnitude);
            }

            foreach (var(_, weightForPrevLayer) in node.BiasWeights)
            {
                UpdateBiasNodeWeight(weightForPrevLayer, delta, momentumMagnitude);
            }
        }

        return(deltas);
    }
    internal static double CalculateDelta(Layer layer, Dictionary <Node, double> backwardsPassDeltas, Node node)
    {
        var sumDeltaWeights = 0d;

        foreach (var(passedNode, passedDelta) in backwardsPassDeltas)
        {
            if (passedNode.Weights.TryGetValue(node, out var weightBetweenNodeAndPassedNode))
            {
                sumDeltaWeights += passedDelta * weightBetweenNodeAndPassedNode.Value;
            }
        }

        return(sumDeltaWeights * layer.ActivationFunctionDifferential(node.Output));
    }
Esempio n. 3
0
    private static Dictionary <Node, double> NegativeSampleOutput(Layer outputLayer, double targetOutput, int outputIndex, ErrorFunctionType errorFunctionType, double learningRate, double momentumMagnitude)
    {
        var errorFunctionDifferential = ErrorFunctionResolver.ResolveErrorFunctionDifferential(errorFunctionType);
        var outputNode = outputLayer.Nodes[outputIndex];

        var delta = errorFunctionDifferential.Invoke(targetOutput, outputNode.Output)
                    * outputLayer.ActivationFunctionDifferential(outputNode.Output)
                    * learningRate;

        foreach (var(prevNode, weightForPrevNode) in outputNode.Weights)
        {
            Backpropagation.UpdateNodeWeight(prevNode, weightForPrevNode, delta, momentumMagnitude);
        }
        foreach (var(_, weightForPrevLayer) in outputNode.BiasWeights)
        {
            Backpropagation.UpdateBiasNodeWeight(weightForPrevLayer, delta, momentumMagnitude);
        }

        return(new Dictionary <Node, double> {
            { outputNode, delta }
        });
    }