Softmax的导数输出很大的形状

问题描述 投票:0回答:1

我正在使用反向传播算法创建一个基本的我的第一个手写数字识别神经网络,没有任何框架(例如Tensorflow,PyTorch ...)。

我的神经网络有784个输入和10个输出。因此,对于最后一层,我必须使用Softmax。

由于某些内存错误,我现在的图像形状为((300,784)),标签的形状为(300,10)之后,我要根据分类交叉熵计算损失。现在我们要解决我的问题。在反向传播中,我需要手动计算激活函数的一阶导数。我正在这样做:

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种可能的结果。

  1. 我的Softmax向后是错误的

  2. dW是错误的

  3. 我在其他地方完全有其他错误

任何帮助将不胜感激!

python-3.x neural-network backpropagation softmax
1个回答
1
投票

最近我遇到了同样的问题。我不确定,但是也许这个问题可以帮助您:Softmax derivative in NumPy approaches 0 (implementation)

© www.soinside.com 2019 - 2024. All rights reserved.