神经网络的传播会使用到非线性映射,利用的就是一些激活函数:
sigmoid函数
def sigmoid(x):
return 1 / (1 + np.exp(-x))
plt.figure()
x = np.arange(-20, 20, 0.1)
y1 = sigmoid(x)
y2 = sigmoid(x) * (1 - sigmoid(x))
plt.plot(x, y1, label='Sigmoid')
plt.plot(x, y2, label='Derivative')
plt.title('sigmoid function')
plt.legend()
plt.show()
![81d60d6efffea183c7756975e7524c8b.png](https://i-blog.csdnimg.cn/blog_migrate/bb24357da18c622e0ae5afda63b80d17.png)
特点:
- x很大/很小时,函数值会陷入饱和区(函数值为0/1);
- 神经网络反向传播时,涉及到激活函数的导数,会导致梯度消失;
- 图像可以看出,sigmoid函数值非0均值;
- 指数计算代价比较大。
Tanh函数
def tanh(x):
return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
plt.figure()
x = np.arange(-20, 20, 0.1)
y1 = tanh(x)
y2 = 1 - tanh(x) ** 2
plt.plot(x, y1, label='Tanh')
plt.plot(x, y2, label='Derivative')
plt.title('Tanh function')
plt.legend()
plt.show()
![ca685232e8ddf2545acd5d6416f593a2.png](https://i-blog.csdnimg.cn/blog_migrate/ccc0cb367974abc685695da0fdb7cd08.png)
特点:
- 0均值;
- 有梯度饱和区;
- 指数计算代价大;
ReLU函数
def relu(x):
return [max(0, i) for i in x]
plt.figure()
x = np.arange(-1, 1.05, 0.05)
y1 = relu(x)
y2 = [[1, 0][i == 0] for i in y1]
plt.plot(x, y1, label='ReLU')
plt.plot(x, y2, label='Derivative')
plt.title('ReLU function')
plt.legend()
plt.show()
![d4e3546ea0d61b85d150e0493f1d291a.png](https://i-blog.csdnimg.cn/blog_migrate/32e377623de5a02bc4ecf08d312fc7bc.png)
特点:
- x>0的区域,没有梯度饱和和梯度消失的现象;
- 计算代价小;
- 非0均值;
- dead relu:x<0的区域,函数值为0,该神经元“死亡”;梯度为0,神经网络的参数不会被更新。
Leaky ReLU:
相当于在ReLU的基础上添加了一个斜率,解决ReLU的dead relu问题。
def LeakyRelu(x):
return [max(0.1*i, i) for i in x]
plt.figure()
x = np.arange(-2, 2, 0.1)
y1 = LeakyRelu(x)
plt.plot(x, y1, label='LeakyRelu')
plt.title('LeakyRelu function')
plt.legend()
plt.show()
![aa90492ab8090e546713ebaf33eba570.png](https://i-blog.csdnimg.cn/blog_migrate/0ccb02c1d993438530d48da030346073.png)
PReLU函数
这里的
ELU函数
def ELU(x):
return [[0.2 * (np.exp(i) - 1), i][i > 0] for i in x]
plt.figure()
x = np.arange(-1, 1, 0.02)
y1 = ELU(x)
plt.plot(x, y1, label='ELU')
plt.title('ELU function')
plt.legend()
plt.show()
![31356e8261b33cb54bdf9ab38cbfaaa2.png](https://i-blog.csdnimg.cn/blog_migrate/9ef4a656becd11007a1a2bda683b3915.png)