Relu(或 Rectified Linear Activation Function)是深度学习(/community/tutorials/introduction-to-machine-learning)世界中最常见的激活函数选项。
** Relu 激活函数的基本概念如下:**
1Return 0 if the input is negative otherwise return the input as it is.
我们可以用数学来形容它如下:
Relu的伪代码如下:
1if input > 0:
2 return input
3else:
4 return 0
在本教程中,我们将学习如何实现自己的ReLu函数,了解其一些缺点,并了解ReLu的更好的版本。
推荐阅读: Linear Algebra for Machine Learning [Part 1/2]
让我们开始吧!
在 Python 中实现 ReLu 函数
让我们在Python中写下我们自己的Relu实现,我们将使用内置MAX函数来实现它。
ReLu 的代码如下:
1def relu(x):
2 return max(0.0, x)
要测试该函数,让我们在几个输入上运行它。
1x = 1.0
2print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
3x = -10.0
4print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
5x = 0.0
6print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
7x = 15.0
8print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
9x = -20.0
10print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
完整代码
完整的代码如下:
1def relu(x):
2 return max(0.0, x)
3
4x = 1.0
5print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
6x = -10.0
7print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
8x = 0.0
9print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
10x = 15.0
11print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
12x = -20.0
13print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
输出:
1Applying Relu on (1.0) gives 1.0
2Applying Relu on (-10.0) gives 0.0
3Applying Relu on (0.0) gives 0.0
4Applying Relu on (15.0) gives 15.0
5Applying Relu on (-20.0) gives 0.0
ReLu 函数的格列度
让我们看看 ReLu 函数的梯度(衍生性)是什么,在差异化时,我们会得到以下函数:
1f'(x) = 1, x>=0
2 = 0, x<0
我们可以看到,对于x小于零的值,梯度为0,这意味着某些神经元的重量和偏见没有更新。
为了克服这个问题,我们有 ** Leaky ReLu 函数。
漏洞 ReLu 函数
泄漏 ReLu 函数是正常 ReLu 函数的改进,为了解决负值的零梯度问题,泄漏 ReLu 会将 x 的极小线性组件给负输入。
数学上,我们可以将 Leaky ReLu 表达为:
1f(x)= 0.01x, x<0
2 = x, x>=0
数学上:
- f(x)=1 (x<0)
- (αx)+1 (x>=0)(x)
在这里, a 是一个小常数,就像我们上面的 0.01 一样。
图形上可以显示为:
泄漏的ReLu的梯度
让我们计算 Leaky ReLu 函数的梯度,梯度可以是:
1f'(x) = 1, x>=0
2 = 0.01, x<0
在这种情况下,负输入的梯度不是零,这意味着所有神经元都将被更新。
在 Python 中实施 Leaky ReLu
Leaky ReLu 的实施方法如下所示:
1def relu(x):
2 if x>0 :
3 return x
4 else :
5 return 0.01*x
讓我們來試一下入口。
1x = 1.0
2print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
3x = -10.0
4print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
5x = 0.0
6print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
7x = 15.0
8print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
9x = -20.0
10print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
完整代码
Leaky ReLu 的完整代码如下:
1def leaky_relu(x):
2 if x>0 :
3 return x
4 else :
5 return 0.01*x
6
7x = 1.0
8print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
9x = -10.0
10print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
11x = 0.0
12print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
13x = 15.0
14print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
15x = -20.0
16print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
输出:
1Applying Leaky Relu on (1.0) gives 1.0
2Applying Leaky Relu on (-10.0) gives -0.1
3Applying Leaky Relu on (0.0) gives 0.0
4Applying Leaky Relu on (15.0) gives 15.0
5Applying Leaky Relu on (-20.0) gives -0.2
结论
本教程是关于Python中的ReLu函数。我们还看到了ReLu函数的改进版本。Leaky ReLu解决了ReLu函数中负值的零梯度问题。