Variance Reduction
SGD's noise floor forces decreasing steps and a slow rate, even on problems where full gradient descent converges linearly. For finite sums there is a way out: occasionally compute something exact and use it to cancel the noise. Variance-reduced methods (SVRG, SAGA, SAG) keep SGD's cheap steps and recover a linear rate with a constant step.
SVRG
Stochastic variance-reduced gradient keeps a snapshot and its full gradient , recomputed once per epoch. Each inner step uses
It is still unbiased (), but when and are both near the first two terms nearly cancel, so the variance goes to zero as the method converges. No noise floor, so a constant step works.
import numpy as np
rng = np.random.default_rng(0)
n, d = 1000, 20
X = rng.normal(size=(n, d)); y = X @ rng.normal(size=d) + rng.normal(size=n)
lam = 0.1
f = lambda w: 0.5 * np.mean((X @ w - y)**2) + 0.5 * lam * w @ w
gi = lambda w, i: X[i] * (X[i] @ w - y[i]) + lam * w
full = lambda w: X.T @ (X @ w - y) / n + lam * w
w_star = np.linalg.solve(X.T @ X / n + lam * np.eye(d), X.T @ y / n)
f_star = f(w_star)
L_max = np.max(np.sum(X**2, axis=1)) + lam
def sgd(epochs, t):
w = np.zeros(d)
for _ in range(epochs * n):
i = rng.integers(n); w -= t * gi(w, i)
return f(w) - f_star
def svrg(epochs, t):
w = np.zeros(d)
for _ in range(epochs // 2): # each outer loop costs about 2 passes
w_snap, g_snap = w.copy(), full(w)
for _ in range(n):
i = rng.integers(n)
w -= t * (gi(w, i) - gi(w_snap, i) + g_snap)
return f(w) - f_star
for epochs in (4, 8, 12):
print(f"{epochs:2d} passes: SGD gap {sgd(epochs, 0.2 / L_max):.1e} SVRG gap {svrg(epochs, 0.2 / L_max):.1e}") 4 passes: SGD gap 1.2e-02 SVRG gap 2.2e-02
8 passes: SGD gap 2.5e-02 SVRG gap 3.7e-05
12 passes: SGD gap 3.4e-02 SVRG gap 4.3e-08
With the same step size and the same number of passes over the data, SGD stalls at its noise floor while SVRG keeps converging geometrically.
SAGA
SAGA avoids the periodic full pass by storing the last gradient seen for every example, , and their average :
The memory cost is one gradient per example. For linear models the gradient of example is a scalar times , so only one number per example needs storing.
import numpy as np
rng = np.random.default_rng(0)
n, d = 1000, 20
X = rng.normal(size=(n, d)); y = X @ rng.normal(size=d) + rng.normal(size=n)
lam = 0.1
f = lambda w: 0.5 * np.mean((X @ w - y)**2) + 0.5 * lam * w @ w
w_star = np.linalg.solve(X.T @ X / n + lam * np.eye(d), X.T @ y / n)
t = 0.2 / (np.max(np.sum(X**2, axis=1)) + lam)
w = np.zeros(d)
scal = X @ w - y # stored residual per example: grad_i = scal_i * x_i
g_bar = X.T @ scal / n
for k in range(1, 20 * n + 1):
i = rng.integers(n)
s_new = X[i] @ w - y[i]
v = (s_new - scal[i]) * X[i] + g_bar + lam * w
g_bar += (s_new - scal[i]) * X[i] / n
scal[i] = s_new
w -= t * v
if k % (5 * n) == 0:
print(f"{k // n:2d} passes: SAGA gap {f(w) - f(w_star):.1e}") 5 passes: SAGA gap 9.3e-03
10 passes: SAGA gap 2.2e-05
15 passes: SAGA gap 6.3e-07
20 passes: SAGA gap 2.6e-09
Where variance reduction fits
| Method | Cost per step | Extra memory | Rate (strongly convex) |
|---|---|---|---|
| gradient descent | gradients | none | linear, iterations per digit |
| SGD | 1 gradient | none | |
| SVRG | about 2–3 gradients on average | one snapshot | linear, about gradients per digit |
| SAGA / SAG | 1 gradient | one gradient per example | linear |
For deep networks these methods have not displaced SGD and Adam (data augmentation and huge undermine the snapshot), but for convex finite sums they are the methods of choice.
In software
- scikit-learn's
LogisticRegression(solver="saga")and"sag", andRidge(solver="saga"); thelightningandcoptlibraries implement SVRG and SAGA for linear models.
Exercises
Exercise 1: Unbiasedness
Show that the SVRG direction is unbiased.
Answer
.
Solution
Both stochastic terms have expectation equal to the corresponding full gradient, and the snapshot terms cancel, leaving .
Exercise 2: Variance at the optimum
What is the variance of the SVRG direction when ?
Answer
Zero.
Solution
The direction is for every . Plain SGD's direction is generally not zero, which is the source of its noise floor.
Exercise 3: Memory of SAGA (code)
For logistic regression with examples and features, compare the memory (in floats) of storing full per-example gradients with storing one scalar per example.
Answer
versus floats.
Solution
n, d = 10**6, 1000
print(f"full gradients: {n * d:.0e} floats ({n * d * 8 / 1e9:.0f} GB); scalars: {n:.0e} floats ({n * 8 / 1e6:.0f} MB)")full gradients: 1e+09 floats (8 GB); scalars: 1e+06 floats (8 MB)
For linear models , so storing the scalar suffices.
Test yourself
Why can SVRG use a constant step size and still converge exactly?
- Its gradient estimate's variance vanishes near the optimum
- It uses the Hessian
- It only works on quadratics
- It rounds the iterates
SAGA's extra memory is…
- none
- one stored gradient per example
- the full Hessian
- one snapshot only
On a strongly convex finite sum, variance-reduced methods converge…
- linearly
- like
- like
- not at all