I have implemented ReLu derivative as:
def relu_derivative(x):
return (x>0)*np.ones(x.shape)
I also tried:
def relu_derivative(x):
x[x>=0]=1
x[x<0]=0
return x
Size of X=(3072,10000). But it's taking much time to compute. Is there any other optimized solution?
Approach #1 : Using
numexpr
When working with large data, we can use
numexpr
module that supports multi-core processing if the intended operations could be expressed as arithmetic ones. Here, one way would be -Thus, to solve our case, it would be -
Approach #2 : Using NumPy
views
Another trick would be to use
views
by viewing the mask of comparisons as anint
array, like so -On performance, it should be identical to creating
X>=0
.Timings
Comparing all posted solutions on a random array -
The
numexpr
based one was with8
threads. Thus, with more number of threads available for compute, it should improve further.Related post
on how to control multi-core functionality.Approach #3 : Approach #1 + #2 -
Mix both of those for the most optimal one for large arrays -