在python numpy中实现Relu导数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46411180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement Relu derivative in python numpy
提问by Bon
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.
我正在尝试实现一个函数,该函数计算矩阵中每个元素的 Relu 导数,然后在矩阵中返回结果。我正在使用 Python 和 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
基于其他交叉验证帖子,当 x > 0 时 x 的 Relu 导数为 1,当 x < 0 时为 0,当 x == 0 时为未定义或 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?
不幸的是,xi 是一个数组,因为 x 是一个矩阵。reluDerivativeSingleElement 函数不适用于数组。所以我想知道有没有办法使用 numpy 将矩阵中的值映射到另一个矩阵,就像 numpy 中的 exp 函数一样?
Thanks a lot in advance.
非常感谢。
采纳答案by Irshad Bhat
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.]])
回答by Jakub Bartczuk
That's an exercise in vectorization.
这是矢量化的练习。
This code
这段代码
if x > 0:
y = 1
elif xi <= 0:
y = 0
Can be reformulated into
可以改写成
y = (x > 0) * 1
This is something that will work for numpy arrays, since boolean expressions involving them are turned into arrays of values of these expressions for elements in said array.
这适用于 numpy 数组,因为涉及它们的布尔表达式被转换为所述数组中元素的这些表达式的值的数组。
回答by milosdju
Basic function to return derivative of relu could be summarized as follows:
返回 relu 导数的基本函数可以总结如下:
f'(x) = x > 0
So, with numpy that would be:
因此,使用 numpy 将是:
def relu_derivative(z):
return np.greater(z, 0).astype(int)
回答by Krishna Mishra
def dRelu(z):
return np.where(z <= 0, 0, 1)
Here z is a ndarray in my case.
在我的例子中,z 是一个 ndarray。
回答by Kirill Dolmatov
If you want to use pure Python:
如果你想使用纯 Python:
def relu_derivative(x):
return max(sign(x), 0)
回答by user3503711
def reluDerivative(self, x):
return 1 * (x > 0)
回答by hxd1011
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.
你在一个很好的轨道上:考虑向量化操作。在我们定义一个函数的地方,我们将这个函数应用到一个矩阵,而不是写一个 for 循环。
This threads answers your question, where it replace all the elements satisfy the condition. You can modify it into ReLU derivative.
这个线程回答了你的问题,它替换了所有满足条件的元素。您可以将其修改为 ReLU 导数。
https://stackoverflow.com/questions/19766757/replacing-numpy-elements-if-condition-is-met
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.
另外python对函数式编程的支持很好,尽量使用lambda函数。
回答by Shital Shah
This works:
这有效:
def dReLU(x):
return 1. * (x > 0)
回答by Rishabh Agrahari
回答by marnix
When x is larger than 0, the slope is 1. When x is smaller than or equal to 0, the slope is 0.
当 x 大于 0 时,斜率为 1。当 x 小于或等于 0 时,斜率为 0。
if (x > 0):
return 1
if (x <= 0):
return 0
This can be written more compact:
这可以写得更紧凑:
return 1 * (x > 0)