Implement Relu derivative in python numpy

2019-04-08 02:42发布

I'm trying to implement a function that computes the Relu derivative for each element in a matrix, and then return the result in a matrix. I'm using Python and Numpy.

Based on other Cross Validation posts, the Relu derivative for x is 1 when x > 0, 0 when x < 0, undefined or 0 when x == 0

Currently, I have the following code so far:

def reluDerivative(self, x):
    return np.array([self.reluDerivativeSingleElement(xi) for xi in x])

def reluDerivativeSingleElement(self, xi):
    if xi > 0:
        return 1
    elif xi <= 0:
        return 0

Unfortunately, xi is an array because x is an matrix. reluDerivativeSingleElement function doesn't work on array. So I'm wondering is there a way to map values in a matrix to another matrix using numpy, like the exp function in numpy?

Thanks a lot in advance.

9条回答
做自己的国王
2楼-- · 2019-04-08 02:48

Basic function to return derivative of relu could be summarized as follows:

f'(x) = x > 0

So, with numpy that would be:

def relu_derivative(z):
    return np.greater(z, 0).astype(int)
查看更多
我命由我不由天
3楼-- · 2019-04-08 02:49

This works:

def dReLU(x):
    return 1. * (x > 0)
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-04-08 02:50

I guess this is what you are looking for:

>>> def reluDerivative(x):
...     x[x<=0] = 0
...     x[x>0] = 1
...     return x

>>> z = np.random.uniform(-1, 1, (3,3))
>>> z
array([[ 0.41287266, -0.73082379,  0.78215209],
       [ 0.76983443,  0.46052273,  0.4283139 ],
       [-0.18905708,  0.57197116,  0.53226954]])
>>> reluDerivative(z)
array([[ 1.,  0.,  1.],
       [ 1.,  1.,  1.],
       [ 0.,  1.,  1.]])
查看更多
虎瘦雄心在
5楼-- · 2019-04-08 02:57

You are on a good track: thinking on vectorized operation. Where we define a function, and we apply this function to a matrix, instead of writing a for loop.

This threads answers your question, where it replace all the elements satisfy the condition. You can modify it into ReLU derivative.

https://stackoverflow.com/questions/19766757/replacing-numpy-elements-if-condition-is-met

In addition, python supports functional programming very well, try to use lambda function.

https://www.python-course.eu/lambda.php

查看更多
淡お忘
6楼-- · 2019-04-08 02:58

When x is larger than 0, the slope is 1. When x is smaller than or equal to 0, the slope is 0.

if (x > 0):
    return 1
if (x <= 0):
    return 0

This can be written more compact:

return 1 * (x > 0)
查看更多
女痞
7楼-- · 2019-04-08 03:02

As mentioned by Neil in the comments, you can use heaviside function of numpy.

def reluDerivative(self, x):
    return np.heaviside(x, 0)
查看更多
登录 后发表回答