Type at least 2 letters. Enter opens the first result, Esc closes.

    Optimization · Lesson 2 · Gradient descent and curvature

    Convexity, smoothness and the condition number

    Intermediate 75 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 01 (gradient descent and the narrow valley); the Hessian and Taylor's theorem (calculus 03); eigenvalues of symmetric matrices and the Rayleigh quotient (linear algebra 06); positive semidefinite matrices and the order A⪯BA \preceq B (linear algebra 07). Time: about 75 minutes, plus the notebook.


    Why this matters #

    Most convergence rates in this track depend on three numbers: the largest curvature LL, the smallest curvature μ\mu, and their ratio κ=L/μ\kappa = L/\mu. For each digit of accuracy, gradient descent needs a number of steps proportional to κ\kappa (lesson 03); momentum needs a number proportional to κ\sqrt\kappa (lesson 04). Convexity turns "the gradient is zero" into "this is the global minimum", which is why linear models are easy to train and to analyse. A neural-network loss is not convex, but near a minimum the Hessian still plays the role of LL and μ\mu: its largest eigenvalue limits the learning rate, and preconditioners (feature scaling, Adam, Shampoo, Muon) try to make the effective κ\kappa smaller. Discretized differential equations have a huge κ\kappa, one reason why physics-informed losses are hard to train.

    1. Predict first #

    2. The picture #

    Convex: chords above, tangents below #

    A function of one variable is convex when the segment between any two points of its graph (a chord) lies on or above the graph. For a differentiable function this is the same as: every tangent line lies on or below the graph. For a twice-differentiable function it is the same as f′′≥0f'' \ge 0 everywhere: the slope never decreases.

    Interactive figure: loading…

    Drag the violet chord ends aa, bb and the aqua tangent point tt. Where the chord lies below the graph, or the graph falls below the tangent, the gap is shaded magenta. For the double well both tests fail for some choices. For 12x2\tfrac12 x^2, the logistic loss and x4/4x^4/4 no choice fails: they are convex. For ∣x∣|x| the chord test passes everywhere, but at t=0t = 0 there is no tangent line (no derivative), only many supporting lines.

    Two parabolas around the graph #

    Suppose μ≤f′′(x)≤L\mu \le f''(x) \le L for all xx, with μ>0\mu > 0. Pick any point xx. Taylor's theorem puts the whole graph between two parabolas that touch it at xx:

    f(y)≤f(x)+f′(x)(y−x)+L2(y−x)2,f(y)≥f(x)+f′(x)(y−x)+μ2(y−x)2.\begin{aligned} f(y) &\le f(x) + f'(x)(y - x) + \tfrac{L}{2}(y - x)^2, \\ f(y) &\ge f(x) + f'(x)(y - x) + \tfrac{\mu}{2}(y - x)^2. \end{aligned}

    The first line is an upper parabola with curvature LL, the second a lower parabola with curvature μ\mu. The upper parabola ("ff curves up at most as fast as LL") is LL-smoothness. The lower one ("ff curves up at least as fast as μ\mu") is μ\mu-strong convexity. The important word is every: the two parabolas must hold around every point xx, not only at the minimum.

    Interactive figure: loading…

    Drag the point on the graph and move the sliders LL and μ\mu. The orange parabola has curvature LL, the blue parabola curvature μ\mu; both touch ff at the point. Where ff leaves the band, the gap is shaded magenta. For 12x2+log⁡cosh⁡x\tfrac12x^2 + \log\cosh x, find the smallest LL and the largest μ\mu that work at every point (answer: L=2L = 2, μ=1\mu = 1). Then try x4/4x^4/4: whatever LL you choose, some point far out breaks the upper bound.

    Two dimensions: the condition number is the shape of the valley #

    Take the quadratic f(x)=12(x12+100 x22)=12x⊤Axf(x) = \tfrac12(x_1^2 + 100\,x_2^2) = \tfrac12 x^\top A x with A=diag⁡(1,100)A = \operatorname{diag}(1, 100). Its curvature is 1 along x1x_1 and 100 along x2x_2, so μ=1\mu = 1, L=100L = 100 and κ=100\kappa = 100. The level sets are ellipses with axis ratio κ=10\sqrt{\kappa} = 10: a long, narrow valley. As in lesson 01, gradient descent must use steps small enough for the steep direction (η<2/L\eta < 2/L), and then it crawls along the flat one. For a general quadratic, LL and μ\mu are the largest and smallest eigenvalues of AA, and the valley points along the eigenvectors.

    Interactive figure: loading…

    Move the eigenvalues λ1\lambda_1, λ2\lambda_2 and the angle φ\varphi. The blue ellipses are level sets of 12x⊤Hx\tfrac12 x^\top H x; read μ\mu, LL, κ\kappa and the axis ratio κ\sqrt\kappa in the panel. Think of HH as 1nX⊤X\frac1n X^\top X, the covariance of two centered features. Press Standardize the features: the aqua dashed ellipses show the loss after each feature is divided by its standard deviation. With φ=0\varphi = 0 they are circles (κ=1\kappa = 1). With φ=45°\varphi = 45° nothing changes: the features are correlated, and rescaling each feature alone cannot remove a correlation.

    Quick check

    A quadratic f(x)=12x⊤Axf(x) = \tfrac12 x^\top A x has A=(3113)A = \begin{pmatrix} 3 & 1 \\ 1 & 3 \end{pmatrix}. What is its condition number κ\kappa?

    3. Definitions and main results #

    Convex sets and convex functions #

    A set C⊆RdC \subseteq \R^d is convex if, for all x,y∈Cx, y \in C and θ∈[0,1]\theta \in [0, 1], the point θx+(1−θ)y\theta x + (1 - \theta) y is in CC: the segment between two points of CC stays in CC. Examples: balls, half-spaces {x:a⊤x≤b}\{x : a^\top x \le b\}, the positive semidefinite matrices, the probability simplex. The intersection of convex sets is convex.

    A function ff is convex if its domain is convex and, for all x,yx, y in it and θ∈[0,1]\theta \in [0, 1],

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

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

    Theorem 1 (local = global). If ff is convex, every local minimum is a global minimum, and the set of minimizers is convex.

    Proof.

    1. Let xx be a local minimum: there is r>0r > 0 with f(x)≤f(z)f(x) \le f(z) whenever ∥z−x∥≤r\|z - x\| \le r. Suppose some yy has f(y)<f(x)f(y) < f(x).
    2. For θ∈(0,1]\theta \in (0, 1] let zθ=(1−θ)x+θyz_\theta = (1 - \theta) x + \theta y. Convexity gives f(zθ)≤(1−θ)f(x)+θf(y)<f(x)f(z_\theta) \le (1 - \theta) f(x) + \theta f(y) < f(x).
    3. For θ≤r/∥y−x∥\theta \le r/\|y - x\|, the point zθz_\theta is inside the ball around xx and has a smaller value than xx. This contradicts step 1, so no such yy exists.
    4. If xx and yy are both minimizers with value f⋆f^\star, then f(θx+(1−θ)y)≤θf⋆+(1−θ)f⋆=f⋆f(\theta x + (1 - \theta) y) \le \theta f^\star + (1 - \theta) f^\star = f^\star: the whole segment consists of minimizers. ■\blacksquare

    The first-order condition: tangents lie below #

    Theorem 2 (first-order condition). Let ff be differentiable on an open convex set DD. Then ff is convex if and only if

    f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top (y - x)

    for all x,y∈Dx, y \in D. The right side is the tangent plane at xx (in 1D, the tangent line).

    Proof.

    1. (⇒) Fix x,yx, y and t∈(0,1]t \in (0, 1]. Convexity with θ=1−t\theta = 1 - t gives f(x+t(y−x))≤(1−t)f(x)+tf(y)f(x + t(y - x)) \le (1 - t) f(x) + t f(y).
    2. Subtract f(x)f(x) and divide by tt: f(x+t(y−x))−f(x)t≤f(y)−f(x)\dfrac{f(x + t(y - x)) - f(x)}{t} \le f(y) - f(x).
    3. Let t→0+t \to 0^+. The left side tends to the directional derivative ∇f(x)⊤(y−x)\nabla f(x)^\top (y - x).
    4. (⇐) Take θ∈[0,1]\theta \in [0, 1] and z=θx+(1−θ)yz = \theta x + (1 - \theta) y. Use the inequality at the point zz twice: f(x)≥f(z)+∇f(z)⊤(x−z)f(x) \ge f(z) + \nabla f(z)^\top (x - z) and f(y)≥f(z)+∇f(z)⊤(y−z)f(y) \ge f(z) + \nabla f(z)^\top (y - z).
    5. Multiply the first by θ\theta, the second by 1−θ1 - \theta, and add. The gradient terms cancel, because θ(x−z)+(1−θ)(y−z)=θx+(1−θ)y−z=0\theta(x - z) + (1 - \theta)(y - z) = \theta x + (1 - \theta) y - z = 0. What is left is θf(x)+(1−θ)f(y)≥f(z)\theta f(x) + (1 - \theta) f(y) \ge f(z). ■\blacksquare

    Consequence. If ff is convex and differentiable and ∇f(x⋆)=0\nabla f(x^\star) = 0, then f(y)≥f(x⋆)+0f(y) \ge f(x^\star) + 0 for every yy: a stationary point is a global minimizer. For a convex loss, "gradient descent converged" means "we found the best parameters".

    The second-order condition: curvature is never negative #

    Theorem 3 (second-order condition). Let ff be twice continuously differentiable on an open convex set DD. Then ff is convex if and only if the Hessian is positive semidefinite, ∇2f(x)⪰0\nabla^2 f(x) \succeq 0, at every x∈Dx \in D.

    Why. Taylor's theorem with integral remainder gives

    f(y)=f(x)+∇f(x)⊤(y−x)+∫01(1−t) (y−x)⊤∇2f(x+t(y−x)) (y−x) dt.\begin{aligned} f(y) = f(x) &+ \nabla f(x)^\top(y - x) \\ &+ \int_0^1 (1 - t)\,(y - x)^\top \nabla^2 f(x + t(y - x))\,(y - x)\,dt . \end{aligned}

    If the Hessian is PSD, the integral is ≥0\ge 0, and Theorem 2 gives convexity. Conversely, on a line g(t)=f(x+tv)g(t) = f(x + tv) the function gg is convex, so its slope g′g' cannot decrease, and g′′(0)=v⊤∇2f(x) v≥0g''(0) = v^\top \nabla^2 f(x)\, v \ge 0 for every direction vv.

    In 2D, a PSD Hessian means the surface is a bowl or a trough at every point, never a saddle. The figure shows the three shapes of a quadratic x⊤Sxx^\top S x.

    Interactive figure: loading…

    Switch between bowl, trough and saddle, and rotate the figure. A convex quadratic is a bowl (both eigenvalues of SS positive: strongly convex) or a trough (one eigenvalue zero: convex, not strongly convex). The saddle has one negative eigenvalue: not convex.

    Two tests you will use again:

    • Least squares f(w)=12n∥Xw−y∥2f(w) = \frac{1}{2n}\|Xw - y\|^2. The Hessian is 1nX⊤X\frac1n X^\top X, and v⊤X⊤Xv=∥Xv∥2≥0v^\top X^\top X v = \|Xv\|^2 \ge 0. Convex.
    • Logistic loss with labels yi∈{−1,+1}y_i \in \{-1, +1\}: ℓ(w)=1n∑ilog⁡(1+e−mi)\ell(w) = \frac1n \sum_i \log(1 + e^{-m_i}) with margins mi=yi xi⊤wm_i = y_i\, x_i^\top w. With the sigmoid σ(m)=1/(1+e−m)\sigma(m) = 1/(1 + e^{-m}),
    ∇2ℓ(w)=1n∑i=1nσ(mi)(1−σ(mi)) xixi⊤.\nabla^2 \ell(w) = \frac1n \sum_{i=1}^n \sigma(m_i)\big(1 - \sigma(m_i)\big)\, x_i x_i^\top .

    The weights σ(1−σ)\sigma(1 - \sigma) lie in (0,14](0, \tfrac14], so ∇2ℓ⪰0\nabla^2\ell \succeq 0 (convex) and ∇2ℓ⪯14nX⊤X\nabla^2\ell \preceq \frac{1}{4n}X^\top X. The weights go to 0 as the margins grow, so no μ>0\mu > 0 works everywhere. Adding λ2∥w∥2\tfrac{\lambda}{2}\|w\|^2 adds λI\lambda I to the Hessian.

    Quick check

    Is f(x)=max⁡(x,0)2f(x) = \max(x, 0)^2 convex, and is it LL-smooth?

    Where convexity fails: neural networks #

    The smallest example is a "network" with one weight per layer, f(u,v)=12(uv−1)2f(u, v) = \tfrac12(uv - 1)^2. Both (1,1)(1, 1) and (−1,−1)(-1, -1) are minimizers (f=0f = 0), but their midpoint (0,0)(0, 0) has f=12f = \tfrac12. The set of minimizers, the hyperbola uv=1uv = 1, is not convex, so by Theorem 1 the function is not convex. The Hessian agrees: ∇2f(0,0)=(0−1−10)\nabla^2 f(0, 0) = \begin{pmatrix} 0 & -1 \\ -1 & 0 \end{pmatrix} has eigenvalues ±1\pm 1.

    The same happens in every network with two or more hidden units. Swapping two hidden units (their rows in the first layer, their columns in the next) does not change the function. So a minimizer θA\theta_A with two different hidden units has a copy θB\theta_B with the same loss. If the loss were convex, the midpoint would also be a minimizer. But at the midpoint the two units are equal, and the loss is in general higher. Sign flips (for tanh) and rescaling (aW1,W2/a)(aW_1, W_2/a) with a>0a > 0 (for ReLU) give the same conclusion.

    Interactive figure: loading…

    Move tt along the segment from θA\theta_A (t=0t = 0) to θB\theta_B (t=1t = 1). The two ends compute the same function and have loss 0. The violet curve is the loss along the segment; a convex loss would stay on or below the dashed chord, here at 0. In the two-unit tanh network the midpoint has equal hidden units whose output weights cancel, so it outputs 0 everywhere. Switch to 12(uv−1)2\tfrac12(uv - 1)^2 to see the segment leave the set of minimizers (aqua).

    Operations that preserve convexity #

    You rarely check a Hessian by hand. You build the function from convex pieces (Boyd and Vandenberghe §3.2):

    • nonnegative weighted sums ∑icifi\sum_i c_i f_i with ci≥0c_i \ge 0 (a data term plus a regularizer);
    • composition with an affine map, f(Ax+b)f(Ax + b) (any convex loss of a linear model);
    • pointwise maximum max⁡ifi(x)\max_i f_i(x), or supremum over any family (hinge loss max⁡(0,1−y w⊤x)\max(0, 1 - y\,w^\top x));
    • h(g(x))h(g(x)) with hh convex and nondecreasing and gg convex (for example eg(x)e^{g(x)});
    • partial minimization inf⁡z∈Cf(x,z)\inf_{z \in C} f(x, z), with ff jointly convex and CC convex.

    CVXPY's disciplined convex programming (DCP) rules apply these rules automatically. Products of convex functions and compositions like W2 ϕ(W1x)W_2\,\phi(W_1 x) are not on the list.

    Smoothness, strong convexity and the condition number #

    Definition (LL-smooth). ff is differentiable and its gradient is LL-Lipschitz: ∥∇f(x)−∇f(y)∥≤L ∥x−y∥\|\nabla f(x) - \nabla f(y)\| \le L\,\|x - y\| for all x,yx, y. For twice-differentiable ff this is the same as −LI⪯∇2f(x)⪯LI-L I \preceq \nabla^2 f(x) \preceq L I for all xx.

    Definition (μ\mu-strongly convex, μ>0\mu > 0). f−μ2∥x∥2f - \tfrac{\mu}{2}\|x\|^2 is convex. For differentiable ff this is the same as f(y)≥f(x)+∇f(x)⊤(y−x)+μ2∥y−x∥2f(y) \ge f(x) + \nabla f(x)^\top (y - x) + \tfrac{\mu}{2}\|y - x\|^2 for all x,yx, y. For twice-differentiable ff: ∇2f(x)⪰μI\nabla^2 f(x) \succeq \mu I for all xx.

    With both, μI⪯∇2f(x)⪯LI\mu I \preceq \nabla^2 f(x) \preceq L I for all xx. The condition number is κ=L/μ≥1\kappa = L/\mu \ge 1.

    • Quadratic f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with AA symmetric positive definite: the Hessian is AA everywhere, so L=λmax⁡(A)L = \lambda_{\max}(A) and μ=λmin⁡(A)\mu = \lambda_{\min}(A) (Rayleigh quotient, linear algebra 06).
    • Ridge regression f(w)=12n∥Xw−y∥2+λ2∥w∥2f(w) = \frac{1}{2n}\|Xw - y\|^2 + \tfrac{\lambda}{2}\|w\|^2 with X∈Rn×dX \in \R^{n \times d}: ∇2f=1nX⊤X+λI\nabla^2 f = \frac1n X^\top X + \lambda I. With the singular values s1≥⋯≥sds_1 \ge \dots \ge s_d of XX (set sj=0s_j = 0 for j>nj > n when n<dn < d), L=s12/n+λL = s_1^2/n + \lambda and μ=sd2/n+λ\mu = s_d^2/n + \lambda. A larger λ\lambda lowers κ\kappa. With λ=0\lambda = 0 and rank⁡X<d\operatorname{rank} X < d, μ=0\mu = 0: convex, but not strongly convex.
    Strictly convex is not strongly convex

    x4x^4 and exe^x are strictly convex, but not strongly convex. For x4x^4, f′′(0)=0f''(0) = 0; for exe^x, f′′(x)=ex→0f''(x) = e^x \to 0 as x→−∞x \to -\infty. No μ>0\mu > 0 fits under the curve everywhere. Strong convexity is what gives the linear convergence rates of lesson 03; strict convexity only gives a unique minimizer (when one exists: exe^x has none).

    The descent lemma #

    Theorem 4 (descent lemma). If ff is LL-smooth, then for all x,yx, y:

    f(y)≤f(x)+∇f(x)⊤(y−x)+L2∥y−x∥2.f(y) \le f(x) + \nabla f(x)^\top (y - x) + \tfrac{L}{2}\|y - x\|^2 .

    No convexity is needed. This is the upper parabola of section 2, in any dimension.

    Proof. Write zt=x+t(y−x)z_t = x + t(y - x) for the points of the segment from xx to yy.

    1. By the fundamental theorem of calculus along the segment, f(y)−f(x)=∫01∇f(zt)⊤(y−x) dtf(y) - f(x) = \int_0^1 \nabla f(z_t)^\top (y - x)\, dt.
    2. Subtract ∇f(x)⊤(y−x)=∫01∇f(x)⊤(y−x) dt\nabla f(x)^\top (y - x) = \int_0^1 \nabla f(x)^\top(y - x)\,dt. The integrand becomes (∇f(zt)−∇f(x))⊤(y−x)\big(\nabla f(z_t) - \nabla f(x)\big)^\top (y - x).
    3. By Cauchy–Schwarz, the integrand is at most ∥∇f(zt)−∇f(x)∥ ∥y−x∥\|\nabla f(z_t) - \nabla f(x)\|\,\|y - x\|. By the Lipschitz bound, ∥∇f(zt)−∇f(x)∥≤L∥zt−x∥=Lt ∥y−x∥\|\nabla f(z_t) - \nabla f(x)\| \le L\|z_t - x\| = Lt\,\|y - x\|. So the integrand is at most Lt ∥y−x∥2L t\,\|y - x\|^2.
    4. ∫01Lt dt=L/2\int_0^1 L t\, dt = L/2. ■\blacksquare

    Corollary (one gradient step). Put y=x−η∇f(x)y = x - \eta\nabla f(x) into the lemma:

    f(x−η∇f(x))≤f(x)−η(1−Lη2) ∥∇f(x)∥2.f(x - \eta\nabla f(x)) \le f(x) - \eta\big(1 - \tfrac{L\eta}{2}\big)\, \|\nabla f(x)\|^2 .

    The decrease is guaranteed for 0<η<2/L0 < \eta < 2/L, and it is largest at η=1/L\eta = 1/L, where f(x−∇f(x)/L)≤f(x)−∥∇f(x)∥2/(2L)f(x - \nabla f(x)/L) \le f(x) - \|\nabla f(x)\|^2/(2L). So the step 1/L1/L minimizes the upper parabola. This answers the question "how small is small enough?" left open in lesson 01.

    The PL inequality #

    On the quadratic of section 2 (x⋆=0x^\star = 0, f⋆=0f^\star = 0): ∥∇f(x)∥2=x12+1002x22\|\nabla f(x)\|^2 = x_1^2 + 100^2 x_2^2 and 2μ (f(x)−f⋆)=x12+100 x222\mu\,(f(x) - f^\star) = x_1^2 + 100\, x_2^2. Each term on the left is at least the matching term on the right, because λi2≥μλi\lambda_i^2 \ge \mu\lambda_i for every eigenvalue. So the gradient is large whenever the gap f−f⋆f - f^\star is large. This is what makes gradient descent converge fast.

    Theorem 5 (Polyak–Łojasiewicz, PL). If ff is differentiable and μ\mu-strongly convex with minimum value f⋆f^\star, then

    ∥∇f(x)∥2≥2μ (f(x)−f⋆)for all x.\|\nabla f(x)\|^2 \ge 2\mu\,\big(f(x) - f^\star\big) \qquad \text{for all } x.

    Proof.

    1. Strong convexity at xx: f(y)≥f(x)+∇f(x)⊤(y−x)+μ2∥y−x∥2f(y) \ge f(x) + \nabla f(x)^\top (y - x) + \tfrac{\mu}{2}\|y - x\|^2 for all yy.
    2. Take the minimum over yy of both sides. The left side becomes f⋆f^\star.
    3. The right side is a convex quadratic in yy. Its gradient ∇f(x)+μ(y−x)\nabla f(x) + \mu(y - x) is zero at y=x−∇f(x)/μy = x - \nabla f(x)/\mu, and its value there is f(x)−∥∇f(x)∥2/(2μ)f(x) - \|\nabla f(x)\|^2/(2\mu).
    4. So f⋆≥f(x)−∥∇f(x)∥2/(2μ)f^\star \ge f(x) - \|\nabla f(x)\|^2/(2\mu). Rearrange. ■\blacksquare

    The same argument on the descent lemma gives the upper twin, ∥∇f(x)∥2≤2L (f(x)−f⋆)\|\nabla f(x)\|^2 \le 2L\,(f(x) - f^\star), for any LL-smooth ff with a minimum. Together they explain the stopping rule of lesson 01: a small gradient means a small gap, f(x)−f⋆≤∥∇f(x)∥2/(2μ)f(x) - f^\star \le \|\nabla f(x)\|^2/(2\mu).

    Lesson 03 combines PL with the corollary of the descent lemma: with η=1/L\eta = 1/L, each step gives f(xk+1)−f⋆≤(1−μ/L)(f(xk)−f⋆)f(x_{k+1}) - f^\star \le (1 - \mu/L)(f(x_k) - f^\star), a linear rate whose speed depends only on κ\kappa.

    Quick check

    Which statement about the PL inequality ∥∇f∥2≥2μ(f−f⋆)\|\nabla f\|^2 \ge 2\mu(f - f^\star) is true?

    PL without convexity

    PL is weaker than strong convexity. Two examples:

    • f(x)=x2+3sin⁡2xf(x) = x^2 + 3\sin^2 x has f′′(π/2)=2+6cos⁡π=−4<0f''(\pi/2) = 2 + 6\cos\pi = -4 < 0, so it is not convex. But its only stationary point is x=0x = 0, and it satisfies PL: Karimi, Nutini and Schmidt (2016) show that μ=1/32\mu = 1/32 works; numerically the best constant is about 0.1750.175.
    • Least squares with a singular X⊤XX^\top X has many minimizers (not strongly convex), but satisfies PL with μ\mu equal to the smallest nonzero eigenvalue of 1nX⊤X\frac1n X^\top X.

    Since the linear rate of lesson 03 uses only the descent lemma and PL, it also holds for these functions. Liu, Zhu and Belkin (2022) show that very wide neural networks satisfy a PL-type condition in a ball around their initialization, which helps explain why gradient descent can drive their training loss to zero.

    Feature scaling is diagonal preconditioning #

    With centered, uncorrelated features of standard deviations sjs_j, the least-squares Hessian is 1nX⊤X≈diag⁡(s12,…,sd2)\frac1n X^\top X \approx \operatorname{diag}(s_1^2, \dots, s_d^2), so κ≈smax⁡2/smin⁡2\kappa \approx s_{\max}^2/s_{\min}^2. Standardizing (xj→xj/sjx_j \to x_j/s_j) gives a Hessian of about II and κ≈1\kappa \approx 1.

    In weight space this is a change of variables w=Dvw = Dv with D=diag⁡(1/sj)D = \operatorname{diag}(1/s_j):

    1. The new loss g(v)=f(Dv)g(v) = f(Dv) has gradient ∇g(v)=D ∇f(Dv)\nabla g(v) = D\,\nabla f(Dv) and Hessian ∇2g=D ∇2f D\nabla^2 g = D\, \nabla^2 f\, D (chain rule).
    2. A gradient step on vv is v+=v−ηD∇f(w)v^+ = v - \eta D\nabla f(w). Multiply by DD to map it back: w+=w−ηD2∇f(w)w^+ = w - \eta D^2 \nabla f(w).
    3. So gradient descent on the standardized problem is gradient descent on the original problem with one step size per coordinate, η/sj2\eta/s_j^2: a diagonal preconditioner.

    A diagonal preconditioner cannot remove correlations. Two standardized features with correlation ρ\rho give the Hessian (1ρρ1)\begin{pmatrix} 1 & \rho \\ \rho & 1\end{pmatrix} with eigenvalues 1±ρ1 \pm \rho, so κ=(1+∣ρ∣)/(1−∣ρ∣)\kappa = (1 + |\rho|)/(1 - |\rho|). That needs a full-matrix preconditioner (whitening, or Newton's method in lesson 10).

    Why κ\kappa controls first-order methods. On a quadratic, gradient descent multiplies the error along eigenvector ii by 1−ηλi1 - \eta\lambda_i per step (lesson 01). The steep direction needs η<2/L\eta < 2/L. Then the flat direction keeps a factor 1−ημ>1−2/κ1 - \eta\mu > 1 - 2/\kappa, so about κ2ln⁡(1/ε)\tfrac{\kappa}{2}\ln(1/\varepsilon) steps or more are needed to reduce its error by a factor ε\varepsilon (lesson 03; momentum needs about κ\sqrt{\kappa}, lesson 04).

    Quick check

    Least squares with two centered, uncorrelated features, both with standard deviation 1 (κ=1\kappa = 1). You multiply the second feature by 10. What is the new κ\kappa of the Hessian 1nX⊤X\frac1n X^\top X?

    Answers to section 1. (1) x4x^4 is convex and even strictly convex. But f′′(x)=12x2f''(x) = 12x^2 is unbounded, so no LL works on all of R\R, and f′′(0)=0f''(0) = 0, so no μ>0\mu > 0 works. The rates of lesson 03 do not apply: fixed-step gradient descent, x+=x(1−4ηx2)x^+ = x(1 - 4\eta x^2), even diverges from every start with ∣x0∣>1/2η|x_0| > 1/\sqrt{2\eta}. (2) The variances are 11 and 10610^6, so κ≈106\kappa \approx 10^6 before and κ≈1\kappa \approx 1 after. The step estimate κ2ln⁡(1/ε)\tfrac{\kappa}{2}\ln(1/\varepsilon) drops by a factor of about 10610^6.

    4. Worked example #

    Least squares with n=4n = 4 samples and two centered, uncorrelated features with standard deviations 1 and 10:

    X=(110−1101−10−1−10),y=X(10.1)=(200−2)X = \begin{pmatrix} 1 & 10 \\ -1 & 10 \\ 1 & -10 \\ -1 & -10 \end{pmatrix}, \qquad y = X \begin{pmatrix} 1 \\ 0.1 \end{pmatrix} = \begin{pmatrix} 2 \\ 0 \\ 0 \\ -2 \end{pmatrix}

    and the loss is f(w)=12n∥Xw−y∥2f(w) = \frac{1}{2n}\|Xw - y\|^2.

    So w⋆=(1,0.1)w^\star = (1, 0.1) and f⋆=0f^\star = 0.

    Step 1: the constants. The two columns are orthogonal: (1,−1,1,−1)⋅(10,10,−10,−10)=0(1, -1, 1, -1)\cdot(10, 10, -10, -10) = 0. So 1nX⊤X=diag⁡(4/4, 400/4)=diag⁡(1,100)\frac1n X^\top X = \operatorname{diag}(4/4,\ 400/4) = \operatorname{diag}(1, 100). ff is strongly convex with μ=1\mu = 1, L=100L = 100, κ=100\kappa = 100.

    Step 2: ridge. Adding 12∥w∥2\tfrac{1}{2}\|w\|^2 (λ=1\lambda = 1) gives the Hessian diag⁡(2,101)\operatorname{diag}(2, 101) and κ=50.5\kappa = 50.5: better conditioned, but the minimizer moves to (0.5, 0.099)(0.5,\ 0.099), because ridge changes the problem.

    Step 3: standardizing. Divide column 2 by its standard deviation 10. The Hessian becomes II, so κ=1\kappa = 1, and the minimizer in the new coordinates is v⋆=(1,1)v^\star = (1, 1) (since 10⋅0.1=110 \cdot 0.1 = 1).

    Step 4: the descent lemma at w0=0w_0 = 0. f(w0)=18∥y∥2=18⋅8=1f(w_0) = \frac18\|y\|^2 = \frac18 \cdot 8 = 1. The gradient is ∇f(w)=1nX⊤(Xw−y)=diag⁡(1,100) (w−w⋆)\nabla f(w) = \frac1n X^\top(Xw - y) = \operatorname{diag}(1, 100)\,(w - w^\star), so ∇f(w0)=(−1,−10)\nabla f(w_0) = (-1, -10) and ∥∇f(w0)∥2=101\|\nabla f(w_0)\|^2 = 101. One step with η=1/L=0.01\eta = 1/L = 0.01: w1=(0,0)−0.01 (−1,−10)=(0.01, 0.1)w_1 = (0, 0) - 0.01\,(-1, -10) = (0.01,\ 0.1). Then

    f(w1)=12(1⋅(0.01−1)2+100⋅(0.1−0.1)2)=12⋅0.9801=0.49005,f(w0)−12L∥∇f(w0)∥2=1−101200=0.495.\begin{aligned} f(w_1) &= \tfrac12\big(1 \cdot (0.01 - 1)^2 + 100 \cdot (0.1 - 0.1)^2\big) \\ &= \tfrac12 \cdot 0.9801 = 0.49005, \\ f(w_0) - \tfrac{1}{2L}\|\nabla f(w_0)\|^2 &= 1 - \tfrac{101}{200} = 0.495 . \end{aligned}

    So f(w1)=0.49005≤0.495f(w_1) = 0.49005 \le 0.495: the bound of the corollary holds, and it is almost tight.

    Step 5: PL at w0w_0. ∥∇f∥2=101≥2μ (f−f⋆)=2\|\nabla f\|^2 = 101 \ge 2\mu\,(f - f^\star) = 2, and 101≤2L (f−f⋆)=200101 \le 2L\,(f - f^\star) = 200. ✓

    Step 6: what the step did. The steep coordinate w2w_2 reached its optimum 0.10.1 in one step. The flat coordinate w1w_1 moved only 1% of the way, from 0 to 0.01. Its error shrinks by 1−μ/L=0.991 - \mu/L = 0.99 per step, so a reduction by 10610^6 takes ln⁡(106)/(−ln⁡0.99)≈1,375\ln(10^6)/(-\ln 0.99) \approx 1{,}375 steps. After standardizing, the gradient at v=0v = 0 is (−1,−1)(-1, -1), and one step with η=1/L=1\eta = 1/L = 1 lands exactly on v⋆=(1,1)v^\star = (1, 1).

    5. In code #

    NumPy checks the worked example.

    import numpy as np
    
    # Worked example: two centered, uncorrelated features with standard deviations 1 and 10
    X = np.array([[1, 10], [-1, 10], [1, -10], [-1, -10]], dtype=float)
    w_star = np.array([1.0, 0.1])
    y = X @ w_star                               # so f* = f(w*) = 0
    n = len(y)
    H = X.T @ X / n                              # Hessian of f(w) = ||Xw - y||^2 / (2n)
    mu, L = np.linalg.eigvalsh(H)                # eigenvalues in ascending order
    print(mu, L, L / mu)                         # 1.0 100.0 100.0
    
    ridge = np.linalg.eigvalsh(H + 1.0 * np.eye(2))       # ridge with lambda = 1
    print(ridge, ridge[1] / ridge[0])                      # [  2. 101.] 50.5
    
    Xs = X / X.std(axis=0)                                 # standardized features
    print(np.linalg.eigvalsh(Xs.T @ Xs / n))               # [1. 1.]  -> kappa = 1
    
    f = lambda w: np.sum((X @ w - y) ** 2) / (2 * n)
    grad = lambda w: X.T @ (X @ w - y) / n
    w0 = np.zeros(2)
    g0 = grad(w0)
    w1 = w0 - g0 / L                                       # one step with step 1/L
    print(f(w0), g0, g0 @ g0)                              # 1.0 [ -1. -10.] 101.0
    print(w1, f(w1), f(w0) - g0 @ g0 / (2 * L))            # [0.01 0.1 ] 0.49005 0.495
    print(2 * mu * f(w0) <= g0 @ g0 <= 2 * L * f(w0))      # True (PL and its upper twin)
    

    The two parabolas and PL on a convex function that is not quadratic, f(x)=12x⊤Ax−b⊤x+∑ilog⁡cosh⁡xif(x) = \tfrac12 x^\top A x - b^\top x + \sum_i \log\cosh x_i. Its Hessian is A+diag⁡(1/cosh⁡2xi)A + \operatorname{diag}(1/\cosh^2 x_i) and 0<1/cosh⁡2≤10 < 1/\cosh^2 \le 1, so μ=λmin⁡(A)\mu = \lambda_{\min}(A) and L=λmax⁡(A)+1L = \lambda_{\max}(A) + 1. We test both bounds at 10,000 random pairs of points:

    import numpy as np
    from scipy.optimize import minimize
    
    rng = np.random.default_rng(0)
    M = rng.standard_normal((3, 3))
    A = M @ M.T + 0.5 * np.eye(3)                 # symmetric positive definite
    b = rng.standard_normal(3)
    f = lambda x: 0.5 * x @ A @ x - b @ x + np.sum(np.log(np.cosh(x)))
    grad = lambda x: A @ x - b + np.tanh(x)
    ev = np.linalg.eigvalsh(A)
    mu, L = ev[0], ev[-1] + 1.0
    
    upper = lower = True
    for _ in range(10_000):
        x, z = 3 * rng.standard_normal((2, 3))
        tangent = f(x) + grad(x) @ (z - x)
        upper &= f(z) <= tangent + L / 2 * (z - x) @ (z - x) + 1e-9
        lower &= f(z) >= tangent + mu / 2 * (z - x) @ (z - x) - 1e-9
    fstar = minimize(f, np.zeros(3), jac=grad, method="BFGS", options={"gtol": 1e-12}).fun
    pl = all(grad(x) @ grad(x) >= 2 * mu * (f(x) - fstar) - 1e-9
             for x in 3 * rng.standard_normal((10_000, 3)))
    print(upper, lower, pl)                       # True True True
    

    6. Where it shows up #

    • Linear models. Least squares, ridge, logistic regression, the lasso and the hinge-loss SVM are convex: a stationary point is the answer, and κ\kappa is the condition number of the (regularized) feature covariance. For example, scikit-learn's documentation says that its sag and saga solvers converge fast only when the features have about the same scale.
    • Deep networks. Not convex (permutation symmetry), but near a minimum the Hessian plays the role of LL and μ\mu. Its largest eigenvalue, the sharpness, limits the stable learning rate to about 2/λmax⁡2/\lambda_{\max} (lessons 03 and 11). Adam's per-coordinate scaling is a diagonal preconditioner (lesson 06); Shampoo and Muon use matrix preconditioners (lesson 12). Normalization layers such as BatchNorm apply the idea of feature scaling inside the network: they standardize the inputs of a layer.
    • ML for science. The 1D Laplacian on NN grid points has κ≈4(N+1)2/π2\kappa \approx 4(N + 1)^2/\pi^2, which grows like h−2h^{-2} for grid spacing hh (about 48, 4,134 and 406,095 for NN = 10, 100, 1,000). A least-squares loss on the residual of the discretized equation, 12∥Tu−g∥2\tfrac12\|Tu - g\|^2, has Hessian T⊤TT^\top T, so its condition number is the square of that. Rathore et al. (ICML 2024) trace the difficulty of training physics-informed neural networks to this ill-conditioning. Nondimensionalizing and normalizing each input field of a surrogate model (temperature in K, pressure in Pa) is the same diagonal preconditioning as feature scaling.

    7. Common mistakes #

    • Checking the Hessian at one point. ∇2f(x⋆)⪰0\nabla^2 f(x^\star) \succeq 0 at a minimizer holds for many nonconvex functions. Convexity needs ∇2f⪰0\nabla^2 f \succeq 0 on the whole (convex) domain.
    • Strict = strong. x4x^4 and exe^x are strictly convex, but f′′f'' gets arbitrarily close to 0, so they are not strongly convex.
    • "Logistic regression is strongly convex." Only with an L2 term. Without it, μ=0\mu = 0 on Rd\R^d, and on linearly separable data there is no minimizer at all (the loss keeps decreasing as ∥w∥→∞\|w\| \to \infty).
    • Reading κ\kappa off XX. The Hessian is 1nX⊤X\frac1n X^\top X, and κ(X⊤X)=κ(X)2\kappa(X^\top X) = \kappa(X)^2, where κ(X)=s1/sd\kappa(X) = s_1/s_d is the ratio of singular values.
    • "Standardization makes κ=1\kappa = 1." Only for uncorrelated features. Correlated features keep κ=(1+∣ρ∣)/(1−∣ρ∣)\kappa = (1 + |\rho|)/(1 - |\rho|) or worse.
    • Ridge "fixes" conditioning for free. A larger λ\lambda lowers κ\kappa, but it also changes the minimizer. Feature scaling does not change the fitted function of an unregularized linear model.
    • Notation. Bubeck writes α\alpha (strong convexity) and β\beta (smoothness); Boyd and Vandenberghe write mm and MM. This track uses μ\mu and LL.

    8. Check yourself #

    1. For ∣x∣|x|, max⁡(0,x)2\max(0, x)^2 and exe^x on R\R: convex? LL-smooth? strongly convex?
      Answer∣x∣|x|: convex; not differentiable at 0, so not LL-smooth; not strongly convex. max⁡(0,x)2\max(0, x)^2: convex; LL-smooth with L=2L = 2 (its derivative 2max⁡(0,x)2\max(0, x) is 2-Lipschitz); not strongly convex (flat for x<0x < 0). exe^x: strictly convex; not LL-smooth (f′′=exf'' = e^x is unbounded); not strongly convex (f′′→0f'' \to 0 as x→−∞x \to -\infty).
    2. A quadratic has Hessian eigenvalues 0.10.1, 11 and 5050. Give LL, μ\mu, κ\kappa. What is κ\kappa after adding 0.92∥x∥2\tfrac{0.9}{2}\|x\|^2?
      AnswerL=50L = 50, μ=0.1\mu = 0.1, κ=500\kappa = 500. The new term adds 0.9I0.9I: eigenvalues 11, 1.91.9, 50.950.9, so κ=50.9\kappa = 50.9.
    3. Why does the PL inequality imply that every stationary point is a global minimizer?
      AnswerAt a point with ∇f(x)=0\nabla f(x) = 0, PL gives 0≥2μ (f(x)−f⋆)0 \ge 2\mu\,(f(x) - f^\star), so f(x)≤f⋆f(x) \le f^\star. Since always f(x)≥f⋆f(x) \ge f^\star, we get f(x)=f⋆f(x) = f^\star.
    4. Two standardized features have correlation 0.80.8. What is κ\kappa of 1nX⊤X\frac1n X^\top X? Does more rescaling of single features help?
      AnswerThe eigenvalues are 1.81.8 and 0.20.2, so κ=9\kappa = 9. No: a diagonal rescaling cannot remove a correlation. Whitening (a full-matrix preconditioner) can.
    5. Show that f(w)=∥Xw−y∥2+λ∥w∥1f(w) = \|Xw - y\|^2 + \lambda\|w\|_1 with λ≥0\lambda \ge 0 is convex without computing a Hessian.
      Answer∥⋅∥2\|\cdot\|^2 is convex, and composition with the affine map w↦Xw−yw \mapsto Xw - y keeps convexity. ∥⋅∥1\|\cdot\|_1 is a norm, hence convex (triangle inequality and homogeneity). A nonnegative weighted sum of convex functions is convex.
    6. ff is LL-smooth with L=4L = 4. Which step guarantees the largest decrease from the descent lemma, and what is that guaranteed decrease if ∥∇f(x)∥=2\|\nabla f(x)\| = 2?
      Answerη=1/L=0.25\eta = 1/L = 0.25. The guaranteed decrease is ∥∇f(x)∥2/(2L)=4/8=0.5\|\nabla f(x)\|^2/(2L) = 4/8 = 0.5.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Define a convex function.

    2. State the first-order condition for convexity.

    3. Why is a stationary point of a convex differentiable function a global minimizer?

    4. What is the second-order test for convexity?

    5. Why is the loss of a neural network with two hidden units not convex?

    6. Define LL-smooth and μ\mu-strongly convex for a C2C^2 function.

    7. State the descent lemma.

    8. What does one step with η=1/L\eta = 1/L guarantee for an LL-smooth ff?

    9. State the PL inequality.

    10. Two uncorrelated features have standard deviations 1 and 10. What is κ\kappa of the least-squares Hessian, and after standardizing?

    Open the review →

    9. Interview angle #

    • "Is the training loss of a neural network convex?" — No. Permuting hidden units gives distinct minimizers whose midpoint is in general worse. Smallest example: 12(uv−1)2\tfrac12(uv - 1)^2, with minimizers (1,1)(1, 1) and (−1,−1)(-1, -1) and value 12\tfrac12 at the midpoint.
    • "What is the condition number, and why care?" — κ=L/μ\kappa = L/\mu, the ratio of the largest to the smallest curvature. Gradient descent needs O(κlog⁡(1/ε))O(\kappa\log(1/\varepsilon)) steps.
    • "Why standardize features before gradient descent?" — It is a diagonal preconditioner that lowers κ\kappa (to about 1 for uncorrelated features). For an unregularized linear model it does not change the fitted function, only the speed.
    • "Strictly vs strongly convex?" — x4x^4 is strictly but not strongly convex. Strong convexity gives f(x)−f⋆≥μ2∥x−x⋆∥2f(x) - f^\star \ge \tfrac{\mu}{2}\|x - x^\star\|^2 and linear rates.
    • "Why is logistic regression convex?" — Its Hessian is 1n∑iσi(1−σi)xixi⊤⪰0\frac1n\sum_i \sigma_i(1 - \sigma_i)x_i x_i^\top \succeq 0.

    10. Practice #

    The notebook for this lesson runs in Colab. It has four exercises, each with a check cell and a folded solution:

    1. Test convexity on a grid with the second-order condition: logistic loss (convex) against a small two-unit network loss (not convex).
    2. Compute LL, μ\mu and κ\kappa of ridge regression on the diabetes data, with raw and with standardized features.
    3. Verify the descent lemma numerically at many random points.
    4. Verify the PL inequality for a strongly convex quadratic and find the best constant.

    11. Watch and read #

    Convex sets and functions with pictures, and why convex problems are easy.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The definition, the chord picture and examples of convex functions.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The two parabolas of section 2: the definitions of L-smooth and μ-strongly convex.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Convex functions, the first- and second-order conditions and the operations that preserve convexity.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play