M3-085M3: Deep Learning FoundationsTraining Diagnostics & DebuggingEasy
⚡Flashcard Drill➔Mastery:

Systematic Troubleshooting Workflow When Training Loss Fails to Decrease

📐 Mathematical Definition
checklist: overfit-1-batch→data→lr→init→loss→grad→arch\text{checklist}:\ \text{overfit-1-batch}\to\text{data}\to\text{lr}\to\text{init}\to\text{loss}\to\text{grad}\to\text{arch}
⚡ One-Sentence Core Answer
First perform the single-batch overfitting sanity check; then systematically audit label correctness, learning rate schedules, initialization scale, gradient flow, and loss formulation.

📌 Key Takeaways

  • •
    Golden First Step: test whether the model can overfit a tiny batch (8–16 samples) to zero loss with regularization disabled
  • •
    If single batch fails: code bug (broken gradient, incorrect loss formulation, detached tensors, or bad initialization)
  • •
    If single batch passes: data pipeline bug (corrupted labels, unnormalized features, severe class imbalance, or wrong learning rate)

📐 Mathematical Derivations

Diagnostic Decision Tree:
① Step 1: The Single-Batch Overfitting Test:
Take a single batch of 8 samples. Set dropout=0, weight_decay=0, use Adam with η=10−3\eta = 10^{-3}, and train for 200 steps.
- Result A: Loss fails to reach $\approx 0$   ⟹  \implies Guaranteed Implementation Bug. Check: (1) Did you forget loss.backward() or optimizer.step()? (2) Did you call .detach() on a critical tensor? (3) Is loss formulated properly (e.g., passing probabilities into CrossEntropyLoss instead of logits)? (4) Are all weights initialized to zero?
- Result B: Loss easily drops to zero   ⟹  \implies Model and backprop code are 100% mathematically correct. The bug lies in data, capacity, or optimization scale.
② Step 2: Audit Data Pipeline:
- Are ground truth labels aligned with inputs, or did a data loader shuffle break correspondence?
- Are input features standardized? (Unnormalized inputs with magnitude 10510^5 freeze gradients).
③ Step 3: Audit Optimization Hyperparameters:
- Run Learning Rate Range Test. Learning rate may be 100×100\times too large (diverging) or 100×100\times too small (stagnating).

🏭 Production Trade-offs

Productivity principle: The single-batch test costs 30 seconds of compute and eliminates 80% of debugging guesswork. Never start by modifying model architecture or tweaking complex hyperparameters before verifying code correctness on a single batch.
⚠️ Common Interview Pitfalls
  • ✕
    Tuning learning rates and collecting more data when the core training loop has an active autograd graph detachment bug
  • ✕
    Skipping the single-batch sanity check and spending hours troubleshooting data distribution issues
🎯 Interviewer Follow-up Inquiries
  • ?
    Why is the single-batch overfitting test the most powerful binary isolation step in deep learning debugging?
  • ?
    What common subtle bugs cause tensors to become unintentionally detached from the autograd computational graph?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM3-084: 3D Parallelism and Sequence Parallelism (SP) in Large Foundation Models📋Back to BankNext →M3-086: Training Loss Decreases While Validation Loss Increases: Diagnosis and Remedies
中文速览与面试要点

训练 loss 不下降,你会按什么顺序排查?

从'能否过拟合单个 batch'开始,依次查数据/标签、lr、初始化、损失实现、梯度流、结构。

核心要点

  • 数学机理:调试的核心是 用最小代价二分定位 。 第一步:能否过拟合单个 batch ——取一个小 batch(如 8 个样本),关闭正则(dropout=0、weight decay=0),用较大 lr 训练几百步;若 loss 不能降到接近 0,说明 模型或训练代码有 bug (而非数据规模/超参问题),这是最高效的二分。 第二步:数据与标签 ——检查 (a) 输入是否被正确读取(可视化几个样本)、(b) 标签是否与输入对齐(打乱后 loss 应显著变差)、(c) 是否有 NaN/inf/全零样本、(d) 数据归一化是否正确(均值/方差)。 第三步:lr ——用 lr range test 找合理区间;lr 过小则下降极慢(看似'不降')。 第四步:初始化 ——检查输出 logits 的分布(初始应接近均匀、熵接近 log K);若初始 logits 幅度异常,说明初始化有问题。 第五步:损失实现 ——手算一个小例子的 loss 与框架对比(如用 2 个样本验证 CE 值);常见错误是维度/轴用错(如在错误维度求 softmax)、label 编码错(one-hot vs index)。 第六步:梯度流 ——检查各层梯度是否非零、是否有梯度消失(浅层梯度为 0)。 第七步:结构 ——最后才怀疑网络结构(残差、归一化位置、mask 等)。
  • 深度剖析与工程权衡:① 顺序的重要性 ——从'最可能出 bug 且最易验证'的地方开始;数据管道与损失实现是 bug 高发区(约 60% 的训练不收敛源于此),而结构问题相对少见。② '过拟合单 batch'的深意 ——任何有足够容量的模型都应能记住几个样本;若不能,说明 优化路径被阻断 (梯度未正确流动)或 目标不可达 (标签错)。这是'模型-优化-数据'三层中最快定位的一步。③ 常见'隐性'bug ——(a) 忘记 zero_grad()(梯度累积导致发散或不收敛)、(b) 在 eval 模式下训练(BN/dropout 行为错)、(c) 标签未 detach、(d) 数据未 shuffle、(e) 损失函数 reduction 用错(sum vs mean)。④ 与'loss 下降但指标差'的区别 ——本题是'loss 不降'(优化问题);若 loss 降但业务指标差,则是'损失-指标错配'问题(见对应题)。⑤ 自动化工具 ——用'梯度检查'(gradcheck)验证自定义算子、用'过拟合测试'做冒烟测试、用 hook 记录激活/梯度统计;把这些写进 CI 可避免回归。⑥ 面试要点 ——被问'模型训不起来怎么办',给出' 先过拟合单 batch → 再查数据 → 再查 lr → 再查损失 → 再查梯度 '的有序清单,并强调'先做二分实验而非盲调超参',这是最能体现调试素养的回答。
  • 先做'能否过拟合 1 个 batch'测试(最有效的二分法)
  • 检查数据管道与标签是否对齐、是否有 NaN/inf
  • 再查 lr、初始化、损失实现、梯度流

常见陷阱

  • 一上来就调 lr 和超参(未排除代码 bug)
  • 不做'过拟合单 batch'测试(错过最有效的二分)