M4-010M4: Sequences & TransformersSeq2Seq & Attention OriginsMedium
⚡Flashcard Drill➔Mastery:

Teacher Forcing and Exposure Bias in Autoregressive Sequence Generation

📐 Mathematical Definition
train: p(yt∣y<t∗,x);infer: p(yt∣y^<t,x);dist mismatch\text{train}:\ p(y_t|y^*_{<t},x);\qquad \text{infer}:\ p(y_t|\hat y_{<t},x);\qquad \text{dist mismatch}
⚡ 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 tt receives ground-truth target yt−1∗y_{t-1}^* as input; enables parallel training across all time steps
  • •
    Exposure Bias: during inference, the model receives its own potentially corrupted prediction y^t−1\hat{y}_{t-1}; 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: max⁡θ∑t=1Tlog⁡P(yt∗∣y1:t−1∗,X;θ)\max_\theta \sum_{t=1}^T \log P(y_t^* \mid y_{1:t-1}^*, X; \theta).
- Advantage: Clean gradients, rapid convergence, and full temporal parallelization (in Transformers, causal masking computes all TT 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: y^t∼P(⋅∣y^1:t−1,X;θ)\hat{y}_t \sim P(\cdot \mid \hat{y}_{1:t-1}, X; \theta).
- Distributional Shift: Let PdataP_{\text{data}} be the distribution of ground-truth prefixes, and PmodelP_{\text{model}} be the prefix distribution induced by sampling. In inference, inputs are drawn from Pmodel≠PdataP_{\text{model}} \ne P_{\text{data}}. 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 tt, input token is selected via coin flip: xt={yt−1∗with probability ϵiy^t−1with probability 1−ϵix_t = \begin{cases} y_{t-1}^* & \text{with probability } \epsilon_i \\ \hat{y}_{t-1} & \text{with probability } 1 - \epsilon_i \end{cases}.
Decay schedule gradually anneals ϵi\epsilon_i from 1.01.0 (pure teacher forcing) to 00 (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.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

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

← PreviousM4-009: Why Attention is Characterized as 'Differentiable Soft Alignment'📋Back to BankNext →M4-011: Beam Search Decoding and Length Bias Normalization
中文速览与面试要点

解释 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 与'过拟合'混为一谈