tezvyn:

AdamW: Decoupling Weight Decay for Better Generalization

AI-drafted, machine-checkedSource: Wikipedia: Adam (optimizer)intermediate

AdamW fixes a flaw in the Adam optimizer by decoupling weight decay from the gradient update, improving model generalization. It's a go-to for training large networks like Transformers. The footgun is thinking it's the same as Adam with L2 regularization.

WHY IT EXISTS The popular Adam optimizer had a subtle flaw in how it handled L2 regularization, a common technique to prevent overfitting. When combined, the regularization effect was unintentionally weakened for weights with large historical gradients. AdamW (Adam with Weight Decay) was created to decouple these two concepts, leading to more effective regularization and better model performance.

THE MENTAL MODEL Think of training a model as steering a ship (the parameters) towards a destination (minimum loss). Adam is an advanced autopilot using momentum (past velocity) and an adaptive rudder (past terrain difficulty). Weight decay is like a constant drag force pulling the ship back towards a central, neutral position, preventing it from making extreme, overconfident turns. In the original Adam, this drag force was tangled up with the rudder's movements, making it less effective in rough waters. AdamW applies the drag directly to the ship's hull after the autopilot makes its steering adjustment, ensuring a consistent, predictable drag that helps the ship find a more generalizable path.

HOW IT WORKS Adam maintains moving averages of past gradients (momentum) and their squares (which scales the learning rate). In the original Adam, L2 regularization is added to the gradient before these averages are calculated. This means the regularization's strength is unintentionally scaled down for parameters with large gradient histories. AdamW fixes this by performing the update step using only the gradients of the loss function. Then, in a separate step, it applies weight decay by directly subtracting a small fraction of the current weight's value from the weight itself. This decouples the decay from the adaptive learning rate mechanism.

WHEN TO USE IT AdamW is the default choice for training large, modern neural networks, especially Transformers like BERT and GPT, and deep computer vision models. Its improved generalization makes it a go-to for tasks where overfitting is a major concern and state-of-the-art performance is required. Most modern deep learning frameworks, like PyTorch and TensorFlow, have it as a standard option.

WHEN NOT TO USE IT For simpler models or datasets where overfitting is not a significant problem, the benefits of AdamW over the original Adam with L2 regularization may be negligible. If you are trying to precisely replicate older research that used the original Adam, using AdamW could produce different, albeit often better, results.

ONE CANONICAL EXAMPLE Fine-tuning a large language model like BERT for a text classification task. The original BERT paper used a specific implementation of Adam with weight decay. However, the deep learning community quickly adopted AdamW as the superior method. Modern libraries like Hugging Face Transformers use AdamW as the default optimizer because it consistently leads to better fine-tuning stability and higher accuracy on downstream tasks.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.