【问题标题】:Neural Network Cost Function Not Minimizing神经网络成本函数未最小化
【发布时间】:2020-02-29 03:27:41
【问题描述】:

所以我为 MNIST 做了一个简单的神经网络(784 个输入神经元、30 个隐藏神经元和 10 个输出神经元),但是成本函数 (MSE) 总是增加到 4.5 并且永远不会减少,并且输出神经元最终都只是输出1. 代码如下:

np.set_printoptions(suppress=True)

epochs = 50
batch = 60000
learning_rate = 3

B1 = np.random.randn(30, 1)
B2 = np.random.randn(10, 1)

W1 = np.random.randn(784, 30)
W2 = np.random.randn(30, 10)

for i in range(epochs):
    X, Y = shuffle(X, Y)

    c_B1 = np.zeros(B1.shape)
    c_B2 = np.zeros(B2.shape)
    c_W1 = np.zeros(W1.shape)
    c_W2 = np.zeros(W2.shape)

    for b in range(0, np.size(X, 0), batch):
        inputs = X[b:b+batch]
        outputs = Y[b:b+batch]

        Z1 = nn_forward(inputs, W1.T, B1)
        A1 = sigmoid(Z1)
        Z2 = nn_forward(A1, W2.T, B2)
        A2 = sigmoid(Z2)

        e_L = (outputs - A2) * d_sig(Z2)
        e_1 = np.multiply(np.dot(e_L, W2.T), d_sig(Z1))

        d_B2 = np.sum(e_L, axis=0)
        d_B1 = np.sum(e_1, axis=0)

        d_W2 = np.dot(A1.T, e_L)
        d_W1 = np.dot(inputs.T, e_1)

        d_B2 = d_B2.reshape((np.size(B2, 0), 1))
        d_B1 = d_B1.reshape((np.size(B1, 0), 1))

        c_B1 = np.add(c_B1, d_B1)
        c_B2 = np.add(c_B2, d_B2)
        c_W1 = np.add(c_W1, d_W1)
        c_W2 = np.add(c_W2, d_W2)

    B1 = np.subtract(B1, (learning_rate/batch) * c_B1)
    B2 = np.subtract(B2, (learning_rate/batch) * c_B2)
    W1 = np.subtract(W1, (learning_rate/batch) * c_W1)
    W2 = np.subtract(W2, (learning_rate/batch) * c_W2)    

    print(i, cost(outputs, A2))

我做错了什么?

【问题讨论】:

    标签: python numpy neural-network mnist


    【解决方案1】:

    我马上注意到两件事:

    • 为什么使用 MSE 作为分类问题的损失函数? MSE 通常用于回归问题。尝试使用crossentropy
    • 你有 sigmoid 作为输出激活,它将你的输入 x 映射到间隔 (0,1),所以如果你想进行分类,你应该查看输出向量的 argmax 和将其用作预测的类标签。

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 2014-02-03
      • 2012-07-07
      • 2022-01-01
      • 2014-02-21
      • 1970-01-01
      • 2015-08-04
      • 2016-12-20
      • 2018-11-09
      相关资源
      最近更新 更多