【问题标题】:How do I implement leaky relu using Numpy functions如何使用 Numpy 函数实现泄漏 relu
【发布时间】:2018-11-04 03:58:07
【问题描述】:

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

有没有一种方法可以让我只使用 Numpy 函数来进行泄漏 relu?

【问题讨论】:

    标签: numpy activation-function relu


    【解决方案1】:

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

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

    任何高于 0 的地方,你都保留这个值,在其他地方,你用 arr * 0.01 替换它。

    【讨论】:

      【解决方案2】:

      这里有两种实现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  
      

      【讨论】:

      【解决方案3】:
      import numpy as np
      
      
      
      
      def leaky_relu(arr):
          alpha = 0.1
          
          return np.maximum(alpha*arr, arr)
      

      【讨论】:

        【解决方案4】:
        def leaky_relu_forward(x, alpha):
          out = x                                                
          out[out <= 0]=out[out <= 0]* alpha
          return out
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-13
          • 1970-01-01
          • 2017-12-31
          • 2018-02-20
          • 2018-03-06
          • 2021-11-18
          • 1970-01-01
          • 2018-10-11
          相关资源
          最近更新 更多