【发布时间】:2019-12-01 15:15:06
【问题描述】:
我正在使用反向传播算法创建一个基本的、也是我的第一个不带任何框架(如 Tensorflow、PyTorch...)的手写数字识别神经网络。
我的神经网络有 784 个输入和 10 个输出。所以对于最后一层,我必须使用 Softmax。
由于一些记忆错误,我现在有形状为 (300, 784) 的图像和形状为 (300, 10) 的标签 之后,我从 Categorical Cross-entropy 计算损失。 现在我们要解决我的问题了。在反向传播中,我需要手动计算激活函数的一阶导数。我是这样做的:
dAl = -(np.divide(Y, Al) - np.divide(1 - Y, 1 - Al))
#Y = test labels
#Al - Activation value from my last layer
然后我的反向传播就可以开始了,所以最后一层是softmax。
def SoftmaxDerivative(dA, Z):
#Z is an output from np.dot(A_prev, W) + b
#Where A_prev is an activation value from previous layer
#W is weight and b is bias
#dA is the derivative of an activation function value
x = activation_functions.softmax(dA)
s = x.reshape(-1,1)
dZ = np.diagflat(s) - np.dot(s, s.T)
return dZ
1.这个功能正常吗?
最后,我想计算权重和偏差的导数,所以我使用这个:
dW = (1/m)*np.dot(dZ, A_prev.T)
#m is A_prev.shape[1] -> 10
db = (1/m)*np.sum(dZ, axis = 1, keepdims = True)
但它在 dW 上失败,因为 dZ.shape 是 (3000, 3000) (与 A_prev.shape 相比,即 (300,10)) 所以据此我假设,只有 3 种可能的结果。
我的 Softmax 后向错误
dW 错误
我在其他地方完全有其他错误
任何帮助将不胜感激!
【问题讨论】:
-
嘿 - 在处理形状问题(或寻求一些帮助)时,添加所有涉及的张量的维度总是有用的 :)。几个小观察(1)您的一阶导数似乎不检查 0 分区,(2)您的 SoftmaxDerivative() 根本不使用 Z 输入。假设 dA.shape = (300, 10),正确的输出形状应该是 (300, 300) .. 所以也许你在某处乘以 dA.shape = (3000, 10) 某处..?
标签: python-3.x neural-network backpropagation softmax