评估流程规范:训练损失与验证损失差距分析
Agent 辅助工程分析
前置要求
第 51 日预训练循环与第 42 日交叉熵损失。
学习目标
测量训练步数推进中训练集与验证集损失的分离曲线,界定过拟合开始发生的拐点。
操作指引
定义 estimate_loss() 函数:在 torch.no_grad() 与 model.eval() 环境下遍历 eval_iters 个批次。记录典型曲线:0步 (4.10, 4.12);500步 (2.20, 2.25);1500步 (1.10, 1.95)。指出 1000 步左右验证集损失停止下降的泛化平台期。
自主演练与记录
1. 为何评估循环必须在 torch.no_grad() 和 model.eval() 下执行?
2. 训练集损失持续走低而验证集损失开始抬头表明什么?
3. 挑选最优模型检查点(Checkpoint)的唯一标准是什么?
model.eval() 禁用 Dropout;torch.no_grad() 避免构建计算图,节省 50% 以上显存。
torch.no_grad() 避免保存中间激活值梯度图,显著节省显存;model.eval() 关闭随机 Dropout 以获得确定性评估。两者差距扩大表明模型正在死记硬背训练样本(过拟合)。最佳检查点必须严格以验证集最低损失为准,决不能以训练集损失为准。
掌握标准评估代码范式,明确验证集损失优选原则。
权威源资源与权利证明
nanoGPT: model.py architecture implementation
对应章节 / 时间戳: model.py: class GPT forward(idx, targets): wte + wpe, Blocks, ln_f, lm_head (returns cross-entropy loss when targets are given)
验证版本与日期: Git master (Verified: 2026-10-30)
Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.
Python, PyTorch, and Day 43-45 attention fundamentals.
Local models and classes in the curriculum mirror nanoGPT semantics directly.
对比候选资源与选型分析 (2)
Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.
Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.
Google Machine Learning Crash Course
对应章节 / 时间戳: Modules: Training and Test Sets; Overfitting; Classification accuracy
验证版本与日期: Current online edition (Verified: 2026-10-30)
Industrial, beginner-legible treatment of training/test splitting and overfitting; complements d2l.ai's mathematical framing.
Basic algebra; short self-contained modules.
Split discipline, leakage rules, and metric definitions are fully explained inside the lesson; the course is further reading.
对比候选资源与选型分析 (2)
Co-primary: deeper mathematical coverage of model selection, generalization, and optimization for the mathematics-track capstone.
Normative reference once learners call sklearn; kept as an alternative because these days teach evaluation protocol before library usage.