0.8 — Convex functions, Jensen, and the gradient as steepest direction
Goal: Recognise convex functions through three equivalent lenses, apply Jensen's inequality, and prove that the gradient points in the direction of steepest ascent.
1. Why this session exists
Training a model is, in the end, solving an optimisation problem
θ∈RpminL(θ).
Two questions decide whether that problem is well-behaved:
Is every local minimum a global minimum? That is decided by convexity.
Why does moving along −∇L help at all? That is decided by a geometric fact we'll prove in §6: the gradient is the direction of steepest ascent.
Along the way we pick up Jensen's[2] inequality, the workhorse behind entropy bounds, the ELBO we'll derive in Chapter 9, and several AM–GM-style tricks that will reappear without warning.[1]
2. Convex sets, convex functions
A set C⊆Rn is convex if it contains every line segment between two of its points:
x,y∈C,θ∈[0,1]⟹θx+(1−θ)y∈C.
A function f:C→R defined on a convex set C is convex if
f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)for all x,y∈C,θ∈[0,1].
It is strictly convex if the inequality is strict for x=y and θ∈(0,1).
Geometrically: the chord between (x,f(x)) and (y,f(y)) lies above the graph of f.
A first stock of examples to keep in mind:
f(x)=x2 on R, strictly convex.
f(x)=∣x∣ on R, convex but not strictly so.
f(x)=ex on R, strictly convex.
f(x)=−lnx on (0,∞), strictly convex.
f(x)=∥x∥ for any norm, convex (triangle inequality plus positive homogeneity).
f(x)=x⊤Ax on Rn with A⪰0, convex.
3. First-order condition (the "linear lower bound" picture)
If f is differentiable on an open convex set C, then f is convex on C if and only if its graph lies above every tangent hyperplane:
f(y)≥f(x)+∇f(x)⊤(y−x)for all x,y∈C.
Proof (⇒). Assume f is convex. Fix x,y∈C and θ∈(0,1], and rewrite the convexity inequality at θy+(1−θ)x=x+θ(y−x):
f(x+θ(y−x))≤(1−θ)f(x)+θf(y).
Subtract f(x) from both sides and divide by θ>0:
θf(x+θ(y−x))−f(x)≤f(y)−f(x).
Let θ→0+. The left-hand side is, by definition, the directional derivative of f at x in direction (y−x), which equals ∇f(x)⊤(y−x). Hence
∇f(x)⊤(y−x)≤f(y)−f(x),
i.e. f(y)≥f(x)+∇f(x)⊤(y−x), as claimed.
Proof (⇐). Assume the linear lower bound holds at every base point. Pick x,y∈C, θ∈[0,1], and let z=θx+(1−θ)y. Applying the lower bound at z once for x and once for y:
f(x)f(y)≥f(z)+∇f(z)⊤(x−z),≥f(z)+∇f(z)⊤(y−z).
Multiply the first by θ, the second by (1−θ), and add:
θf(x)+(1−θ)f(y)≥f(z)+∇f(z)⊤[θ(x−z)+(1−θ)(y−z)].
The bracketed vector is θx+(1−θ)y−z=0. So
θf(x)+(1−θ)f(y)≥f(z),
which is the convexity inequality. ■
Immediate consequence. If ∇f(x∗)=0 and f is convex, then for every y∈C,
f(y)≥f(x∗)+0=f(x∗).
That is, x∗ is a global minimum. Stationarity ⇒ global optimality for convex objectives. This is the single most important reason convex problems are loved in optimisation.
4. Second-order condition (Hessian ≽ 0)
If f is twice continuously differentiable on an open convex set C, then
f convex on C⟺∇2f(x)⪰0 for all x∈C.
Here "⪰0" means positive semidefinite: d⊤∇2f(x)d≥0 for every direction d∈Rn. Strict convexity follows from ∇2f(x)≻0 (positive definite), but the converse is not exact (e.g. f(x)=x4 is strictly convex on R even though f′′(0)=0).
Sketch. Restrict f to a line. Define g(t):=f(x+td) for fixed x∈C and d∈Rn. By the chain rule from session 0.3,
g′(t)=∇f(x+td)⊤d,g′′(t)=d⊤∇2f(x+td)d.
Convexity of f on C is equivalent to convexity of g on every line segment lying in C, and convexity of a smooth one-dimensional function is equivalent to g′′≥0. Quantifying over all directions d gives ∇2f⪰0.
Worked example — linear regression loss. Let
L(θ)=21∥Xθ−y∥2.
Then
∇L(θ)=X⊤(Xθ−y),∇2L(θ)=X⊤X.
The Gram matrix X⊤X is always ⪰0 because d⊤X⊤Xd=∥Xd∥2≥0. So MSE regression is convex in θ. If X has full column rank, X⊤X≻0 and the problem is strictly convex with a unique minimiser given by the normal equations θ∗=(X⊤X)−1X⊤y.
5. Jensen's inequality
Finite form. If f is convex, θi≥0 with ∑i=1nθi=1, then
f(i=1∑nθixi)≤i=1∑nθif(xi).
Proof (induction on n). The base case n=2 is the definition of convexity. Suppose the inequality holds for n−1 points and consider n points with weights θ1,…,θn. Without loss of generality θn<1; set s=1−θn>0. Then
i=1∑nθixi=s⋅=:x~i=1∑n−1sθixi+θnxn,
and the weights θi/s are nonnegative summing to 1. Apply convexity at the two points x~ and xn with weights s and θn:
Expectation form. If X is a random variable taking values in C with E∥X∥<∞, and f is convex on C, then
f(E[X])≤E[f(X)].
For discrete X this is the finite form with θi=P(X=xi). The continuous case follows by approximation; we won't prove the measure-theoretic version here but we will use it freely.
Tiny worked use — Gibbs' inequality (revisit of 0.6). For distributions p,q on a finite set X,
KL(p∥q)=x∈X∑p(x)lnq(x)p(x)≥0.
Because −ln is convex, Jensen applied with weights p(x) on points q(x)/p(x) gives
Strict convexity of −ln further gives equality iff q(x)/p(x) is constant on the support of p, i.e. iff p=q. This is the inequality we already used in 0.6.
6. The gradient is the direction of steepest ascent
Pick a base point x∈Rn with f differentiable there, and a unit vector u∈Rn (so ∥u∥=1 in the Euclidean norm). The directional derivative of f at x in direction u is
Duf(x):=t→0limtf(x+tu)−f(x).
Apply the chain rule from 0.3 to g(t)=f(x+tu):
Duf(x)=g′(0)=∇f(x)⊤u.
(We are using the numerator convention fixed in 0.2: ∂f/∂x is a 1×n row, and ∇f:=(∂f/∂x)⊤ is its column transpose.)
We want the direction u∗ that maximises the instantaneous increase of f:
u∗=arg∥u∥=1max∇f(x)⊤u.
Cauchy–Schwarz gives, for any unit u,
∇f(x)⊤u≤∥∇f(x)∥⋅∥u∥=∥∇f(x)∥,
with equality if and only if u is the positive unit multiple of ∇f(x). Therefore
u∗=∥∇f(x)∥∇f(x),∥u∥=1maxDuf(x)=∥∇f(x)∥.
So the gradient points in the direction of steepest ascent, and its magnitude is the rate of ascent in that direction. Equivalently, −∇f(x) is the direction of steepest descent, which is the move taken at each step of gradient descent:
θk+1=θk−η∇L(θk).
7. What this buys us going forward
Three takeaways to carry into the rest of the course.
Convex losses are exceptional, but the ones we meet first are convex. Linear regression with MSE (§4) and logistic regression in the logits (Chapter 1) both have convex losses; stationarity therefore implies global optimality.
Neural networks are non-convex in their parameters as soon as we compose nonlinearities. Stationarity tells us nothing about global optimality. The pragmatic reason gradient descent still works is the subject of Chapter 3 (over-parameterisation, implicit bias, neural-tangent-kernel intuition).
Jensen is everywhere. Already used implicitly in 0.6 for KL≥0; we'll use it again to derive the ELBO in Chapter 9 (variational inference and the variational autoencoder), and once more in 0.10 to bound the log-sum-exp shift.
Self-test
(a) Show that f(x)=max(0,x) (the ReLU) is convex on R. Where does the first-order condition of §3 fail to apply, and why is that fine?
(b) Let X be a positive random variable with E[X]=1. Use Jensen to prove E[lnX]≤0, and identify the case of equality.
(c) For a positive-definite quadratic f(x)=21x⊤Ax−b⊤x with A≻0, compute ∇f and ∇2f, and show the unique minimiser is x∗=A−1b. From §6, what does steepest descent at x=x∗ look like? Why does −∇f(x) generally not point at x∗?
(d) Suppose f,g:C→R are both convex on a convex set C, and α,β≥0. Prove that αf+βg is convex. (This is the property that lets us add an L2 regulariser λ∥θ∥2 to any convex loss and stay convex.)
References
[1]
Boyd, S., & Vandenberghe, L. (2004). Convex Optimization. Cambridge University Press. — Used in 0.8 (convex functions, Jensen, first/second-order conditions). stanford.edu/~boyd/cvxbook
Jensen, J. L. W. V. (1906). "Sur les fonctions convexes et les inégalités entre les valeurs moyennes". Acta Math., 30, 175–193. — Jensen's inequality (0.8).