gpt4 book ai didi

python - python中如何实现Leaky Relu的导数?

转载 作者:太空狗 更新时间:2023-10-30 01:04:56 29 4
gpt4 key购买 nike

如何在不使用 Tensorflow 的情况下在 Python 中实现 Leaky ReLU 的导数?

还有比这更好的方法吗?我希望函数返回一个 numpy 数组

def dlrelu(x, alpha=.01):
# return alpha if x < 0 else 1

return np.array ([1 if i >= 0 else alpha for i in x])

在此先感谢您的帮助

最佳答案

您使用的方法有效,但严格来说,您计算的是关于损失或下层的导数,因此从下层传递值来计算导数 (dl/dx) 可能是明智的。

无论如何,您可以避免使用对大x 更有效的循环。这是一种方法:

def dlrelu(x, alpha=0.01):
dx = np.ones_like(x)
dx[x < 0] = alpha
return dx

如果你从下层传递错误,它看起来像这样:

def dlrelu(dl, x, alpha=0.01):
""" dl and x have same shape. """
dx = np.ones_like(x)
dx[x < 0] = alpha
return dx*dl

关于python - python中如何实现Leaky Relu的导数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48102882/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com