評估流程規範:訓練損失與驗證損失差距分析
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.