Introduction
The AdamW Optimizer is an improved version of the Adam Optimizer that introduces a better way of applying Weight Decay (L2 Regularization).
Although Adam performs exceptionally well, researchers found that its implementation of L2 regularization was not ideal.
AdamW separates weight decay from gradient updates, resulting in better generalization and improved model performance.
What is AdamW?
AdamW (Adam with Weight Decay) is an optimization algorithm that combines Adam's adaptive learning rates with decoupled weight decay regularization.
In simple terms:
AdamW is Adam with a better implementation of regularization.
Why Do We Need AdamW?
Standard Adam sometimes:
- Overfits the training data.
- Generalizes poorly.
- Handles weight decay inefficiently.
AdamW solves these issues.
Working of AdamW
Initialize Parameters
↓
Compute Gradient
↓
Compute Adam Updates
↓
Apply Weight Decay
↓
Update Parameters
↓Repeat
Mathematical Representation
Adam update:
W = W − η × (m̂t / (√v̂t + ε)) Weight decay update:
W = W − ηλW Combined update:
W = W − η × (m̂t / (√v̂t + ε)) − ηλWwhere:
- W = weights
- η = learning rate
- λ = weight decay coefficient
- m̂t = corrected first moment
- v̂t = corrected second moment
What is Weight Decay?
Weight decay reduces the magnitude of weights to prevent overfitting.
Large Weights↓
Weight Decay
↓
Smaller Weights
↓
Better Generalization
Why is AdamW Better than Adam?
Adam:
- Applies L2 regularization indirectly.
AdamW:
- Applies weight decay separately.
This leads to:
- Better regularization
- Better generalization
- Improved performance
Example
Suppose a neural network begins to overfit.
AdamW:
- Penalizes large weights.
- Prevents the model from memorizing the training data.
- Improves performance on unseen data.
Advantages of AdamW
- Better generalization.
- Reduces overfitting.
- Adaptive learning rates.
- Stable convergence.
- Widely used in modern AI models.
Limitations of AdamW
- Requires tuning of weight decay.
- Slightly more computationally expensive.
- More hyperparameters than SGD.
Applications of AdamW
| Application | Usage |
|---|---|
| Transformers | Training |
| BERT | Fine-Tuning |
| GPT Models | Training |
| Computer Vision | Deep Networks |
| Large Language Models | Optimization |
Real-World Examples
- GPT Models
- BERT
- Vision Transformers (ViT)
- Language Translation Models
- Image Classification Models
Adam vs AdamW
| Feature | Adam | AdamW |
|---|---|---|
| Weight Decay | Indirect | Decoupled |
| Regularization | Moderate | Better |
| Generalization | Good | Better |
| Overfitting Control | Moderate | Excellent |
SGD vs AdamW
| Feature | SGD | AdamW |
|---|---|---|
| Adaptive Learning Rate | No | Yes |
| Weight Decay | Manual | Built-In |
| Large Models | Moderate | Excellent |
| Transformers | Rarely Used | Widely Used |
Common Hyperparameters
| Parameter | Typical Value |
|---|---|
| Learning Rate | 0.001 |
| β1 | 0.9 |
| β2 | 0.999 |
| Weight Decay | 0.01 |
When Should You Use AdamW?
Use AdamW when:
- Training Transformers.
- Fine-tuning LLMs.
- Working with large datasets.
- Overfitting is a concern.
- Better generalization is required.
Best Practices
- Start with weight decay = 0.01.
- Use learning rate scheduling.
- Monitor validation performance.
- Experiment with different regularization strengths.
Interview Tip
A common interview question is:
"Why was AdamW introduced?"
A strong answer is:
AdamW was introduced to separate weight decay from Adam's gradient updates, resulting in better regularization and improved generalization performance.
Conclusion
AdamW is one of the most important modern optimization algorithms. By combining Adam's adaptive learning rates with decoupled weight decay, it provides excellent convergence and better generalization, making it the preferred optimizer for Transformers and Large Language Models.