简体   繁体   English

神经网络反向传播无法正常工作

[英]Neural Net backpropagation doesn't work properly

Lately I've implemented my own neural network (using different guides, but mainly from here ), for future use (I intend to use it for an OCR program i'l develop). 最近,我已经实现了自己的神经网络(使用不同的指南,但主要是从此处开始 ),以备将来使用(我打算将其用于我开发的OCR程序)。 currently I'm testing it, and I'm having this weird problem. 目前,我正在测试它,并且遇到了这个奇怪的问题。

Whenever I give my network a training example, the algorithm changes the weights in a way that leads to the desired output. 每当我给网络提供训练示例时,算法都会以产生所需输出的方式更改权重。 However, after a few training examples, the weights get messed up- making the network work well for some outputs, and making it wrong for other outputs (even if I enter the input of the training examples, exactly as it was). 但是,经过几个训练示例后,权重变得一团糟-使网络对于某些输出正常工作,而对其他输出却出错(即使我完全按照原样输入了训练示例的输入)。

I would appreciate if someone directed me towards the problem, should they see it. 如果有人指示我解决问题,我将不胜感激。 Here are the methods for calculating the error of the neurons and the weight adjusting- 以下是计算神经元误差和调整体重的方法-

    private static void UpdateOutputLayerDelta(NeuralNetwork Network, List<double> ExpectedOutputs)
    {
        for (int i = 0; i < Network.OutputLayer.Neurons.Count; i++)
        {
            double NeuronOutput = Network.OutputLayer.Neurons[i].Output;
            Network.OutputLayer.Neurons[i].ErrorFactor = ExpectedOutputs[i]-NeuronOutput; //calculating the error factor
            Network.OutputLayer.Neurons[i].Delta = NeuronOutput * (1 - NeuronOutput) * Network.OutputLayer.Neurons[i].ErrorFactor; //calculating the neuron's delta
        }
    }

    //step 3 method
    private static void UpdateNetworkDelta(NeuralNetwork Network)
    {
        NeuronLayer UpperLayer = Network.OutputLayer;
        for (int i = Network.HiddenLayers.Count - 1; i >= 0; i--)
        {
            foreach (Neuron LowerLayerNeuron in Network.HiddenLayers[i].Neurons)
            {
                for (int j = 0; j < UpperLayer.Neurons.Count; j++)
                {
                    Neuron UpperLayerNeuron = UpperLayer.Neurons[j];
                    LowerLayerNeuron.ErrorFactor += UpperLayerNeuron.Delta * UpperLayerNeuron.Weights[j + 1]/*+1 because of bias*/;
                }
                LowerLayerNeuron.Delta = LowerLayerNeuron.Output * (1 - LowerLayerNeuron.Output) * LowerLayerNeuron.ErrorFactor;
            }
            UpperLayer = Network.HiddenLayers[i];
        }
    }

    //step 4 method
    private static void AdjustWeights(NeuralNetwork Network, List<double> NetworkInputs)
    {
        //Adjusting the weights of the hidden layers
        List<double> LowerLayerOutputs = new List<double>(NetworkInputs);
        for (int i = 0; i < Network.HiddenLayers.Count; i++)
        {
            foreach (Neuron UpperLayerNeuron in Network.HiddenLayers[i].Neurons)
            {
                UpperLayerNeuron.Weights[0] += -LearningRate * UpperLayerNeuron.Delta;
                for (int j = 1; j < UpperLayerNeuron.Weights.Count; j++)
                    UpperLayerNeuron.Weights[j] += -LearningRate * UpperLayerNeuron.Delta * LowerLayerOutputs[j - 1] /*-1 because of bias*/;
            }
            LowerLayerOutputs = Network.HiddenLayers[i].GetLayerOutputs();
        }

        //Adjusting the weight of the output layer
        foreach (Neuron OutputNeuron in Network.OutputLayer.Neurons)
        {
            OutputNeuron.Weights[0] += -LearningRate * OutputNeuron.Delta * 1; //updating the bias - TODO: change this if the bias is also changed throughout the program
            for (int j = 1; j < OutputNeuron.Weights.Count; j++)
                OutputNeuron.Weights[j] += -LearningRate * OutputNeuron.Delta * LowerLayerOutputs[j - 1];
        }
    }

The learning rate is 0.5, and the neurons' activation function is a sigmoid function. 学习速率为0.5,神经元的激活功能为S型功能。

EDIT: I've noticed I never implemented the function to calculate the overall error: E=0.5 * Sum(ty) for each training example. 编辑:我注意到我从未实现过用于计算总体误差的函数:E = 0.5 *每个训练示例的Sum(ty)。 could that be the problem? 可能是问题所在吗? and if so, how should I fix it? 如果是这样,我应该如何解决?

The learning rate 0.5 seems a bit too large. 学习率0.5似乎太大了。 Usually values closer to 0.01 or 0.1 are used. 通常使用接近0.010.1值。 Also, it usually helps in convergence if training patterns are presented in random order. 同样,如果训练模式以随机顺序出现,通常也有助于收敛。 More useful hints can be found here: Neural Network FAQ (comp.ai.neural archive) . 在此处可以找到更多有用的提示: 神经网络常见问题解答(comp.ai.neural存档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM