Transformer Training Essentials
Introduction
Welcome back to Bringing Transformers to Life: Training & Inference! You've made excellent progress in this course. In your first lesson, you assembled a complete Transformer architecture, integrating all the components we've built throughout this learning journey. In the second lesson, you created a robust data preparation pipeline that transforms raw text into training-ready tensors, complete with vocabularies, special tokens, and dynamic batching.
Now we are at a pivotal point, as in today's lesson we'll be discussing training the Transformer. This is where everything comes together as we implement the actual training process: you'll learn how Transformers learn through autoregressive modeling, where the model predicts the next token given all previous tokens. We'll explore teacher forcing, a key training technique that accelerates learning, and implement sophisticated optimization strategies, including learning rate scheduling with warmup. By the end of this lesson, you'll have a complete training pipeline that can effectively train your Transformer model on sequence-to-sequence tasks.
Understanding Autoregressive Training Objectives
The autoregressive training objective forms the foundation of how Transformers learn to generate sequences. Unlike traditional machine learning, where we might predict a single output, autoregressive models learn to predict each token in a sequence conditioned on all previous tokens. This creates a natural decomposition of the sequence generation probability.
Mathematically, for a target sequence , the autoregressive objective maximizes the likelihood: where is the source sequence and represents all tokens before position . This factorization allows the model to learn complex dependencies while maintaining computational tractability. During training, we use cross-entropy loss to measure how well the model's predicted probability distribution matches the true next token at each position.
Teacher Forcing: Training vs Inference
Teacher forcing is the standard way to train autoregressive Transformers (both encoder-decoder and decoder-only models).
During training we feed the ground-truth tokens into the decoder (or into the masked language-model input), while asking the model to predict the next token for every position. Because the model always sees the correct previous context, gradients are better behaved and convergence is much faster than if it had to consume its own, still-noisy predictions.
The downside is the resulting train–inference mismatch, often called exposure bias:
- Training: context consists of perfect tokens from the data set.
- Inference: context consists of the model's own predictions, which may contain mistakes that propagate.
Although exposure bias is an unwanted side-effect, in practice teacher forcing is still preferred because (1) it makes optimization tractable, (2) it yields state-of-the-art performance when combined with techniques such as scheduled sampling, label smoothing, or beam search, and (3) large, diverse data sets help the model learn to recover from occasional errors.
