Skip to content

正则化

正则化是防止过拟合的技术。核心思想:限制模型复杂度,让它学到真正的规律而非噪声

训练时随机丢弃一部分神经元,迫使网络不过度依赖任何单个特征:

import torch.nn as nn
# Dropout 层:训练时随机将 20% 的神经元输出置零
dropout = nn.Dropout(p=0.2)
x = torch.randn(3, 10)
output = dropout(x) # 训练时一些值变为 0
print(f"非零比例: {(output != 0).sum().item() / output.numel():.1%}")
p 值场景
0.1输入层(轻微正则化)
0.2隐藏层(常用值)
0.5大模型过拟合严重时

在损失函数中加入权重的 L2 范数,惩罚过大的权重:

Ltotal=Loriginal+λw2L_{\text{total}} = L_{\text{original}} + \lambda \sum w^2
# AdamW 内置 weight decay,与梯度更新解耦
optimizer = torch.optim.AdamW(model.parameters(), lr=0.001, weight_decay=0.01)
方法原理实现
Dropout随机丢弃神经元nn.Dropout(p)
Weight Decay惩罚大权重AdamW(weight_decay=...)
数据增强扩充训练数据旋转、裁剪、加噪
Early Stopping验证集不再提升时停止监控 val_loss
Label Smoothing标签不要 100% 确定0.1 平滑
class RegularizedMLP(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Linear(784, 256),
nn.BatchNorm1d(256), # 归一化(也是一种正则化)
nn.ReLU(),
nn.Dropout(0.3), # Dropout
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(128, 10),
)
def forward(self, x):
return self.model(x)
# 训练时:model.train() → Dropout 生效
# 推理时:model.eval() → Dropout 自动关闭