plot函数_深度学习--激活函数

神经网络的传播会使用到非线性映射,利用的就是一些激活函数:

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

特点:

  1. x很大/很小时,函数值会陷入饱和区(函数值为0/1);
  2. 神经网络反向传播时,涉及到激活函数的导数,会导致梯度消失;
  3. 图像可以看出,sigmoid函数值非0均值;
  4. 指数计算代价比较大。

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

特点:

  1. 0均值;
  2. 有梯度饱和区;
  3. 指数计算代价大;

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

特点:

  1. x>0的区域,没有梯度饱和和梯度消失的现象;
  2. 计算代价小;
  3. 非0均值;
  4. 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

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值