【问题标题】:RELU BackpropagationRELU 反向传播
【发布时间】:2019-03-18 07:02:00
【问题描述】:

我在使用 relu 激活函数时无法实现反向传播。我的模型有两个隐藏层,两个隐藏层都有 10 个节点,输出层有一个节点(因此有 3 个权重,3 个偏差)。我的模型适用于这个损坏的backward_prop 函数。但是,该函数使用 sigmoid 激活函数(作为 cmets 包含在函数中)与反向传播一起使用。因此,我相信我搞砸了 relu 推导。

谁能把我推向正确的方向?

# The derivative of relu function is 1 if z > 0, and 0 if z <= 0
def relu_deriv(z):
    z[z > 0] = 1
    z[z <= 0] = 0
    return z

# Handles a single backward pass through the neural network
def backward_prop(X, y, c, p):
    """
    cache (c): includes activations (A) and linear transformations (Z)
    params (p): includes weights (W) and biases (b)
    """
    m = X.shape[1] # Number of training ex
    dZ3 = c['A3'] - y
    dW3 = 1/m * np.dot(dZ3,c['A2'].T)
    db3 = 1/m * np.sum(dZ3, keepdims=True, axis=1)
    dZ2 = np.dot(p['W3'].T, dZ3) * relu_deriv(c['A2']) # sigmoid: replace relu_deriv w/ (1-np.power(c['A2'], 2))
    dW2 = 1/m * np.dot(dZ2,c['A1'].T)
    db2 = 1/m * np.sum(dZ2, keepdims=True, axis=1)
    dZ1 = np.dot(p['W2'].T,dZ2) * relu_deriv(c['A1']) # sigmoid: replace relu_deriv w/ (1-np.power(c['A1'], 2))
    dW1 = 1/m * np.dot(dZ1,X.T)
    db1 = 1/m * np.sum(dZ1, keepdims=True, axis=1)

    grads = {"dW1":dW1,"db1":db1,"dW2":dW2,"db2":db2,"dW3":dW3,"db3":db3}
    return grads

【问题讨论】:

    标签: python neural-network backpropagation gradient-descent relu


    【解决方案1】:

    您的代码是抛出错误还是训练有问题?你能说清楚吗?

    或者如果你处理二进制分类,你可以尝试只让你的输出激活函数 sigmoid 和其他 ReLU 吗?

    请说明具体情况。

    回复编辑:

    你可以试试这个吗?

     def dReLU(x):
        return 1. * (x > 0)
    

    我指的是:https://gist.github.com/yusugomori/cf7bce19b8e16d57488a

    【讨论】:

    • 当我在 forward_prop 函数和 backward_prop 函数中使用 relu 时,我的模型根本没有改进。但是,如果我在 forward_prop 和 backward_prop 函数中使用 sigmoid 激活,那么我的模型训练得很好。因此,我相信我的 forward_prop 工作正常。我正在使用二进制分类。在我的模型中,2 个隐藏层使用 relu,输出节点使用 sigmoid。
    • 我为您编辑了答案,请尝试,如果这也不起作用,那意味着您的反向传播存在一些结构问题,该问题仅在 ReLU 中出现。然后我们可以检查一下。
    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2018-05-05
    • 2011-08-25
    • 2017-05-24
    相关资源
    最近更新 更多