Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Deep Learning Notes
Topic #427

Teacher Forcing

Teacher forcing is a training technique for Seq2Seq models: instead of feeding the decoder its own (possibly wrong) previous prediction, feed it the true previous token from the training data — dramatically speeding up and stabilizing training, at the cost of a real train/inference mismatch.

The Core Idea

Compare the two approaches for generating the second output token during training, given the true target sequence "J'aime les chiens":

ApproachInput to Decoder at Step 2
Without teacher forcingWhatever the decoder predicted at step 1 (could be wrong, especially early in training)
With teacher forcing"J'aime" — the true, correct token, regardless of what the decoder predicted at step 1

By always feeding the correct previous token during training, each step's prediction task becomes "given the correct context so far, predict the next token" — a well-posed, stable learning problem — rather than "given whatever I may have gotten wrong so far, try to recover and predict the next token," which is a much harder, noisier problem, especially in early training when the decoder is still mostly guessing.

Why This Speeds Up Training Significantly

Without teacher forcing, an early mistake at step 1 could cascade — the decoder conditions step 2's prediction on an already-wrong step 1 token, compounding errors throughout the sequence and providing a very noisy, hard-to-learn-from gradient signal. Teacher forcing decouples each step's learning from the correctness of previous steps' predictions, letting the model learn each step's prediction task in relative isolation, which converges substantially faster.

Code

import torch

def train_step_with_teacher_forcing(encoder, decoder, input_seq, target_seq, loss_fn):
    h, c = encoder(input_seq)
    total_loss = 0

    decoder_input = target_seq[:, 0:1]   # start token
    for t in range(1, target_seq.shape[1]):
        logits, h, c = decoder(decoder_input, h, c)
        total_loss += loss_fn(logits.squeeze(1), target_seq[:, t])

        decoder_input = target_seq[:, t:t+1]   # TEACHER FORCING: use the TRUE token,
                                                  # not the model's own prediction, as next input
    return total_loss

The Cost: Exposure Bias

Training always shows the decoder the correct previous tokens; inference never does — the decoder must condition on its own (potentially imperfect) generated tokens instead, since the true answer isn't available at inference time. This train/inference mismatch is called exposure bias: the model never practices recovering from its own mistakes during training, since teacher forcing never exposes it to a wrong previous token, yet at inference it may well produce one and then have to continue from it regardless.

A Common Mitigation: Scheduled Sampling

Rather than using teacher forcing 100% of the time, scheduled sampling gradually mixes in the model's own predictions during training — starting mostly with teacher forcing early on (for training stability) and increasingly using the model's own generated tokens as training progresses (for better alignment with inference-time conditions), a practical compromise between fast convergence and exposure-bias mitigation.

Common Mistakes

  • Using teacher forcing during inference/evaluation by mistake — the true target sequence isn't available at inference time in any real deployment scenario, so evaluation must use the model's own autoregressive generation, exactly as covered in Seq2Seq Model.
  • Assuming 100% teacher forcing is always optimal — for tasks where exposure bias significantly hurts real-world performance, some mix of scheduled sampling or a lower teacher-forcing ratio can meaningfully help, at some training-stability cost.

Interview Relevance

Q: "What is 'exposure bias' in Seq2Seq training, and how does teacher forcing cause it?" Teacher forcing always conditions each training step on the correct previous token, so the model never practices recovering from its own mistakes during training. At inference, no ground truth is available — the decoder must condition on its own previous predictions, which may be wrong — creating a systematic mismatch between the (easier) conditions seen during training and the (harder) conditions faced at inference.

Practice Question

Why might a model trained purely with teacher forcing perform noticeably worse at inference time on longer output sequences specifically, compared to shorter ones?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →