Relu或者修正线性激活函数是深度学习领域中最常见的激活函数选择。Relu在提供先进结果的同时,在计算上也非常高效。
Relu激活函数的基本概念如下:
Return 0 if the input is negative otherwise return the input as it is.
我们可以用数学方式表示如下:

Relu的伪代码如下:
if input > 0:
return input
else:
return 0
在本教程中,我们将学习如何实现我们自己的ReLu函数,了解一些它的缺点,并了解ReLu的改进版本。
推荐阅读:机器学习的线性代数 [第1/2部分]
让我们开始吧!
在Python中实现ReLu函数
让我们在Python中编写我们自己的Relu实现。我们将使用内置的max函数来实现它。
ReLu的代码如下:
def relu(x):
return max(0.0, x)
为了测试这个函数,让我们对一些输入运行它。
x = 1.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -10.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 0.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 15.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -20.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
完整代码
下面是完整的代码:
def relu(x):
return max(0.0, x)
x = 1.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -10.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 0.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 15.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -20.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
输出:
Applying Relu on (1.0) gives 1.0
Applying Relu on (-10.0) gives 0.0
Applying Relu on (0.0) gives 0.0
Applying Relu on (15.0) gives 15.0
Applying Relu on (-20.0) gives 0.0
ReLu函数的梯度
让我们看看ReLu函数的梯度(导数)会是什么样子。对其进行求导,我们得到以下函数:
f'(x) = 1, x>=0
= 0, x<0
我们可以看到,对于小于零的x值,梯度为0。这意味着一些神经元的权重和偏置没有被更新。这在训练过程中可能会成为一个问题。
为了解决这个问题,我们有Leaky ReLu函数。 接下来让我们学习一下它。
Leaky ReLu函数
Leaky ReLu函数是常规ReLu函数的改进版本。为了解决负值梯度为零的问题,Leaky ReLu为负输入提供了一个极小的线性分量。
数学上,我们可以将Leaky ReLu表示为:
f(x)= 0.01x, x<0
= x, x>=0
数学上:
- f(x)=1 (x<0)
- (αx)+1 (x>=0)(x)
这里a是一个小常数,就像我们之前取的0.01一样。
从图形上可以表示为:

Leaky ReLu的梯度
让我们计算Leaky ReLu函数的梯度。梯度可以计算为:
f'(x) = 1, x>=0
= 0.01, x<0
在这种情况下,负输入的梯度是非零的。这意味着所有的神经元都将被更新。
在Python中实现Leaky ReLu
Leaky ReLu的实现如下:
def relu(x):
if x>0 :
return x
else :
return 0.01*x
让我们在一些输入上试试看。
x = 1.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -10.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 0.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 15.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -20.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
完整代码
Leaky ReLu的完整代码如下:
def leaky_relu(x):
if x>0 :
return x
else :
return 0.01*x
x = 1.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -10.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 0.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 15.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -20.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
输出:
Applying Leaky Relu on (1.0) gives 1.0
Applying Leaky Relu on (-10.0) gives -0.1
Applying Leaky Relu on (0.0) gives 0.0
Applying Leaky Relu on (15.0) gives 15.0
Applying Leaky Relu on (-20.0) gives -0.2
结论
{
“error”: “Upstream error…”
}
Source:
https://www.digitalocean.com/community/tutorials/relu-function-in-python