AI / ML Theory
Chapter 0 · 28 min

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

min⁡θ∈RpL(θ).\min_{\theta \in \mathbb{R}^p} L(\theta).

Two questions decide whether that problem is well-behaved:

  1. Is every local minimum a global minimum? That is decided by convexity.
  2. Why does moving along −∇L-\nabla 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⊆RnC \subseteq \mathbb{R}^n is convex if it contains every line segment between two of its points:

x,y∈C,θ∈[0,1]  ⟹  θx+(1−θ)y∈C.x, y \in C, \quad \theta \in [0,1] \implies \theta x + (1-\theta) y \in C.

A function f:C→Rf : C \to \mathbb{R} defined on a convex set CC is convex if

f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)for all x,y∈C, θ∈[0,1].f\bigl(\theta x + (1-\theta) y\bigr) \le \theta f(x) + (1-\theta) f(y) \quad \text{for all } x, y \in C,\ \theta \in [0,1].

It is strictly convex if the inequality is strict for x≠yx \ne y and θ∈(0,1)\theta \in (0,1).

Geometrically: the chord between (x,f(x))\bigl(x, f(x)\bigr) and (y,f(y))\bigl(y, f(y)\bigr) lies above the graph of ff.

A first stock of examples to keep in mind:

  • f(x)=x2f(x) = x^2 on R\mathbb{R}, strictly convex.
  • f(x)=∣x∣f(x) = |x| on R\mathbb{R}, convex but not strictly so.
  • f(x)=exf(x) = e^x on R\mathbb{R}, strictly convex.
  • f(x)=−ln⁡xf(x) = -\ln x on (0,∞)(0, \infty), strictly convex.
  • f(x)=∥x∥f(x) = \lVert x \rVert for any norm, convex (triangle inequality plus positive homogeneity).
  • f(x)=x⊤Axf(x) = x^\top A x on Rn\mathbb{R}^n with A⪰0A \succeq 0, convex.

3. First-order condition (the "linear lower bound" picture)

If ff is differentiable on an open convex set CC, then ff is convex on CC if and only if its graph lies above every tangent hyperplane:

 f(y)≥f(x)+∇f(x)⊤(y−x)for all x,y∈C. \boxed{\ f(y) \ge f(x) + \nabla f(x)^\top (y - x) \quad \text{for all } x, y \in C.\ }

Proof (⇒). Assume ff is convex. Fix x,y∈Cx, y \in C and θ∈(0,1]\theta \in (0,1], and rewrite the convexity inequality at θy+(1−θ)x=x+θ(y−x)\theta y + (1-\theta) x = x + \theta(y-x):

f(x+θ(y−x))≤(1−θ)f(x)+θf(y).f\bigl(x + \theta(y - x)\bigr) \le (1-\theta) f(x) + \theta f(y).

Subtract f(x)f(x) from both sides and divide by θ>0\theta > 0:

f(x+θ(y−x))−f(x)θ≤f(y)−f(x).\frac{f\bigl(x + \theta(y-x)\bigr) - f(x)}{\theta} \le f(y) - f(x).

Let θ→0+\theta \to 0^+. The left-hand side is, by definition, the directional derivative of ff at xx in direction (y−x)(y-x), which equals ∇f(x)⊤(y−x)\nabla f(x)^\top (y-x). Hence

∇f(x)⊤(y−x)≤f(y)−f(x),\nabla f(x)^\top (y-x) \le f(y) - f(x),

i.e. f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top (y-x), as claimed.

Proof (⇐). Assume the linear lower bound holds at every base point. Pick x,y∈Cx, y \in C, θ∈[0,1]\theta \in [0,1], and let z=θx+(1−θ)yz = \theta x + (1-\theta) y. Applying the lower bound at zz once for xx and once for yy:

f(x)≥f(z)+∇f(z)⊤(x−z),f(y)≥f(z)+∇f(z)⊤(y−z).\begin{aligned} f(x) &\ge f(z) + \nabla f(z)^\top (x - z), \\ f(y) &\ge f(z) + \nabla f(z)^\top (y - z). \end{aligned}

Multiply the first by θ\theta, the second by (1−θ)(1-\theta), and add:

θf(x)+(1−θ)f(y)≥f(z)+∇f(z)⊤[θ(x−z)+(1−θ)(y−z)].\theta f(x) + (1-\theta) f(y) \ge f(z) + \nabla f(z)^\top \bigl[\theta(x - z) + (1-\theta)(y - z)\bigr].

The bracketed vector is θx+(1−θ)y−z=0\theta x + (1-\theta) y - z = 0. So

θf(x)+(1−θ)f(y)≥f(z),\theta f(x) + (1-\theta) f(y) \ge f(z),

which is the convexity inequality. ■\blacksquare

Immediate consequence. If ∇f(x∗)=0\nabla f(x^*) = 0 and ff is convex, then for every y∈Cy \in C,

f(y)≥f(x∗)+0=f(x∗).f(y) \ge f(x^*) + 0 = f(x^*).

That is, x∗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 ff is twice continuously differentiable on an open convex set CC, then

 f convex on C  ⟺  ∇2f(x)⪰0 for all x∈C. \boxed{\ f \text{ convex on } C \iff \nabla^2 f(x) \succeq 0 \text{ for all } x \in C.\ }

Here "⪰0\succeq 0" means positive semidefinite: d⊤∇2f(x) d≥0d^\top \nabla^2 f(x)\, d \ge 0 for every direction d∈Rnd \in \mathbb{R}^n. Strict convexity follows from ∇2f(x)≻0\nabla^2 f(x) \succ 0 (positive definite), but the converse is not exact (e.g. f(x)=x4f(x) = x^4 is strictly convex on R\mathbb{R} even though f′′(0)=0f''(0) = 0).

Sketch. Restrict ff to a line. Define g(t):=f(x+td)g(t) := f(x + t d) for fixed x∈Cx \in C and d∈Rnd \in \mathbb{R}^n. By the chain rule from session 0.3,

g′(t)=∇f(x+td)⊤d,g′′(t)=d⊤∇2f(x+td) d.g'(t) = \nabla f(x + t d)^\top d, \qquad g''(t) = d^\top \nabla^2 f(x + t d)\, d.

Convexity of ff on CC is equivalent to convexity of gg on every line segment lying in CC, and convexity of a smooth one-dimensional function is equivalent to g′′≥0g'' \ge 0. Quantifying over all directions dd gives ∇2f⪰0\nabla^2 f \succeq 0.

Worked example — linear regression loss. Let

L(θ)=12∥Xθ−y∥2.L(\theta) = \tfrac{1}{2} \lVert X\theta - y \rVert^2.

Then

∇L(θ)=X⊤(Xθ−y),∇2L(θ)=X⊤X.\nabla L(\theta) = X^\top (X\theta - y), \qquad \nabla^2 L(\theta) = X^\top X.

The Gram matrix X⊤XX^\top X is always ⪰0\succeq 0 because d⊤X⊤Xd=∥Xd∥2≥0d^\top X^\top X d = \lVert X d \rVert^2 \ge 0. So MSE regression is convex in θ\theta. If XX has full column rank, X⊤X≻0X^\top X \succ 0 and the problem is strictly convex with a unique minimiser given by the normal equations θ∗=(X⊤X)−1X⊤y\theta^* = (X^\top X)^{-1} X^\top y.


5. Jensen's inequality

Finite form. If ff is convex, θi≥0\theta_i \ge 0 with ∑i=1nθi=1\sum_{i=1}^n \theta_i = 1, then

 f ⁣(∑i=1nθixi)≤∑i=1nθif(xi). \boxed{\ f\!\left(\sum_{i=1}^n \theta_i x_i\right) \le \sum_{i=1}^n \theta_i f(x_i).\ }

Proof (induction on nn). The base case n=2n = 2 is the definition of convexity. Suppose the inequality holds for n−1n - 1 points and consider nn points with weights θ1,…,θn\theta_1, \dots, \theta_n. Without loss of generality θn<1\theta_n < 1; set s=1−θn>0s = 1 - \theta_n > 0. Then

∑i=1nθixi=s⋅∑i=1n−1θisxi⏟=: x~+θnxn,\sum_{i=1}^n \theta_i x_i = s \cdot \underbrace{\sum_{i=1}^{n-1} \frac{\theta_i}{s} x_i}_{=: \,\tilde x} + \theta_n x_n,

and the weights θi/s\theta_i / s are nonnegative summing to 11. Apply convexity at the two points x~\tilde x and xnx_n with weights ss and θn\theta_n:

f ⁣(∑i=1nθixi)≤s f(x~)+θnf(xn).f\!\left(\sum_{i=1}^n \theta_i x_i\right) \le s\, f(\tilde x) + \theta_n f(x_n).

By the inductive hypothesis applied to x~\tilde x,

f(x~)≤∑i=1n−1θisf(xi).f(\tilde x) \le \sum_{i=1}^{n-1} \frac{\theta_i}{s} f(x_i).

Multiplying by ss and substituting,

f ⁣(∑i=1nθixi)≤∑i=1n−1θif(xi)+θnf(xn)=∑i=1nθif(xi).■f\!\left(\sum_{i=1}^n \theta_i x_i\right) \le \sum_{i=1}^{n-1} \theta_i f(x_i) + \theta_n f(x_n) = \sum_{i=1}^n \theta_i f(x_i). \quad\blacksquare

Expectation form. If XX is a random variable taking values in CC with E∥X∥<∞\mathbb{E}\lVert X \rVert < \infty, and ff is convex on CC, then

f(E[X])≤E[f(X)].f\bigl(\mathbb{E}[X]\bigr) \le \mathbb{E}[f(X)].

For discrete XX this is the finite form with θi=P(X=xi)\theta_i = \mathbb{P}(X = x_i). 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,qp, q on a finite set X\mathcal{X},

KL(p ∥ q)=∑x∈Xp(x)ln⁡p(x)q(x)≥0.\mathrm{KL}(p \,\|\, q) = \sum_{x \in \mathcal{X}} p(x) \ln \frac{p(x)}{q(x)} \ge 0.

Because −ln⁡-\ln is convex, Jensen applied with weights p(x)p(x) on points q(x)/p(x)q(x)/p(x) gives

−∑xp(x)ln⁡q(x)p(x)  ≥  −ln⁡∑xp(x) q(x)p(x)  =  −ln⁡∑xq(x)  =  −ln⁡1  =  0.-\sum_x p(x) \ln \frac{q(x)}{p(x)} \;\ge\; -\ln \sum_x p(x)\, \frac{q(x)}{p(x)} \;=\; -\ln \sum_x q(x) \;=\; -\ln 1 \;=\; 0.

Strict convexity of −ln⁡-\ln further gives equality iff q(x)/p(x)q(x)/p(x) is constant on the support of pp, i.e. iff p=qp = 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∈Rnx \in \mathbb{R}^n with ff differentiable there, and a unit vector u∈Rnu \in \mathbb{R}^n (so ∥u∥=1\lVert u \rVert = 1 in the Euclidean norm). The directional derivative of ff at xx in direction uu is

Duf(x):=lim⁡t→0f(x+tu)−f(x)t.D_u f(x) := \lim_{t \to 0} \frac{f(x + t u) - f(x)}{t}.

Apply the chain rule from 0.3 to g(t)=f(x+tu)g(t) = f(x + t u):

Duf(x)=g′(0)=∇f(x)⊤u.D_u f(x) = g'(0) = \nabla f(x)^\top u.

(We are using the numerator convention fixed in 0.2: ∂f/∂x\partial f / \partial x is a 1×n1 \times n row, and ∇f:=(∂f/∂x)⊤\nabla f := (\partial f / \partial x)^\top is its column transpose.)

We want the direction u∗u^* that maximises the instantaneous increase of ff:

u∗=arg⁡max⁡∥u∥=1∇f(x)⊤u.u^* = \arg\max_{\lVert u \rVert = 1} \nabla f(x)^\top u.

Cauchy–Schwarz gives, for any unit uu,

∇f(x)⊤u  ≤  ∥∇f(x)∥⋅∥u∥  =  ∥∇f(x)∥,\nabla f(x)^\top u \;\le\; \lVert \nabla f(x) \rVert \cdot \lVert u \rVert \;=\; \lVert \nabla f(x) \rVert,

with equality if and only if uu is the positive unit multiple of ∇f(x)\nabla f(x). Therefore

 u∗=∇f(x)∥∇f(x)∥,max⁡∥u∥=1Duf(x)=∥∇f(x)∥. \boxed{\ u^* = \frac{\nabla f(x)}{\lVert \nabla f(x) \rVert}, \qquad \max_{\lVert u \rVert = 1} D_u f(x) = \lVert \nabla f(x) \rVert.\ }

So the gradient points in the direction of steepest ascent, and its magnitude is the rate of ascent in that direction. Equivalently, −∇f(x)-\nabla f(x) is the direction of steepest descent, which is the move taken at each step of gradient descent:

θk+1=θk−η ∇L(θk).\theta_{k+1} = \theta_k - \eta\, \nabla L(\theta_k).

7. What this buys us going forward

Three takeaways to carry into the rest of the course.

  1. 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.

  2. 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).

  3. Jensen is everywhere. Already used implicitly in 0.6 for KL≥0\mathrm{KL} \ge 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)f(x) = \max(0, x) (the ReLU) is convex on R\mathbb{R}. Where does the first-order condition of §3 fail to apply, and why is that fine?

(b) Let XX be a positive random variable with E[X]=1\mathbb{E}[X] = 1. Use Jensen to prove E[ln⁡X]≤0\mathbb{E}[\ln X] \le 0, and identify the case of equality.

(c) For a positive-definite quadratic f(x)=12x⊤Ax−b⊤xf(x) = \tfrac{1}{2} x^\top A x - b^\top x with A≻0A \succ 0, compute ∇f\nabla f and ∇2f\nabla^2 f, and show the unique minimiser is x∗=A−1bx^* = A^{-1} b. From §6, what does steepest descent at x≠x∗x \ne x^* look like? Why does −∇f(x)-\nabla f(x) generally not point at x∗x^*?

(d) Suppose f,g:C→Rf, g : C \to \mathbb{R} are both convex on a convex set CC, and α,β≥0\alpha, \beta \ge 0. Prove that αf+βg\alpha f + \beta g is convex. (This is the property that lets us add an L2 regulariser λ∥θ∥2\lambda \lVert \theta \rVert^2 to any convex loss and stay convex.)


References

  1. [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

    ↩
  2. [2]

    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).

    ↩