使用 numpy 进行 Leaky-ReLU 反向传播
我想用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论