需要掌握:激活函数(ReLU、Leaky ReLU、PReLU、tanh)
介绍
PyTorch 中的 nn.ReLU() 激活函数,这是深度学习中最基础、应用最广泛的激活函数,也是理解其他激活函数的核心基础。
ReLU激活函数
详见 链接
语法
nn.ReLU()
是 PyTorch 神经网络模块类,需要先实例化再使用;此外 PyTorch 也提供纯函数版 torch.relu(),两者功能一致,模块版更适合嵌入网络结构。
例子
import torch
import torch.nn as nn
# ========== 方式1:模块版(推荐用于网络定义) ==========
# 1. 实例化 ReLU 模块(可指定 inplace 参数,节省内存)
relu = nn.ReLU() # 默认 inplace=False
relu_inplace = nn.ReLU(inplace=True) # 原地修改输入张量,节省内存
# 2. 标量输入
x_scalar = torch.tensor(-1.5)
y_scalar = relu(x_scalar)
print("标量输出:", y_scalar) # 输出: tensor(0.)(负数置0)
# 3. 张量输入(实际场景主流用法)
x_tensor = torch.tensor([-3.0, -1.0, 0.0, 2.0, 5.0])
y_tensor = relu(x_tensor)
print("张量输出:", y_tensor) # 输出: tensor([0., 0., 0., 2., 5.])
# ========== 方式2:函数版(临时计算用) ==========
y_func = torch.relu(x_tensor)
print("函数版输出:", y_func) # 和模块版结果一致: tensor([0., 0., 0., 2., 5.])
# ========== 实际网络中使用 ==========
class SimpleCNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3)
self.relu = nn.ReLU() # 嵌入网络的ReLU层
self.fc1 = nn.Linear(16*30*30, 10) # 假设输入是3×32×32的图片
def forward(self, x):
x = self.conv1(x)
x = self.relu(x) # 卷积后接ReLU激活
x = x.flatten(1)
x = self.fc1(x)
return x
# 测试网络
model = SimpleCNN()
input_data = torch.randn(8, 3, 32, 32) # 批量8,3通道,32×32图片
output = model(input_data)
print("网络输出形状:", output.shape) # 输出: torch.Size([8, 10])
总结
nn.ReLU()是 PyTorch 实现的修正线性单元,核心逻辑是 $max(0, x)$,输出非负;- 有模块版(嵌入网络)和函数版(
torch.relu()),inplace=True可节省内存; - 优点是计算快、缓解梯度消失,缺点是可能出现“神经元死亡”,衍生出 LeakyReLU/PReLU 等变体,是CNN/MLP的基础激活函数。