Step Sizes and Convergence of SGD
Gradient noise forces a trade-off: large steps make fast early progress but leave a large noise ball; small steps are accurate but slow. Step-size schedules get both: start large, then decay. This lesson gives the theory behind the schedules and the averaging trick that makes SGD provably optimal on convex problems.
The two regimes
For a -strongly convex, -smooth with gradient variance bounded by , SGD with a constant step satisfies roughly
The first term decays geometrically (like gradient descent); the second is the noise floor, proportional to . Halving the step halves the floor but doubles the time to reach it.
Classical decreasing steps
To converge exactly, the steps must shrink, but not too fast. The Robbins-Monro conditions are
The first lets the iterates travel any distance; the second kills the noise. satisfies both; for strongly convex with large enough it gives , the best possible rate for stochastic first-order methods. For general convex , with averaging gives in the objective.
import numpy as np
rng = np.random.default_rng(0)
n = 5000
X = rng.normal(size=(n, 5)); w_true = rng.normal(size=5); y = X @ w_true + rng.normal(size=n)
w_star = np.linalg.lstsq(X, y, rcond=None)[0]
def run(schedule, iters=50000):
w = np.zeros(5)
for k in range(1, iters + 1):
i = rng.integers(n)
w -= schedule(k) * X[i] * (X[i] @ w - y[i])
return np.linalg.norm(w - w_star)
schedules = {"constant 0.05": lambda k: 0.05, "constant 0.005": lambda k: 0.005,
"1/k (c = 1)": lambda k: 1.0 / (k + 20), "step decay": lambda k: 0.05 * 0.1 ** (k // 15000)}
for name, s in schedules.items():
print(f"{name:15s}: distance to w* after 50,000 steps {run(s):.4f}")constant 0.05 : distance to w* after 50,000 steps 0.3242
constant 0.005 : distance to w* after 50,000 steps 0.0471
1/k (c = 1) : distance to w* after 50,000 steps 0.0076
step decay : distance to w* after 50,000 steps 0.0218
Iterate averaging
Polyak-Ruppert averaging returns the average instead of the last iterate. With a larger, slowly decaying step ( or even constant on least squares), the average achieves the optimal rate and is robust to the choice of constant. The noise in individual iterates averages out.
import numpy as np
rng = np.random.default_rng(1)
n = 5000
X = rng.normal(size=(n, 5)); w_true = rng.normal(size=5); y = X @ w_true + rng.normal(size=n)
w_star = np.linalg.lstsq(X, y, rcond=None)[0]
w = np.zeros(5); w_avg = np.zeros(5)
for k in range(1, 50001):
i = rng.integers(n)
w -= 0.02 * X[i] * (X[i] @ w - y[i])
if k > 5000: # average after a burn-in
w_avg += (w - w_avg) / (k - 5000)
print("last iterate distance: ", round(float(np.linalg.norm(w - w_star)), 4))
print("averaged iterate distance:", round(float(np.linalg.norm(w_avg - w_star)), 4))last iterate distance: 0.2375
averaged iterate distance: 0.0046
Schedules used in practice
| Schedule | Formula | Used in |
|---|---|---|
| step decay | multiply by 0.1 every few epochs | classic ImageNet training |
| or | scikit-learn SGDClassifier(learning_rate="optimal") |
|
| cosine | modern deep learning | |
| warm-up then decay | linear increase for a few thousand steps | transformers |
Warm-up and cosine schedules are discussed in Learning-Rate Schedules and Clipping.
In software
torch.optim.lr_scheduler(StepLR, CosineAnnealingLR, OneCycleLR),optaxschedules in JAX;torch.optim.swa_utils.AveragedModelimplements iterate (weight) averaging, also known as SWA or EMA of weights.- scikit-learn's
SGDRegressor(average=True)turns on Polyak-Ruppert averaging.
Exercises
Exercise 1: Robbins-Monro
Which schedules satisfy both Robbins-Monro conditions? (a) ; (b) ; (c) ; (d) .
Answer
Only (b).
Solution
(a) has . (c) has : the iterates can only travel a bounded distance. (d) has (it still works with averaging, but not for the last iterate in the classical theorem).
Exercise 2: Noise floor
With a constant step, the noise floor is proportional to . If gives an error floor of 0.04, estimate the floor for .
Answer
About 0.01.
Solution
The floor term is linear in ; dividing by 4 divides the floor by 4.
Exercise 3: Cosine schedule (code)
Print the cosine schedule with , , at .
Answer
0.1, about 0.0854, 0.05, about 0.0146, 0.
Solution
import numpy as np
print([round(float(0.5 * 0.1 * (1 + np.cos(np.pi * k / 100))), 4) for k in (0, 25, 50, 75, 100)])[0.1, 0.0854, 0.05, 0.0146, 0.0]
Test yourself
The Robbins-Monro conditions on step sizes are…
- and
- constant
Polyak-Ruppert averaging returns…
- the last iterate
- the average of the iterates
- the best iterate
- the median gradient
With a constant step, halving in SGD…
- halves the noise floor but slows the initial convergence
- doubles the noise floor
- has no effect
- guarantees exact convergence