繁体   English   中英

Relu 激活和反向传播

[英]Relu Activation and Backpropagation

我已经使用 sigmoid 激活 function 实现了 MLP 的反向传播。

在前向阶段,我将来自每一层的 output 存储在 memory 中。

After calculating the output error and output gradient vector I start to go back in reverse and calculate the hidden error for each layer (using output from current layer + weight from layer +1 + output error from layer +1). 然后我使用隐藏错误和来自第 -1 层的 output 来计算梯度向量。 反向传播完成后,我使用为每一层计算的梯度向量更新权重。

我的问题与relu激活function的实现有关。 我有以下用于应用激活函数的函数。 第一个是我在初始运行中使用的,第二个是用于 relu 激活的。

def sigmoid(self, a):
    o = 1/(1+np.exp(-1*a))
    return o

def relu(self, a):
    return np.maximum(0, a)

def reluDerivative(self, x):       
    return 1. * (x > 0)

要实现 relu 激活 function,我是否需要在前向或反向传播阶段进行任何其他更改。 我读到我可能需要在后向阶段计算 relu 导数并应用,但对它的应用方式感到困惑。 感谢任何建议

假设您的 class 当前设置如下:

def logistic(z):
    return 1./(1. + np.exp(-z))


class backpropagation(object):

    ...

    def get_activation(self, a):
        return logistic(a)

    def get_delta_activation(self, a):
        y = logistic(a)
        dy = y * (1. - y)
        return dy

那么新派生的 class 将是

class BPwithRelu(backpropagation):
    
    def get_activation(self, a):
        return np.max(0, a)

    def get_delta_activation(self, a):
        return (x > 0).astype(np.float)


    

在进行反向传播时,您将需要使用链式规则的中间值。 假设您只有一个 relu 后跟一个 sigmoid,则有:

f(x) = relu(sigmoid(x))
relu(x) = max(0,x)
sigmoid(x) = 1/(1+exp(-1*a))

使用链式法则(拉格朗日符号)推导f(x) ):

f'(x) = relu'(sigmoid(x)) * sigmoid'(x)

您会看到 sigmoid 的梯度与 relu 的梯度相乘。 另请注意,relu 计算其相对于 sigmoid 的 output 的梯度,而 sigmoid 计算其相对于输入 (x) 的梯度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM