2

开始训练

初始化模型、优化器、调度器

循环每个epoch

循环每个batch(支持梯度累积)

混合精度前向传播 + 多损失计算

梯度累积检查 → 未满:继续累积
↓ 已满:
梯度裁剪 + 参数更新 + 学习率调整

定期验证 + 早停检查

保存最佳模型 + 训练日志

早停触发或epoch完成

混合精度训练 (AMP)

python
# 前向传播: FP16
with autocast('cuda'):
    outputs = self.model(...)

# 反向传播: 自动精度管理
self.scaler.scale(loss).backward()
self.scaler.step(optimizer)
self.scaler.update()

显存减半: FP16占用显存仅为FP32的一半
训练加速: 利用Tensor Core获得2-3倍速度提升

参数更新:

采用adamW优化器

学习率调度:

三重学习率调度,支持cosine余弦退火,onecycle

python
def _create_scheduler(self, total_steps):
    effective_steps = max(1, total_steps - self.warmup_steps)
    
    if self.scheduler_type == "cosine":
        self.scheduler = CosineAnnealingLR(optimizer, T_max=effective_steps)
    elif self.scheduler_type == "onecycle":
        self.scheduler = OneCycleLR(optimizer, max_lr=lr, total_steps=effective_steps)
    elif self.scheduler_type == "step":
        self.scheduler = StepLR(optimizer, step_size=effective_steps//3, gamma=0.5)

梯度裁剪 (Gradient Clipping)

python
# 防止训练发散的安全机制
torch.nn.utils.clip_grad_norm_(
    self.model.parameters(), 
    max_norm=self.max_grad_norm  # 通常设置为1.0
)

学习率预热与早停机制

这套技术组合在现代NLP训练中已成为标准实践,特别适合GPT-2这类Transformer架构的古诗词生成任务。

posted @ 2025-10-12 21:23  arin876  阅读(7)  评论(0)    收藏  举报