TensorTau PathToAGI Training Deep Networks Level 07

Level 07 · Training Deep Networks · Chapter 103

Gradient Clipping

If one batch produces an enormous gradient, can we limit the update without throwing away its direction?

A common method clips the global gradient norm. Let all parameter gradients form one conceptual vector gg with norm ∥g∥\lVert g\rVert.

If the norm exceeds threshold cc:

g←gc∥g∥.g\leftarrow g\frac{c}{\lVert g\rVert}.

The direction stays the same; the magnitude is capped.

1. Example

If gradient norm is 100 and threshold is 5, multiply every gradient by 0.05. The new global norm is 5.

2. Why clipping helps

Recurrent networks and unstable deep models can occasionally produce exploding gradients. One extreme step may destroy useful parameters or create NaNs. Clipping is a safety rail.

3. Clipping is not a cure for every instability

If gradients are always enormous, inspect learning rate, initialization, normalization, loss scaling and data. Constant clipping can hide a deeper problem.

4. PyTorch

loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()

Clip after gradients are computed and before the optimizer update.

What to Remember

Norm clipping caps dangerous gradient magnitude while preserving direction, but persistent clipping is a diagnostic signal.

Next → Mixed Precision: once training is numerically stable, can we use lower-precision arithmetic to make it faster and smaller without losing useful accuracy?

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

  1. Explain: Norm clipping versus clipping each gradient element independently.
  2. Calculate: Rescale a gradient vector of norm 20 to threshold 5.
  3. Predict: Where must clipping occur relative to backward() and step()?
  4. Break: Train an unstable RNN/MLP without clipping and inspect gradient spikes.
  5. Build: Implement global norm clipping.
  6. Investigate: Log fraction of steps clipped across several thresholds.