Module 17 — Transformer Training
Training a Transformer model involves computing predictions, measuring the loss, propagating gradients backward through the network, and updating model parameters using an optimization algorithm.
Modern Transformer models are commonly trained using AdamW, learning rate warmup, gradient clipping, and weight decay to improve convergence and stability.
Topics
- Backpropagation
- Gradient Clipping
- Warmup
- Weight Decay
- AdamW
1. Forward Pass
The Transformer first computes predictions.
Formula
where
- = Input sequence
- = Predicted output
2. Loss Function
The training objective minimizes the loss.
Formula
where
- = Ground Truth
- = Prediction
3. Backpropagation
Backpropagation computes gradients of the loss with respect to every trainable parameter.
Gradient
For all parameters,
4. Gradient Descent Update
Parameters are updated using the computed gradients.
Formula
where
- = Parameters
- = Learning Rate
5. Gradient Clipping
Large gradients may cause unstable training.
Gradient clipping limits the gradient norm.
Formula
where
- = Original Gradient
- = Maximum Gradient Norm
If
the gradient is scaled down.
6. Learning Rate Warmup
Instead of using a large learning rate immediately, Transformers gradually increase the learning rate during early training.
Original Transformer Warmup
where
- = Current training step
- = Number of warmup steps
7. Learning Rate Decay
After the warmup phase, the learning rate gradually decreases.
A common cosine decay schedule is
where
- = Current step
- = Total training steps
8. Weight Decay
Weight decay regularizes the model by discouraging excessively large parameter values.
Formula
where
- = Weight Decay Coefficient
9. Adam Optimizer
Adam computes adaptive first and second moments.
First Moment
Second Moment
Bias Correction
Adam Update
10. AdamW
AdamW separates weight decay from the adaptive optimization step.
Formula
AdamW is the default optimizer for most modern Transformer and LLM training.
11. Complete Training Pipeline
The complete optimization process is
12. Optimization Objective
The goal is to find optimal parameters.
Formula
13. Matrix Dimensions
Suppose
- Parameters
Gradient
Updated Parameters
14. Why These Techniques Matter
Modern Transformer training relies on
- Stable gradients
- Adaptive optimization
- Learning rate scheduling
- Proper regularization
- Efficient convergence
These techniques are used in
- BERT
- GPT
- GPT-2
- GPT-3
- GPT-4
- T5
- LLaMA
- Gemma
- Qwen
- Mistral
- DeepSeek
- Phi
- Vision Transformers
- Large Language Models (LLMs)
Summary
| Concept | Formula |
|---|---|
| Backpropagation Gradient |