如何在python中实现Leaky Relu的派生词?

Léc*_*bon 6 python neural-network activation-function

如何在不使用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])
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助

Ger*_*ges 5

您使用的方法是可行的,但严格来说,您是在计算损耗或较低层的导数,因此,最好也将较低层的值传递给计算导数(dl / dx)。

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

def dlrelu(x, alpha=0.01):
  dx = np.ones_like(x)
  dx[x < 0] = alpha
  return dx
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)