ML Systems Lab Open interactive version →
Advanced 45 min read clippingweight-decaydropoutlabel-smoothingregularization

Gradient Clipping and Regularization

Bounding gradient explosions, weight decay vs L2, dropout, and connecting optimizer choices to generalization.

A big neural network has enough raw capacity to simply *memorise* its training data — to store the answer key rather than learn the pattern. Left unchecked, it will do exactly that: acing training, flopping on anything new. This lesson is about the toolkit that stops it, plus one safety valve for a different problem entirely.


The safety valve: gradient clipping

First, the odd one out. Gradient clipping is not really about memorisation — it is a seatbelt. As we saw with exploding gradients, sequence models can occasionally produce a gradient thousands of times bigger than usual, and a single such step can wreck the whole model. Clipping simply says: if the gradient's overall size exceeds a cap, shrink it back to the cap while keeping its *direction*. It prevents catastrophe; it does nothing for generalisation. (Clip by the whole-vector size, not each component separately, or you bend the direction — norm-clip at 1.0 is standard for language models.)


The main tool: weight decay

The workhorse regulariser is weight decay: at every step, gently shrink every weight toward zero by a small fixed fraction. This is the same "keep the weights small so the model stays simple" idea from the regularisation lesson — small weights mean a smoother, less memorising model.

There is one subtlety that trips up nearly everyone. For plain SGD, weight decay is *exactly* the same as adding an L2 penalty to the loss. For the Adam optimizer, it is *not*. Adam rescales each parameter's update by how big that parameter's gradients have been; fold the L2 penalty into the loss and it gets rescaled too, so parameters end up regularised *unevenly* — and backwards from what you want. The fix is AdamW, which applies the weight decay *directly* to the weights, off to the side of Adam's rescaling, so every weight shrinks by the same fraction as intended. This one change measurably improves generalisation, which is why AdamW — not Adam — trains modern language models. If you take one practical thing from this lesson: with Adam, use AdamW, never L2-in-the-loss.


Two more, for two more failure modes

Dropout attacks over-reliance. During training it randomly switches off a fraction of neurons on each step, so the network can never lean on any single neuron or pathway — it is forced to build redundant, backup representations. At test time every neuron is back on. It is heavy for plain fully-connected layers (drop about half) and lighter in transformers (drop 10–30%), where attention already spreads things out.

Label smoothing attacks over-confidence. Normally the training targets are hard 0s and 1s, which push the model to become *infinitely* confident to drive the loss to zero — and wildly overconfident models are poorly calibrated. Label smoothing softens the targets slightly (say 0.9 instead of 1.0), making perfect confidence impossible and keeping the model's probabilities honest.


They are a system, not a checklist

The catch is that these tools interact — with each other and with the optimizer. Small-batch SGD already injects noise that regularises for free, so a small model trained that way may need little else; large-batch Adam has almost no built-in noise and leans hard on explicit weight decay and dropout to compensate. Pile on every regulariser at full strength and you can *over*-regularise a small model into underfitting. So there is a budget: tune one knob at a time, start with weight decay (the biggest lever), and match the amount to your model size and dataset size rather than reaching for all of them at once.

Key points

Takeaway

Regularization and optimizer are a coupled system. Gradient clipping is not regularization — it is catastrophe prevention for sequence models. Weight decay is the primary regularizer but works only as intended in AdamW, not in Adam with L2 in the loss. Dropout and label smoothing address separate failure modes, but all of these tools interact with each other and with the optimizer, so they form a budget to tune together, not an independent checklist to max out. Get the pairings wrong — L2 in Adam, no clipping for RNNs — and regularization either does nothing or quietly corrupts training.

Recap

Check your understanding

Q1. Adam with L2 (λ=0.01 in the loss) reaches the same training loss as AdamW (weight_decay=0.01) but noticeably worse test perplexity. Why?

Q2. Why is norm clipping preferred over value clipping for transformers? Give an example where value clipping bends the gradient direction.

Q3. Explain how label smoothing works, why it improves calibration, and when you would not use it.

Q4. A 6-layer transformer with AdamW (wd=0.1), dropout 0.1, label smoothing 0.1, and clip 1.0 still has validation loss 20% above training. Which two of the following are the right next steps, in the right spirit?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →