【问题标题】:Backpropagation algorithm giving bad results反向传播算法给出不好的结果
【发布时间】:2019-10-15 08:41:42
【问题描述】:

我正在尝试使用 MNIST 数据集通过前馈神经网络和反向传播来解决经典的手写数字识别问题。我使用Michael Nielsen's book 学习基本知识,使用3Blue1Brown's youtube video 学习反向传播算法。

我前段时间写完并一直在调试,因为结果很糟糕。在最好的情况下,网络可以在 1 个 epoch 后识别约 4000/10000 个样本,并且这个数字只会在接下来的 epoch 中下降,这让我相信反向传播算法存在一些问题。在过去的几天里,我一直沉浸在索引地狱中,试图调试它,但无法弄清楚问题出在哪里,我将不胜感激任何帮助指出它。

一点背景知识:1) 我没有使用任何矩阵乘法,也没有使用外部框架,而是使用 for 循环做所有事情,因为这就是我从视频中学到的。 2)与书不同,我将权重和偏差存储在同一个数组中。每层的偏差是该层权重矩阵末尾的一列。

最后是代码,这是NeuralNetwork类的Backpropagate方法,在UpdateMiniBatch中调用,它本身在SGD中调用:

/// <summary>
/// Returns the partial derivative of the cost function on one sample with respect to every weight in the network.
/// </summary>
public List<double[,]> Backpropagate(ITrainingSample sample)
{
    // Forwards pass
    var (weightedInputs, activations) = GetWeightedInputsAndActivations(sample.Input);

    // The derivative with respect to the activation of the last layer is simple to compute: activation - expectedActivation
    var errors = activations.Last().Select((a, i) => a - sample.Output[i]).ToArray();

    // Backwards pass
    List<double[,]> delCostOverDelWeights = Weights.Select(x => new double[x.GetLength(0), x.GetLength(1)]).ToList();
    List<double[]> delCostOverDelActivations = Weights.Select(x => new double[x.GetLength(0)]).ToList();
    delCostOverDelActivations[delCostOverDelActivations.Count - 1] = errors;

    // Comment notation:
    // Cost function: C
    // Weight connecting the i-th neuron on the (l + 1)-th layer to the j-th neuron on the l-th layer: w[l][i, j]
    // Bias of the i-th neuron on the (l + 1)-th layer: b[l][i]
    // Activation of the i-th neuon on the l-th layer: a[l][i]
    // Weighted input of the i-th neuron on the l-th layer: z[l][i] // which doesn't make sense on layer 0, but is left for index convenience
    // Notice that weights, biases, delCostOverDelWeights and delCostOverDelActivation all start at layer 1 (the 0-th layer is irrelevant to their meanings) while activations and weightedInputs strat at the 0-th layer

    for (int l = Weights.Count - 1; l >= 0; l--)
    {
        //Calculate ∂C/∂w for the current layer:
        for (int i = 0; i < Weights[l].GetLength(0); i++)
            for (int j = 0; j < Weights[l].GetLength(1); j++)
                delCostOverDelWeights[l][i, j] = // ∂C/∂w[l][i, j]
                    delCostOverDelActivations[l][i] * // ∂C/∂a[l + 1][i]
                    SigmoidPrime(weightedInputs[l + 1][i]) * // ∂a[l + 1][i]/∂z[l + 1][i] = ∂(σ(z[l + 1][i]))/∂z[l + 1][i] = σ′(z[l + 1][i])
                    (j < Weights[l].GetLength(1) - 1 ? activations[l][j] : 1); // ∂z[l + 1][i]/∂w[l][i, j] = a[l][j] ||OR|| ∂z[l + 1][i]/∂b[l][i] = 1

        // Calculate ∂C/∂a for the previous layer(a[l]):
        if (l != 0)
            for (int i = 0; i < Weights[l - 1].GetLength(0); i++)
                for (int j = 0; j < Weights[l].GetLength(0); j++)
                    delCostOverDelActivations[l - 1][i] += // ∂C/∂a[l][i] = sum over j:
                        delCostOverDelActivations[l][j] * // ∂C/∂a[l + 1][j]
                        SigmoidPrime(weightedInputs[l + 1][j]) * // ∂a[l + 1][j]/∂z[l + 1][j] = ∂(σ(z[l + 1][j]))/∂z[l + 1][j] = σ′(z[l + 1][j])
                        Weights[l][j, i]; // ∂z[l + 1][j]/∂a[l][i] = w[l][j, i]
    }

    return delCostOverDelWeights;
}

GetWeightedInputsAndActivations:

public (List<double[]>, List<double[]>) GetWeightedInputsAndActivations(double[] input)
{
    List<double[]> activations = new List<double[]>() { input }.Concat(Weights.Select(x => new double[x.GetLength(0)])).ToList();
    List<double[]> weightedInputs = activations.Select(x => new double[x.Length]).ToList();

    for (int l = 0; l < Weights.Count; l++)
        for (int i = 0; i < Weights[l].GetLength(0); i++)
        {
            double value = 0;
            for (int j = 0; j < Weights[l].GetLength(1) - 1; j++)
                value += Weights[l][i, j] * activations[l][j];// weights
            weightedInputs[l + 1][i] = value + Weights[l][i, Weights[l].GetLength(1) - 1];// bias
            activations[l + 1][i] = Sigmoid(weightedInputs[l + 1][i]);
        }

    return (weightedInputs, activations);
}

整个 NeuralNetwork 以及其他所有内容都可以在 here 找到。

编辑:在对 repo 进行许多重大更改之后,上述链接可能不再起作用,但考虑到答案,希望它应该是无关紧要的。为了完整起见,这是functional link to the changed repository

【问题讨论】:

  • 尝试仅使用 2 个神经元运行并手动验证中间结果。您可能还想检查SigmoidPrime,因为它似乎与您的实现不同
  • @mostanes 感谢您的评论,刚刚发现问题并经过 5 天的努力解决了。在我发现之前,我做了一个测试,我在 [2, 3, 1] 网络上手动计算了 9 个权重的梯度,并且效果很好。我的 SigmoidPrime 只是写同样东西的一种很好的方式,我从书里找到了它。您可以尝试在纸上手动验证。
  • 你最后一行的链接坏了。

标签: c# machine-learning neural-network backpropagation


【解决方案1】:

已修复。问题是:我没有将像素输入除以 255。其他一切似乎都正常工作,我现在在第一个 epoch 得到 +9000/10000。

【讨论】:

    猜你喜欢
    • 2014-12-19
    • 2012-03-14
    • 1970-01-01
    • 2014-01-10
    • 2013-11-28
    • 1970-01-01
    • 2017-05-12
    • 2016-10-16
    相关资源
    最近更新 更多