3.5.13 · D4Sequence Models

Exercises — Teacher forcing

3,132 words14 min readBack to topic

Everything here builds on Teacher forcing. If a term feels shaky, re-read the parent first.

A quick reminder of the two words we lean on the whole page, in plain language:

Figure — Teacher forcing

Two numbers we compute over and over:


Level 1 — Recognition

Recall Solution — L1.1

Training with teacher forcing: (b) the ground-truth . The correct earlier token is handed in, like training wheels. Inference: it has no choice — the true continuation does not exist yet, so it must feed its own . This mismatch between the two situations is exactly exposure bias.

Recall Solution — L1.2
  • : pure teacher forcing — always feed ground truth. Standard, fast, stable.
  • : pure free running — always feed the model's own prediction. Hard, unstable.
  • : scheduled sampling — flip a fair coin each step; sometimes truth, sometimes prediction. See 3.5.14-Scheduled-sampling. is literally the probability of using the ground truth at any given step.
Recall Solution — L1.3

. Higher confidence on the right token would push this toward .


Level 2 — Application

Recall Solution — L2.1

Per-step : Sum: . Because every step got the true previous token, no error could cascade — each probability is read directly off a clean history.

Recall Solution — L2.2

Sum . That is about 4.4× the teacher-forced loss. The spike at is the cascade: one wrong argmax became the next step's input, dragging the model into a state it never trained on. This is the numeric fingerprint of exposure bias.

Recall Solution — L2.3
  • : , and .
  • : , but (floor kicks in).
  • Floor reached when . For all , .

Level 3 — Analysis

Recall Solution — L3.1

The culprit is . It returns the index of the largest logit — a step function that is flat almost everywhere (tiny changes in logits don't change the chosen index) and jumps discontinuously when the top-2 tokens swap. A flat function has derivative ; a jump has no derivative. So (or undefined), and the error signal at step cannot propagate through the previous-step selection. Teacher forcing fix: the input is a constant from the dataset, not a function of . There is no in the path, so gradients from every step flow straight back to the parameters without passing through a non-differentiable choice. This is a large part of why teacher forcing trains fast and stably.

Recall Solution — L3.2

Each step independently uses prediction with probability . Expected prediction-steps . Probability all six use ground truth . So even at a fully teacher-forced sequence is rare — the model spends most runs practising recovery, which is the whole point of the compromise.

Recall Solution — L3.3

Exposure bias is the gap between the loss on ground-truth histories and the loss on the model's own histories:

  • Student A: (small — robust to its own mistakes).
  • Student B: (large — collapses once it feeds itself). Student B suffers far more. Equal training loss says nothing about robustness; only the gap does.

Level 4 — Synthesis

Recall Solution — L4.1

Linear part must go from to . Slope per epoch. So:

  • : warm-up region .
  • : .
  • : floor region . This mirrors curriculum learning: easy (stable) inputs first, harder (self-generated) inputs later.
Recall Solution — L4.2
h = h0
total_loss = 0
input_t = <START>            # first step: fixed start token, never a prediction
for t = 1 to T:
    output_t, h = f_theta(input_t, h)
    total_loss += CrossEntropy(output_t, y_t)
    # choose next input
    if t < T:
        if random() < epsilon:
            input_t = y_t          # ground truth (teacher forcing)
        else:
            input_t = argmax(output_t)   # model's own prediction (free running)
backprop(total_loss); update(theta)

Key points: the loop always scores against the true target (loss uses ground truth); only the next input is chosen by the coin flip. Step is special — it uses the fixed <START> token, so there is never an undefined "previous prediction." If : random() < 1.0 is always true, so input_t = y_t every step — the argmax branch is dead code. This recovers pure teacher forcing, and the loss reduces to of L2.1.

Recall Solution — L4.3

Teacher forcing lives only in the decoder, because only the decoder is autoregressive — its input at step is the previous output token (or ). That previous token is where we choose truth vs prediction. The encoder reads the source sentence, which is fully known and fixed at both training and inference; it has no "previous prediction" to condition on, so the teacher-forcing knob simply doesn't apply to it. The context vector is the same either way; only how the decoder feeds itself changes.


Level 5 — Mastery

Recall Solution — L5.1

Training only minimises : the parameters are tuned so that ground-truth histories give low loss. The model has never optimised for its own-generated histories. Whenever a self-generated token differs from the truth, the model enters a context it was not trained on, so on average it assigns less probability to the correct next token there higher per-step loss typically. When negative? Rarely, and only by luck: if the model's own predictions happen to exactly match the ground truth for a particular sequence, then the two histories are identical and . And on an individual noisy example the self-history could accidentally lead to a confidently-correct region, giving a slightly negative gap — but this does not survive averaging over data, because training never rewarded off-truth states. So the expected gap is .

Recall Solution — L5.2

(A) . (B) . (C) . Ranking (best worst loss): . Reading: teacher forcing is cheapest but least like inference; free running with a cascade explodes; scheduled sampling pays a modest premium ( vs ) to buy robustness — the sweet spot.

Recall Solution — L5.3

Idea — "soft-mix teacher forcing": instead of feeding either the hard truth token or a hard argmax, feed a soft blend of the truth embedding and the model's softmax-weighted predicted embedding: where is the token embedding and is the model's full probability distribution over vocabulary (not an argmax). Why it dodges the trap: the second term is a smooth, differentiable function of (a weighted average of embeddings, weights = softmax probabilities). No argmax anywhere, so gradients flow cleanly — unlike hard free running. Why it shrinks exposure bias: as the model conditions on its own belief, so it practises recovering from its own imperfect states, narrowing the train/test gap. Annealing from small over epochs mirrors scheduled sampling but stays fully differentiable. Trade-off honestly stated: the soft blend is not exactly the discrete token the model will see at inference, so a residual gap remains — no free lunch, but a strictly better gradient than straight-through argmax.

Recall Self-test recap

Teacher forcing feeds what during training? ::: The ground-truth previous token . The single operation that blocks gradients in free running? ::: (flat/discontinuous). Exposure bias equals which difference? ::: . means? ::: Pure teacher forcing, every step. Expected prediction-steps at over ? ::: .