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 + ε)) − ηλW

where:

  • 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

ApplicationUsage
TransformersTraining
BERTFine-Tuning
GPT ModelsTraining
Computer VisionDeep Networks
Large Language ModelsOptimization

Real-World Examples

  • GPT Models
  • BERT
  • Vision Transformers (ViT)
  • Language Translation Models
  • Image Classification Models

Adam vs AdamW

FeatureAdamAdamW
Weight DecayIndirectDecoupled
RegularizationModerateBetter
GeneralizationGoodBetter
Overfitting ControlModerateExcellent

SGD vs AdamW

FeatureSGDAdamW
Adaptive Learning RateNoYes
Weight DecayManualBuilt-In
Large ModelsModerateExcellent
TransformersRarely UsedWidely Used

Common Hyperparameters

ParameterTypical Value
Learning Rate0.001
β10.9
β20.999
Weight Decay0.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.