outline
- 神经元与神经网络
- 激活函数
- 前向算法
- 损失函数
- 反向传播算法
- 数据的准备
- PyTorch实例:单层神经网络实现
3.2 激活函数
3.2.2 Tanh
- Tanh是一个双曲三角函数,其公式如下所示:
image.png
从图像上可以看出,与Sigmoid不同,它将输入变量映射到(-1,1)之间,它是Sigmoid函数经过简单的变换得到的。
- 导数
- 优缺点:
- 优点:由于其图形在定义域0附近近似线性,并且在整个定义域有可导性,该函数广泛用于深度学习领域的神经网络中作为神经元的激活函数
- 缺点:没有解决梯度消失问题
import torch
m=torch.nn.Tanh() #PyTorch中Tanh的定义
_input = torch.autograd.Variable(torch.rand(2))
print(_input)
print(m(_input))
由于梯度消失的原因,不推荐在隐含层使用Sigmoid和Tanh函数,但是可以在输出层使用。如果有必要使用Sigmoid的时候,Tanh要比Sigmoid好,因为Tanh是0均值。
3.2.3 Hard Tanh
![](https://img.haomeiwen.com/i8254644/d8d05e672cfcd756.png)
PyTorch中Hard Tanh支持指定阈值min_val和max_val以改变输出的最小值和最大值
import torch
m=torch.nn.Hardtanh(-2,2) #大于2的输入变成2,小于-2的输入变成-2
_input = torch.autograd.Variable(torch.rand(2))
print(_input)
print(m(_input))
有用就留个赞吧^_^
网友评论