需要掌握:tanh激活函数
介绍
torch.tanh() 函数,这是深度学习中非常常用的激活函数之
tanh 激活函数
这里是简单介绍,详见链接
tanh(双曲正切函数)是一种非线性激活函数,它将输入值映射到 [-1, 1] 的区间内。
数学公式
tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}
特点
输出以 0 为中心(相比 sigmoid 函数更优),但依然存在“梯度消失”问题(输入绝对值过大时,梯度趋近于 0)。
语法
可以接收标量、张量(Tensor)作为输入,返回对应形状的输出张量。
torch.tanh()
例子
import torch
# 1. 标量输入
x_scalar = torch.tensor(2.0)
y_scalar = torch.tanh(x_scalar)
print("标量输出:", y_scalar) # 输出: tensor(0.9640)
# 2. 张量输入
x_tensor = torch.tensor([-3.0, 0.0, 3.0])
y_tensor = torch.tanh(x_tensor)
print("张量输出:", y_tensor) # 输出: tensor([-0.9951, 0.0000, 0.9951])
# 3. 多维张量输入
x_2d = torch.tensor([[1.0, -2.0], [0.5, -1.0]])
y_2d = torch.tanh(x_2d)
print("二维张量输出:\n", y_2d)
# 输出:
# tensor([[ 0.7616, -0.9640],
# [ 0.4621, -0.7616]])
注意事项
torch.tanh()是逐元素计算的,即对张量中的每个元素单独应用 tanh 函数;反向传播时,tanh 的梯度是 1 - tanh(x)^2,当 x 绝对值很大时,梯度趋近于 0,可能导致模型训练缓慢(梯度消失)。
总结
torch.tanh()是 PyTorch 实现的双曲正切激活函数,输出范围为 [-1, 1],且以 0 为中心;- 支持标量、任意维度张量输入,逐元素计算;
- 优点是输出中心化,缺点是存在梯度消失问题,实际中常被 ReLU 替代,但在 RNN、GAN 等场景仍有应用。