๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #237

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?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Teacher Forcing โ€“ FAQs

Quick answers about learning Teacher Forcing in Deep Learning.

This free note from CodingNow 2.0 explains Teacher Forcing in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Teacher Forcing, is 100% free with no signup required.
With focused practice, most students grasp Teacher Forcing in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now