使用 numpy 进行 Leaky-ReLU 反向传播

发布于 2025-01-18 03:51:23 字数 521 浏览 2 评论 0原文

我想用numpy(向前和向后传)实现泄漏的Relu激活功能,并想获得有关此实现是否正确的评论。

因此,如果x> 0 和alpha * x如果x< = 0

这意味着,如果x> 0 和alpha如果x< = 0

这是我的代码:

import numpy as np

alpha = 0.2
mask = None

def forward(x):
    global mask
    mask = x > 0
    ret = x
    ret[~mask] = ret[~mask] * alpha
    return ret

def backward(error):
    ret = np.ones(shape=error.shape)
    ret[~mask] = alpha
    ret = np.multiply(ret, error)
    return ret

错误是从上层向下传递的张量。

I wanted to implement the Leaky ReLU activation function with numpy (forward and backward pass) and wanted to get some comments about whether this implementation is correct.

So the Leaky ReLU(x) = x if x > 0 and alpha * x if x <= 0.

This means, the derivative is: 1 if x > 0 and alpha if x <= 0.

This is my code:

import numpy as np

alpha = 0.2
mask = None

def forward(x):
    global mask
    mask = x > 0
    ret = x
    ret[~mask] = ret[~mask] * alpha
    return ret

def backward(error):
    ret = np.ones(shape=error.shape)
    ret[~mask] = alpha
    ret = np.multiply(ret, error)
    return ret

And error is the tensor from the upper layer which is passed downward.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文