Optimization for Data Science · Lecture 11
SGD: stationarity, PL, and variance reduction
Follow the lecture from approximate stationarity to fast convergence under PL, then build a gradient estimator whose variance shrinks near a snapshot.
1 SGD reaches approximate stationarity
A smooth nonconvex objective, with an unbiased noisy gradient. Track an individual run and compare the average gradient size across 256 runs with the guarantee from the lecture.
L = 1, f⋆ = 0, Var(g) = σ². Choose η = 1/√T and keep it fixed during the run.
A noisy path on a nonconvex function
The highlighted point is one run. Five faint points show other runs; the trail marks recent iterates.
Average squared gradient
The empirical curve averages pre-update points w₀,…,wk−1 and 256 independent runs. The dashed curve bounds the true expectation.
What the theorem controls
The objective has a 1-Lipschitz gradient and a finite infimum. Its noisy gradient has variance σ² at every point. It does not satisfy a global PL inequality: at w = π the gradient vanishes even though f(π) = 2 > f⋆.
The plotted statistic is an average over past iterates, not the gradient at the latest point. An independent uniform choice among w₀,…,wk−1 has the same expected statistic. A small gradient does not by itself establish global optimality.
Changing T starts a new run with a new fixed step. The step does not shrink as the animation advances. At the end of the run, the bound on the average gradient norm is √[2(f(w₀) − f⋆) + σ²] / T1/4. This is a worst-case upper bound; the empirical curve need not decay at exactly that rate.
The landscape uses the actual, unwrapped iterates. The chart has no average at k = 0. In logarithmic view, zero and values below 10−6 appear at the plotting floor; the readout retains their actual values. All horizontal axes are linear.
2 PL: fast progress, persistent noise
Two losses pull toward different points. Their average satisfies PL, with L = μ = 1. Compare the theorem with the actual expected error, and change the step size to trade fast initial progress for a smaller noise neighborhood.
SGD: wk+1 = (1 − ηk)wk − ηkskb, sk ∈ {−1, +1} with equal probability.
Constant: ηk = η. Decreasing: ηk = 1/(k + 1), k = 0, 1, 2, …
Parameters along individual runs
The highlighted path shows one SGD run. Lighter paths show five more runs.
Expected objective error
The SGD mean uses 128 independent runs. Black dashes show the exact expectation; teal dashes show the PL upper bound, not a prediction.
Here L = μ = 1, σ² = b², so the bound is (1 − η)kw₀²/2 + ηb²/2.
The decreasing schedule uses steps 1, ½, ⅓, …, independently of the constant-step slider. Its exact expected gap is b²/(2k) for k ≥ 1. The displayed PL bound applies only to constant-step SGD.
The calculations for both step-size choices
The oracle is g(w, s) = w + sb. Its variance is b² for every w, so this example satisfies the globally uniform variance assumption. The average objective has L = μ = 1.
For a constant step:
E[f(wk) − f⋆] = ½(1 − η)2kw₀² + ηb²/[2(2 − η)] · [1 − (1 − η)2k].
The limiting gap is ηb²/[2(2 − η)]. The general PL theorem gives an upper bound on a noise neighborhood; this is the exact expectation for this example.
With ηk = 1/(k + 1), the first update uses η₀ = 1. For k ≥ 1, the iterate is the negative average of the sampled offsets:
wk = −(b/k) Σj=0k−1 sj, E[f(wk) − f⋆] = b²/(2k).
The first update removes dependence on w₀ for this particular quadratic. At k = 0, all methods have gap w₀²/2; the 1/k formula applies only for k ≥ 1. The sampling variance stays b², but the smaller steps average its effect away. All horizontal axes in this demo are linear.
3 Why the corrected gradient has small variance
Now let the components have different curvatures. Compute the full gradient at a snapshot w̃. A sample estimates how much the gradient has changed since that reference point.
vs(w; w̃) = f′s(w) − f′s(w̃) + f′(w̃) = w + sr(w − w̃).
Raw component gradients
The two possible sampled gradients can disagree at w⋆ = 0.
Corrected gradients
The two corrected lines meet the full gradient at the snapshot.
Draw a sample to follow the same component through both gradient evaluations.
Raw sampling variance (rw + b)²: · Corrected second moment w² + Var(v):
The variance calculation, one inequality at a time
Centering subtracts |EAᵢ|² from E|Aᵢ|², where Aᵢ = f′ᵢ(w) − f′ᵢ(w̃). Component smoothness gives the second inequality. These values average both possible samples exactly.
Why sharing the sample matters
The same s appears in both component gradients. Their linear offsets sb cancel exactly. Moving b changes the raw gradients but leaves the corrected gradients unchanged.
Both estimators have mean w. At w = w̃, SVRG has zero variance, even when the gradient w is nonzero. Its second moment there is w². A stale snapshot can increase variance; it must be refreshed as optimization progresses.
For r > 0, raw variance grows with w and is not globally bounded. This experiment illustrates the control variate directly; the finite-sum analysis uses objective-dependent moment bounds instead of a uniform variance constant.
4 Next lecture: SVRG in action
Fit a synthetic regression dataset with 120 observations and two parameters. All methods start at the same point. SVRG pays for a full gradient at each snapshot, then reuses it for many corrected updates.
Paths on the quadratic
Contours are objective-error levels. Diamonds mark the averaged SVRG snapshots.
Objective convergence
Full gradients and snapshot refreshes are charged to the work axis.
| Method | Updates | Gradient calls | Objective gap | Estimator variance |
|---|
Algorithm, averaging, and comparison details
GD uses η = 1/Lf, where Lf is the smoothness of the average. SGD and SVRG use the selected fraction of 1/L, where L = maxᵢ(‖aᵢ‖² + λ). The two constants can differ substantially.
One GD step costs 120 component gradients; one SGD step costs one. This implementation evaluates both component gradients on every SVRG inner update, including the first. An epoch therefore costs 120 + 2m calls. At the first step, those two evaluations could be saved because the direction equals the stored full gradient.
At the end of an epoch, SVRG averages the pre-update points w₀,…,wm−1 and starts the next epoch at that average, as in the lecture algorithm. A snapshot can therefore change the current parameter without an additional gradient call. Diamond markers show these events.
The default axis compares equal computational work. Switching to updates compares equal numbers of parameter updates and hides their different costs. The trajectories are individual seeded runs, not expected-value guarantees. Selected parameters are for experimentation; Lecture 12 derives sufficient choices for the theorem.
Objective gaps, contours, and exact estimator variances are diagnostics calculated for visualization; their evaluations are excluded from the algorithm's gradient-call counts. The synthetic dataset is generated once and remains fixed when sampling is reset. No data or libraries are downloaded.