Bias and Convergence in Gradient-Clipped SGD
Intro
Sometimes a gradient is really big and the step you take from it is way too large. Gradient clipping fixes this by putting a cap on the step. The gradient keeps its direction, and if it's too long it just gets shortened.
The catch is that SGD uses random gradients, and clipping shrinks some of them more than others. That changes their average. The average can even point the opposite way, so a small step can still go uphill [1].
Clipping an exact gradient
I'll use Euclidean norm clipping with a threshold \(c>0\). It scales the whole vector down, and anything already inside the ball of radius \(c\) stays the same.
The update is \(\theta^+=\theta-\eta C_c(g)\), where \(\eta>0\) is the learning rate. Say \(g=\nabla f(\theta)\) is exact for now. If the gradient isn't zero, we can write \(C_c(g)=\alpha g\) with \(0<\alpha\leq1\). Then
So it's still a descent direction. If \(f\) has an \(L\)-Lipschitz gradient, the descent lemma gives
As long as \(0<\eta\alpha<2/L\), the objective goes down. So with an exact gradient, clipping is basically the same as using a smaller learning rate for that step.
A random gradient is different
Now let \(g\) be an unbiased stochastic gradient, so \(\mathbb E[g\mid\theta]=\nabla f(\theta)\). I'm holding \(\theta\) fixed in the expectations below. Clipping gives each draw a weight \(w(g)=\min(1,c/\|g\|)\), with \(w(0)=1\). The expected clipped gradient is
The weight depends on the gradient it's multiplying. Big gradients get cut down more. If the big ones mostly point in one direction, clipping shifts the balance between directions. So the bias depends on the whole distribution of gradients and not only the mean [2].
A convex example that goes uphill
Here's a one-dimensional example.
The full objective is \(f(\theta)=\mathbb E[f_\xi(\theta)]=\tfrac12\theta^2-\theta\), and the minimizer is \(\theta^*=1\). The sample gradient is \(g=\theta+\xi\). At \(\theta=0\) with \(c=1\), we get
SGD subtracts the gradient. Without clipping, the expected step goes right, toward the minimizer. With clipping it goes left. The rare negative gradients are big enough to beat the common positive ones, and clipping takes away that size advantage.
We can check this with the loss too. \(C_1(g)\) is either \(-1\) or \(1\), and the next parameter is \(\theta^+=-\eta C_1(g)\). So
For any positive learning rate, the expected objective goes up. This is an exact calculation, so there's no neural network or approximation in it.
Why a smaller learning rate doesn't fix it
A smaller step size changes how far you move, but the expected direction stays the same. This example also has a point where the expected clipped update is zero even though the true gradient isn't.
For \(-2<\theta<0\), the sample gradient \(\theta+1\) isn't clipped, and \(\theta-19\) gets clipped to \(-1\).
This is zero at \(\theta=-8/9\), but the real minimizer is still \(1\). Making \(\eta\) smaller doesn't move that zero. It also doesn't mean a random run will stop there, since each update is still noisy.
More generally, clipped SGD can have a convergence error that stays even when you shrink the learning rate, under the usual bounded-variance assumptions [1].
Averaging before clipping
There are two ways to use a batch of gradients.
If the samples have the same distribution, the second one has expectation \(\mathbb E[C_c(g)]\) no matter what the batch size is. A bigger batch lowers the variance, but the bias from clipping is still there.
The first one acts differently. Say the samples are independent and unbiased, with \(\mathbb E\|g_i-\mu\|^2\leq\sigma^2\), where \(\mu=\nabla f(\theta)\), and let \(\bar g_B\) be their average. Euclidean clipping is a projection onto a ball, so it can't make two vectors farther apart. That gives
As the batch gets bigger, this gets closer to clipping the exact gradient. The step can still get shortened, but the extra error from clipping noisy samples goes away. Batch size and gradient alignment are also looked at in [3].
In the example above, averaging ten independent samples before clipping gives an expected clipped gradient of \(2(0.9)^{10}-1\approx-0.303\). Now the sign matches the true gradient. If you clipped each sample first, you'd still get \(0.8\).
In practice
Clipping keeps the SGD step norm at most \(\eta c\). That helps when an occasional gradient would otherwise cause a huge update. There are also convergence results showing it can help when the smoothness assumption lets curvature grow with the gradient norm [4].
So the threshold is a tradeoff between that protection and the bias from shrinking samples unevenly. With a fixed threshold, a bigger batch before clipping can help under the assumptions above. There isn't one threshold that works best for every gradient distribution.
The same math applies to an unbiased stochastic policy gradient if you take a plain gradient step. With Adam, clipping limits the gradient going into the optimizer, not the final update after Adam's scaling. PPO's likelihood-ratio clipping is a different thing from gradient norm clipping.
Summary
Clipping an exact gradient keeps it a descent direction. Clipping a random gradient can change the mean enough to flip it, or make a zero in the wrong place. A smaller learning rate doesn't get rid of that bias. Averaging independent samples before clipping can shrink it, but averaging samples that were already clipped keeps the same expected bias.
References
- Anastasia Koloskova, Hadrien Hendrikx, and Sebastian U. Stich. Revisiting Gradient Clipping (Stochastic Bias and Tight Convergence Guarantees). ICML, 2023.
- Xiangyi Chen, Steven Z. Wu, and Mingyi Hong. Understanding Gradient Clipping in Private SGD (A Geometric Perspective). NeurIPS, 2020.
- Jiang Qian, Yuren Wu, Bojin Zhuang, Shaojun Wang, and Jing Xiao. Understanding Gradient Clipping in Incremental Gradient Methods. AISTATS, 2021.
- Jingzhao Zhang, Tianxing He, Suvrit Sra, and Ali Jadbabaie. Why Gradient Clipping Accelerates Training (A Theoretical Justification for Adaptivity). ICLR, 2020.