M4-010M4: Sequences & TransformersSeq2Seq & Attention OriginsMedium
⚡Flashcard Drill➔Mastery:
Teacher Forcing and Exposure Bias in Autoregressive Sequence Generation
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Teacher forcing trains models by feeding ground-truth historical prefixes; exposure bias arises because inference feeds self-generated tokens, causing errors to compound rapidly.
📌 Key Takeaways
- •Teacher Forcing: training step receives ground-truth target as input; enables parallel training across all time steps
- •Exposure Bias: during inference, the model receives its own potentially corrupted prediction ; out-of-distribution drift
- •Compounding errors: a single erroneous token early in generation throws subsequent hidden states into unexplored regions
📐 Mathematical Derivations
Mathematical Formulations and Dynamics:
① Teacher Forcing Training Regime:
Target likelihood: .
- Advantage: Clean gradients, rapid convergence, and full temporal parallelization (in Transformers, causal masking computes all positions in a single forward pass).
- Flaw: The network is never trained on its own mistakes; it always assumes past history is 100% grammatically and semantically perfect.
② Inference Reality (Free Running):
Generated sequence: .
- Distributional Shift: Let be the distribution of ground-truth prefixes, and be the prefix distribution induced by sampling. In inference, inputs are drawn from . If the model generates an awkward word at step 5, it enters an unfamiliar state not seen during training, triggering cascading hallucination.
③ Mitigation: Scheduled Sampling (Bengio et al., 2015):
At step , input token is selected via coin flip: .
Decay schedule gradually anneals from (pure teacher forcing) to (free running).
① Teacher Forcing Training Regime:
Target likelihood: .
- Advantage: Clean gradients, rapid convergence, and full temporal parallelization (in Transformers, causal masking computes all positions in a single forward pass).
- Flaw: The network is never trained on its own mistakes; it always assumes past history is 100% grammatically and semantically perfect.
② Inference Reality (Free Running):
Generated sequence: .
- Distributional Shift: Let be the distribution of ground-truth prefixes, and be the prefix distribution induced by sampling. In inference, inputs are drawn from . If the model generates an awkward word at step 5, it enters an unfamiliar state not seen during training, triggering cascading hallucination.
③ Mitigation: Scheduled Sampling (Bengio et al., 2015):
At step , input token is selected via coin flip: .
Decay schedule gradually anneals from (pure teacher forcing) to (free running).
🏭 Production Trade-offs
RL / DPO Alternative: In modern LLM training, models undergo Supervised Fine-Tuning (SFT) with teacher forcing, followed by Reinforcement Learning from Human Feedback (RLHF / DPO) where models generate full unconstrained trajectories, directly eliminating exposure bias.
⚠️ Common Interview Pitfalls
- ✕Attempting to use Scheduled Sampling in standard Transformer training; feeding self-generated tokens breaks causal mask parallelization and forces slow sequential unrolling
- ✕Equating exposure bias with simple overfitting; exposure bias is a distribution shift between training conditioning and inference conditioning
🎯 Interviewer Follow-up Inquiries
- ?Why is Scheduled Sampling computationally incompatible with parallel Transformer training?
- ?How does Direct Preference Optimization (DPO) alleviate exposure bias during LLM post-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.
中文速览与面试要点
解释 teacher forcing 与 exposure bias
teacher forcing 训练时用真实前缀,推理时用模型自己的输出;训练-推理输入分布不一致导致误差累积(exposure bias)。
核心要点
- 数学机理: teacher forcing 是自回归模型的标准训练方式:第 t 步的输入是 真实 的前缀 y*_ exposure bias(暴露偏差) 指训练与推理的 输入分布不一致 :训练时模型只见过'正确前缀',推理时(第 t 步)输入是 模型自己生成的 ŷ_ 从未见过 的分布区域——模型在这个分布外区域的预测质量无保证,可能继续出错,形成 误差累积 (error accumulation)。形式上,训练优化的是 Σ_t log p(y*_t|y*_ 序列长度的影响 ——误差累积的概率随长度增长(每步有 ε 的概率偏离,T 步后偏离概率约 1−(1−ε)^T),故长序列上 exposure bias 更严重。 缓解手段 :(a) scheduled sampling (Bengio 等 2015)——训练时以概率 p 用模型自己的输出替代真值,p 随训练从 0 退火到某上界;(b) 序列级训练 (最小风险训练 MRT、BLEU 作为目标);(c) 强化学习 (用序列级奖励做 policy gradient,如 SCST);(d) RLHF/DPO (用偏好数据直接优化生成质量)。
- 深度剖析与工程权衡:① 为什么不能完全用自回归训练 ——若一开始就用模型自己的输出,早期模型输出质量极差、梯度信号混乱,训练无法启动;故需要'从 teacher forcing 逐步过渡'(scheduled sampling 的退火)或'两阶段'(先 teacher forcing 预训练、再序列级微调)。② scheduled sampling 的副作用 ——它引入了训练/推理的另一种不一致(随机混合),且梯度估计有偏(每步依赖采样);实践中效果不稳定,故更常用'两阶段 + 序列级微调'。③ 现代 LLM 的立场 ——LLM 的预训练与 SFT 几乎全用 teacher forcing (因为规模足够大、数据足够多样,模型对'自己生成的分布'也见得多);exposure bias 主要通过 (a) 海量数据、(b) RLHF/DPO 的偏好对齐、(c) 推理时的解码策略(beam search、温度)缓解。这说明'exposure bias 的重要性随数据规模下降'。④ 与 teacher forcing 并行的效率优势 ——teacher forcing 使所有时间步可并行(输入已知),这是 Transformer 训练高效的前提;纯自回归训练(每步依赖上一步输出)无法并行。⑤ 与'off-policy/on-policy'的类比 ——teacher forcing 相当于 off-policy (用行为策略的数据),自回归推理是 on-policy (用自身策略);RLHF 的核心正是'在 on-policy 数据上优化'。⑥ 面试要点 ——被问'exposure bias',应给出' 训练用真值前缀、推理用自生成前缀 → 分布不一致 → 误差累积 '的因果链,并说明'长序列更严重'与'三阶段缓解方案';能类比 off-policy/on-policy 是深度理解的标志。
- 训练用真值前缀,推理用自生成前缀(分布不同)
- 一步错误会改变后续输入分布,导致误差累积
- 缓解:scheduled sampling、序列级训练、RL/最小风险训练
常见陷阱
- 以为 teacher forcing 的并行性是免费的(代价是暴露偏差)
- 把 exposure bias 与'过拟合'混为一谈