M3-020M3: Deep Learning FoundationsNormalization TechniquesEasy
⚡Flashcard Drill➔Mastery:
Comparing BatchNorm and LayerNorm: Normalization Dimensions and Applicable Regimes
📐 Mathematical Definition
⚡ One-Sentence Core Answer
BatchNorm normalizes across batch and spatial dimensions per channel (CNNs); LayerNorm normalizes across feature channels per individual sample (Transformers/RNNs).
📌 Key Takeaways
- •BatchNorm: normalizes over for each channel ; couples samples within a batch; fails on small batches
- •LayerNorm: normalizes over hidden channels (or ) for each independent sample; batch-size independent
- •Sequence handling: LayerNorm handles variable-length sequences seamlessly without padding distortion
📐 Mathematical Derivations
Mathematical Formulations:
Let tensor shape be for vision or for sequences.
① Batch Normalization (Ioffe & Szegedy, 2015):
Computes mean and variance across batch and spatial dimensions for each channel : , .
Normalized value: .
② Layer Normalization (Ba, Kiros, Hinton, 2016):
Computes mean and variance across all features/channels for each individual instance at time-step : , .
Normalized value: .
Let tensor shape be for vision or for sequences.
① Batch Normalization (Ioffe & Szegedy, 2015):
Computes mean and variance across batch and spatial dimensions for each channel : , .
Normalized value: .
② Layer Normalization (Ba, Kiros, Hinton, 2016):
Computes mean and variance across all features/channels for each individual instance at time-step : , .
Normalized value: .
🏭 Production Trade-offs
Why NLP uses LayerNorm: In NLP, sequences have variable lengths and batch sizes can vary dynamically down to 1 during inference. BatchNorm over sequences causes zero-padding tokens to corrupt channel statistics and introduces train-test discrepancies. LayerNorm operates completely independently of other samples in the batch.
⚠️ Common Interview Pitfalls
- ✕Using BatchNorm on small batch sizes (), leading to noisy variance estimates and catastrophic training degradation
- ✕Forgetting to call
model.eval()before running inference with BatchNorm, causing predictions to vary depending on test batch composition
🎯 Interviewer Follow-up Inquiries
- ?Why does BatchNorm fail on small batch sizes, and why doesn't LayerNorm suffer from this problem?
- ?How does SyncBatchNorm coordinate cross-GPU statistics in distributed multi-GPU training?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
比较 BatchNorm 与 LayerNorm 的归一化维度与适用场景
BN 跨 batch 归一化(依赖 batch,CNN 常用);LN 跨特征归一化(与 batch 无关,Transformer/RNN 常用)。
核心要点
- 归一化维度的差异:设输入为 (N, C, H, W)(图像)或 (N, L, D)(序列)。 BatchNorm 对 每个通道 统计 跨样本(N)与空间(H,W) 的均值方差,故统计量依赖 batch 组成; LayerNorm 对 每个样本 统计 跨特征(D 或 C,H,W) 的均值方差,与 batch 无关。 适用场景 :① BN 适合 CNN(图像),因为图像特征的通道统计在样本间稳定、且大 batch 下统计量可靠;② LN 适合 Transformer/RNN,因为序列长度可变(BN 的跨样本统计会因 padding 而失真)、且小 batch 下 BN 统计不可靠。 为什么 NLP 用 LN :(a) 序列长度可变 → BN 需处理 padding 与变长统计,复杂且不准确;(b) NLP 任务的 batch 常较小(长序列占显存)→ BN 统计噪声大;(c) LN 对每个 token 独立归一化,与序列长度解耦。 BN 对小 batch 效果差的原因 :batch 越小,batch 统计量(均值/方差)的估计噪声越大(方差 ∝1/batch),导致归一化不稳定、running 统计偏离真实分布;极端情形(batch=1)时 BN 的方差为 0,完全失效。
- 实践要点:① BN 的训练/推理差异 ——训练用 batch 统计并更新 running 统计;推理用 running 统计(因为推理时可能无 batch 或 batch=1)。这个差异是 最常见的 bug 来源 (忘记 model.eval() 导致推理结果异常)。② LN 的无参数与有参数版本 ——LN 通常带可学习的 γ(缩放)与 β(平移),初始化为 γ=1、β=0(恒等); RMSNorm 去掉均值中心化与 β(见下题)。③ 归一化位置 ——BN 通常放在卷积后、激活前(Conv-BN-ReLU);LN 在 Transformer 中放在子层前(Pre-LN)或后(Post-LN)。④ 其他归一化 —— GN(Group Norm) (分组内归一化,与 batch 无关,适合小 batch 视觉任务)、 IN(Instance Norm) (风格迁移)、 Weight Norm (对权重归一化)。⑤ 实践建议 ——视觉任务用 BN(大 batch)或 GN(小 batch);NLP/序列用 LN/RMSNorm;Transformer 中优先 Pre-LN。⑥ 诊断 ——若训练 loss 震荡且梯度范数波动大,可能是 BN 统计不稳(batch 太小);可换 GN/LN 或增大 batch。
- BN 训练/推理行为不同(running stats)
- LN 训练推理一致,适合变长序列
常见陷阱
- 在变长序列上用 BN(padding 导致统计失真)
- 推理时忘记 model.eval() 导致 BN 用 batch 统计