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.
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])
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With