Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReLU Prime with NumPy array

I want to pass a multidimensionnal array into the relu prime function

def reluprime(x):
    if x > 0:
        return 1
    else:
        return 0

... where the x is the whole array. It returns

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I've had this problem with the normal relu function, and instead of using the python function max() I used np.max() and it worked. But with the relu prime, it is not working either way. I tried:

def reluprime(x):
    if np.greater(x, 0):
        return 1
    else:
        return 0

... and it still returned the same error. How can I fix this? Thank you.

like image 646
Tissuebox Avatar asked Jul 10 '17 21:07

Tissuebox


2 Answers

Since relu prime returns 1 if an entry in a vector is bigger than 0 and 0 otherwise, you could just do:

def reluprime(x):
    return (x>0).astype(x.dtype)

in the code above, the input array x is assumed to be a numpy array. For example, reluprime(np.array([-1,1,2])) returns array([0, 1, 1]).

like image 51
Miriam Farber Avatar answered Sep 25 '22 14:09

Miriam Farber


The if statement does not make sense since it is only evaluated once, for the whole array. If you want the equivalent of an if statement for each element of the array, you should do something like:

def reluprime(x):
    return np.where(x > 0, 1.0, 0.0)
like image 44
Jonas Adler Avatar answered Sep 24 '22 14:09

Jonas Adler