Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement leaky relu using Numpy functions

I am trying to implement leaky Relu, the problem is I have to do 4 for loops for a 4 dimensional array of input.

Is there a way that I can do leaky relu only using Numpy functions?

like image 753
Liu Hantao Avatar asked May 24 '18 20:05

Liu Hantao


2 Answers

Here are two approaches to implement 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  
like image 158
Amir Avatar answered Oct 17 '22 11:10

Amir


import numpy as np




def leaky_relu(arr):
    alpha = 0.1
    
    return np.maximum(alpha*arr, arr)
like image 25
vikanksh nath Avatar answered Oct 17 '22 11:10

vikanksh nath