尺寸的反向传播部分有问题

2024-10-06 12:27:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着从零开始建立一个只有一个隐藏层的神经网络。在反向传播部分提出了一些问题。你知道吗

在隐藏层我使用的是sigmoid激活,在输出层我使用的是softmax函数。你知道吗

为了计算损失函数相对于第1层中重量的梯度,方程式为:

在哪里

def back_prop(parameters,cache,X,Y):
    m = X.shape[1]
    #retriving parameters
    W1 = parameters["W1"]
    W2 = parameters["W2"]

    A1 = cache["A1"]
    A2 = cache["A2"]
    Z1 = cache["Z1"]
    Z2 = cache["Z2"]

    #back propagation calculation
    #calculating dL1,dL2 for updating W1 and W2
    # loss for W2 #
    xa = np.divide(Y,A2)+np.divide((1-Y),(1-A2)) #part of dA2 calculation
    dA2 = (1/m) * np.sum(xa)
    dZ2 = sigmoid_derivative(Z2)
    dW2 = A2
    dL2 = dA2*dZ2*dW2
    # loss for W1 #
    dA1 = W2
    print(dA1.shape)
    dZ1 = softmax_derivative(Z1)
    print(dZ1.shape)
    dW1 = X.T
    print(dW1.shape)

    dl0 = np.dot(dA2,dZ2.T)
    print('shape dl0',dl0.shape)

    dl1 = np.dot(dl0,dA1)
    print('shape dl1',dl1.shape)

    dl2 = np.dot(dl1,dZ1.T)
    print('shape dl2', dl2.shape)
    dL1 = np.dot(dl2,dW1) ###error : dimension for broadcasting is not satisfied
    print('shape dL1',dL1.shape)
    grads ={"dL1" : dL1,
            "dL2" : dL2}

return grads

我试着用np.dot公司()和尺寸问题。当我去更新W1时,dL/dW1维度不合适。你知道吗

这是我的完整代码,请原谅,因为它是在反向传播部分有点混乱。你知道吗

https://gist.github.com/ipritom/30fcad0c74ab59e5b31e1daac1c1d1e7


Tags: a2cachefornpdotw1parametersprint