【问题标题】:Neural network not producing results神经网络不产生结果
【发布时间】:2018-05-06 08:41:16
【问题描述】:

这是我的项目。它包括: m = 24 其中 m 是训练示例的数量; 3个隐藏层和输入层;连接每一层的3组权重;数据为 1x38,响应为 y (1x1)。

import numpy as np
x = np.array([
[1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,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1)))
    a3 = 1/(1 + np.exp(-(np.dot(a2, w2) + 1)))
    a4 = 1/(1 + np.exp(-(np.dot(a3, w3) + 1)))
    a4delta = (y - a4) * (1 * (1 - a4))
    a3delta = a4delta.dot(w3.T) * (1 * (1 - a3))
    a2delta = a3delta.dot(w2.T) * (1 * (1 - a2))
    w3 += a3.T.dot(a4delta)
    w2 += a2.T.dot(a3delta)
    w += x.T.dot(a2delta)
print(a4)

结果如下:

[[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]]

谁能看看我是不是出错了?我的网络需要更改吗?我尝试通过添加更多隐藏层和更多内存来试验超参数

【问题讨论】:

  • 使用库而不是编码每一件事
  • 如果我想编写每件小事怎么办?如果我想使用一个库,我已经@WiLL_K
  • 好的,那么让我们从这个开始,你在哪里计算损失?。你的梯度下降函数在哪里?你的预测函数在哪里?
  • 看看增量。我做了错误(y-a4)乘以各个隐藏层的导数@WiLL_K
  • 你能扩展一下吗?我有点明白,非常感谢@silgon

标签: python numpy machine-learning neural-network gradient-descent


【解决方案1】:

你有一些错误和一些我认为是错误的事情,但也许只是不同的实现。

当您应该减去梯度乘以步长时,您正在将梯度添加到权重中。这就是为什么您的权重仅在一次迭代中就飙升至 1.0。

这些:

w3 += a3.T.dot(a4delta)

应该是这样的:

 w3 -= addBias(a3).T.dot(a4delta) * step

另外,我认为您对 sigmoid 函数的偏导数没有正确的公式。我认为这些:

a3delta = a4delta.dot(w3.T) * (1 * (1 - a3)) 

应该是:

a3delta = a4delta.dot(w3.T) * (a3 * (1 - a3))

您还应该使用以下内容将您的体重初始化为零左右:

ep = 0.12
w = np.random.random((39, 39)) * 2 * ep - ep

大多数实现都会为每一层添加一个偏置节点,但您并没有这样做。它使事情有点复杂,但我认为它会使它收敛得更快。

对我来说,这在 200 次迭代中会收敛到一个自信的答案:

# Weights have different shapes to account for bias node
w = np.random.random((39, 39)) * 2 * ep - ep
w2 = np.random.random((40, 39))* 2 * ep - ep
w3 = np.random.random((40, 1)) * 2 * ep - ep

ep = 0.12
w = np.random.random((39, 39)) * 2 * ep - ep
w2 = np.random.random((40, 39))* 2 * ep - ep
w3 = np.random.random((40, 1)) * 2 * ep - ep

def addBias(mat):
    return np.hstack((np.ones((mat.shape[0], 1)), mat))

step = -.1
for j in range(200):
    # Forward prop
    a2 = 1/(1 + np.exp(- addBias(x).dot(w)))
    a3 = 1/(1 + np.exp(- addBias(a2).dot(w2)))
    a4 = 1/(1 + np.exp(- addBias(a3).dot(w3)))

    # Back prop
    a4delta = (y - a4) 
    # need to remove bias nodes here
    a3delta = a4delta.dot(w3[1:,:].T) * (a3 * (1 - a3))
    a2delta = a3delta.dot(w2[1:,:].T) * (a2 * (1 - a2))

    # Gradient Descent
    # Multiply gradient by step then subtract
    w3 -= addBias(a3).T.dot(a4delta) * step
    w2 -= addBias(a2).T.dot(a3delta) * step 
    w -= addBias(x).T.dot(a2delta) * step
print(np.rint(a4))

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 2021-01-13
    • 2017-02-21
    • 1970-01-01
    • 2016-12-08
    • 2017-12-25
    • 2020-06-21
    • 2019-05-13
    相关资源
    最近更新 更多