如何使用 Numpy 函数实现 leaky relu

How do I implement leaky relu using Numpy functions

我正在尝试实现 leaky Relu,问题是我必须为 4 维输入数组执行 4 个 for 循环。

有没有办法只使用 Numpy 函数来做 leaky relu?

离开 wikipedia 的 leaky relu 条目,应该可以用一个简单的屏蔽函数来做到这一点。

output = np.where(arr > 0, arr, arr * 0.01)

在任何大于 0 的地方,您保留该值,在其他任何地方,您将其替换为 arr * 0.01。

这里有两种实现方法leaky_relu

import numpy as np                                                 

x = np.random.normal(size=[1, 5])

# first approach                           
leaky_way1 = np.where(x > 0, x, x * 0.01)                          

# second approach                                                                   
y1 = ((x > 0) * x)                                                 
y2 = ((x <= 0) * x * 0.01)                                         
leaky_way2 = y1 + y2  
import numpy as np




def leaky_relu(arr):
    alpha = 0.1
    
    return np.maximum(alpha*arr, arr)
def leaky_relu_forward(x, alpha):
  out = x                                                
  out[out <= 0]=out[out <= 0]* alpha
  return out