为什么这种反向传播实现无法正确训练权重?

时间:2015-05-27 19:19:21

标签: python neural-network backpropagation

我使用代码here作为示例,为神经网络编写了以下反向传播例程。我面临的问题让我感到困惑,并且已经将我的调试技巧发挥到了极限。

我面临的问题相当简单:随着神经网络的训练,它的权重被训练为零而没有准确的增益。

我试图多次修复它,验证:

  • 训练集是正确的
  • 目标向量是正确的
  • 前进步骤正确记录信息
  • 后退步增加正确记录
  • 增量上的标志是正确的
  • 权重确实在调整
  • 输入图层的增量全部为零
  • 没有其他错误或溢出警告

一些信息:

  • 训练输入是8x8网格的[0,16]值,表示强度;此网格表示数字(转换为列向量)
  • 目标向量是在与正确数字
  • 对应的位置中为1的输出
  • 原始权重和偏差由高斯分布分配
  • 激活是标准的sigmoid

我不知道从哪里开始。我已经确认我要检查的所有内容都运行正常,但仍然无效,所以我在这里问。以下是我用来反向传播的代码:

def backprop(train_set, wts, bias, eta):
    learning_coef = eta / len(train_set[0])

    for next_set in train_set:
        # These record the sum of the cost gradients in the batch
        sum_del_w = [np.zeros(w.shape) for w in wts]
        sum_del_b = [np.zeros(b.shape) for b in bias]

        for test, sol in next_set:
            del_w = [np.zeros(wt.shape) for wt in wts]
            del_b = [np.zeros(bt.shape) for bt in bias]
            # These two helper functions take training set data and make them useful
            next_input = conv_to_col(test)
            outp = create_tgt_vec(sol)

            # Feedforward step
            pre_sig = []; post_sig = []
            for w, b in zip(wts, bias):
                next_input = np.dot(w, next_input) + b
                pre_sig.append(next_input)
                post_sig.append(sigmoid(next_input))
                next_input = sigmoid(next_input)

            # Backpropagation gradient
            delta = cost_deriv(post_sig[-1], outp) * sigmoid_deriv(pre_sig[-1])
            del_b[-1] = delta
            del_w[-1] = np.dot(delta, post_sig[-2].transpose())

            for i in range(2, len(wts)):
                pre_sig_vec = pre_sig[-i]
                sig_deriv = sigmoid_deriv(pre_sig_vec)
                delta = np.dot(wts[-i+1].transpose(), delta) * sig_deriv
                del_b[-i] = delta
                del_w[-i] = np.dot(delta, post_sig[-i-1].transpose())

            sum_del_w = [dw + sdw for dw, sdw in zip(del_w, sum_del_w)]
            sum_del_b = [db + sdb for db, sdb in zip(del_b, sum_del_b)]

        # Modify weights based on current batch            
        wts = [wt - learning_coef * dw for wt, dw in zip(wts, sum_del_w)]
        bias = [bt - learning_coef * db for bt, db in zip(bias, sum_del_b)]

    return wts, bias

根据Shep的建议,我检查了在训练形状[2, 1, 1]的网络时总是输出1时发生了什么,实际上,网络在这种情况下正常训练。我在这一点上的最佳猜测是,渐变对于0调整过强而在1s上调弱,导致净减少,尽管每一步增加 - 但我不确定。

0 个答案:

没有答案