TExcelTAD
Home › Optimization › Variance Reduction

Variance Reduction

SGD's noise floor forces decreasing steps and a slow O(1/k)O(1/k) rate, even on problems where full gradient descent converges linearly. For finite sums f=1n∑ifif = \frac{1}{n}\sum_if_i 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 w~\tilde{\mathbf{w}} and its full gradient g~=∇f(w~)\tilde{\mathbf{g}} = \nabla f(\tilde{\mathbf{w}}), recomputed once per epoch. Each inner step uses

vk=∇fi(wk)−∇fi(w~)+g~\mathbf{v}_k = \nabla f_i(\mathbf{w}_k) - \nabla f_i(\tilde{\mathbf{w}}) + \tilde{\mathbf{g}}

It is still unbiased (E[∇fi(w~)]=g~\mathbb{E}[\nabla f_i(\tilde{\mathbf{w}})] = \tilde{\mathbf{g}}), but when wk\mathbf{w}_k and w~\tilde{\mathbf{w}} are both near w∗\mathbf{w}^* the first two terms nearly cancel, so the variance goes to zero as the method converges. No noise floor, so a constant step works.

python · try it yourself
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, gi\mathbf{g}_i, and their average gˉ\bar{\mathbf{g}}:

vk=∇fi(wk)−gi+gˉ,then update gi and gˉ\mathbf{v}_k = \nabla f_i(\mathbf{w}_k) - \mathbf{g}_i + \bar{\mathbf{g}}, \qquad \text{then update } \mathbf{g}_i \text{ and } \bar{\mathbf{g}}

The memory cost is one gradient per example. For linear models the gradient of example ii is a scalar times xi\mathbf{x}_i, so only one number per example needs storing.

python · try it yourself
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 nn gradients none linear, κ\kappa iterations per digit
SGD 1 gradient none O(1/k)O(1/k)
SVRG about 2–3 gradients on average one snapshot linear, about n+κn + \kappa 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 nn undermine the snapshot), but for convex finite sums they are the methods of choice.

In software

  • scikit-learn's LogisticRegression(solver="saga") and "sag", and Ridge(solver="saga"); the lightning and copt libraries implement SVRG and SAGA for linear models.

Exercises

Exercise 1: Unbiasedness

Show that the SVRG direction is unbiased.

Answer

Ei[∇fi(w)−∇fi(w~)]+g~=∇f(w)−g~+g~\mathbb{E}_i[\nabla f_i(\mathbf{w}) - \nabla f_i(\tilde{\mathbf{w}})] + \tilde{\mathbf{g}} = \nabla f(\mathbf{w}) - \tilde{\mathbf{g}} + \tilde{\mathbf{g}}.

Solution

Both stochastic terms have expectation equal to the corresponding full gradient, and the snapshot terms cancel, leaving ∇f(w)\nabla f(\mathbf{w}).

Exercise 2: Variance at the optimum

What is the variance of the SVRG direction when w=w~=w∗\mathbf{w} = \tilde{\mathbf{w}} = \mathbf{w}^*?

Answer

Zero.

Solution

The direction is ∇fi(w∗)−∇fi(w∗)+∇f(w∗)=0\nabla f_i(\mathbf{w}^*) - \nabla f_i(\mathbf{w}^*) + \nabla f(\mathbf{w}^*) = \mathbf{0} for every ii. Plain SGD's direction ∇fi(w∗)\nabla f_i(\mathbf{w}^*) is generally not zero, which is the source of its noise floor.

Exercise 3: Memory of SAGA (code)

For logistic regression with n=106n = 10^6 examples and d=1000d = 1000 features, compare the memory (in floats) of storing full per-example gradients with storing one scalar per example.

Answer

10910^9 versus 10610^6 floats.

Solution
python · try it yourself
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 ∇fi(w)=ℓ′(xiTw)xi\nabla f_i(\mathbf{w}) = \ell'(\mathbf{x}_i^T\mathbf{w})\mathbf{x}_i, so storing the scalar ℓ′\ell' suffices.

Test yourself

Why can SVRG use a constant step size and still converge exactly?

  1. Its gradient estimate's variance vanishes near the optimum
  2. It uses the Hessian
  3. It only works on quadratics
  4. It rounds the iterates

SAGA's extra memory is…

  1. none
  2. one stored gradient per example
  3. the full Hessian
  4. one snapshot only

On a strongly convex finite sum, variance-reduced methods converge…

  1. linearly
  2. like O(1/k)O(1/k)
  3. like O(1/k)O(1/\sqrt{k})
  4. not at all