Convexity, smoothness and the condition number
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 (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 , the smallest curvature , and their ratio . For each digit of accuracy, gradient descent needs a number of steps proportional to (lesson 03); momentum needs a number proportional to (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 and : its largest eigenvalue limits the learning rate, and preconditioners (feature scaling, Adam, Shampoo, Muon) try to make the effective smaller. Discretized differential equations have a huge , 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 everywhere: the slope never decreases.
Drag the violet chord ends , and the aqua tangent point . 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 , the logistic loss and no choice fails: they are convex. For the chord test passes everywhere, but at there is no tangent line (no derivative), only many supporting lines.
Two parabolas around the graph #
Suppose for all , with . Pick any point . Taylor's theorem puts the whole graph between two parabolas that touch it at :
The first line is an upper parabola with curvature , the second a lower parabola with curvature . The upper parabola (" curves up at most as fast as ") is -smoothness. The lower one (" curves up at least as fast as ") is -strong convexity. The important word is every: the two parabolas must hold around every point , not only at the minimum.
Drag the point on the graph and move the sliders and . The orange parabola has curvature , the blue parabola curvature ; both touch at the point. Where leaves the band, the gap is shaded magenta. For , find the smallest and the largest that work at every point (answer: , ). Then try : whatever you choose, some point far out breaks the upper bound.
Two dimensions: the condition number is the shape of the valley #
Take the quadratic with . Its curvature is 1 along and 100 along , so , and . The level sets are ellipses with axis ratio : a long, narrow valley. As in lesson 01, gradient descent must use steps small enough for the steep direction (), and then it crawls along the flat one. For a general quadratic, and are the largest and smallest eigenvalues of , and the valley points along the eigenvectors.
Move the eigenvalues , and the angle . The blue ellipses are level sets of ; read , , and the axis ratio in the panel. Think of as , 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 they are circles (). With nothing changes: the features are correlated, and rescaling each feature alone cannot remove a correlation.
A quadratic has . What is its condition number ?
is the ratio of the largest to the smallest eigenvalue, not a ratio of entries. The eigenvalues of are (eigenvector ) and (eigenvector ), so , and . Equal diagonal entries do not mean a round bowl: the valley here is tilted by .
3. Definitions and main results #
Convex sets and convex functions #
A set is convex if, for all and , the point is in : the segment between two points of stays in . Examples: balls, half-spaces , the positive semidefinite matrices, the probability simplex. The intersection of convex sets is convex.
A function is convex if its domain is convex and, for all in it and ,
The right side is the chord. is strictly convex if the inequality is strict for and .
Theorem 1 (local = global). If is convex, every local minimum is a global minimum, and the set of minimizers is convex.
Proof.
- Let be a local minimum: there is with whenever . Suppose some has .
- For let . Convexity gives .
- For , the point is inside the ball around and has a smaller value than . This contradicts step 1, so no such exists.
- If and are both minimizers with value , then : the whole segment consists of minimizers.
The first-order condition: tangents lie below #
Theorem 2 (first-order condition). Let be differentiable on an open convex set . Then is convex if and only if
for all . The right side is the tangent plane at (in 1D, the tangent line).
Proof.
- (⇒) Fix and . Convexity with gives .
- Subtract and divide by : .
- Let . The left side tends to the directional derivative .
- (⇐) Take and . Use the inequality at the point twice: and .
- Multiply the first by , the second by , and add. The gradient terms cancel, because . What is left is .
Consequence. If is convex and differentiable and , then for every : 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 be twice continuously differentiable on an open convex set . Then is convex if and only if the Hessian is positive semidefinite, , at every .
Why. Taylor's theorem with integral remainder gives
If the Hessian is PSD, the integral is , and Theorem 2 gives convexity. Conversely, on a line the function is convex, so its slope cannot decrease, and for every direction .
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 .
Switch between bowl, trough and saddle, and rotate the figure. A convex quadratic is a bowl (both eigenvalues of 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 . The Hessian is , and . Convex.
- Logistic loss with labels : with margins . With the sigmoid ,
The weights lie in , so (convex) and . The weights go to 0 as the margins grow, so no works everywhere. Adding adds to the Hessian.
Is convex, and is it -smooth?
never decreases, so is convex. -smoothness only needs a Lipschitz gradient, not a continuous second derivative: , so . It is not strongly convex, because it is flat for ().
Where convexity fails: neural networks #
The smallest example is a "network" with one weight per layer, . Both and are minimizers (), but their midpoint has . The set of minimizers, the hyperbola , is not convex, so by Theorem 1 the function is not convex. The Hessian agrees: has eigenvalues .
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 with two different hidden units has a copy 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 with (for ReLU) give the same conclusion.
Move along the segment from () to (). 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 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 with (a data term plus a regularizer);
- composition with an affine map, (any convex loss of a linear model);
- pointwise maximum , or supremum over any family (hinge loss );
- with convex and nondecreasing and convex (for example );
- partial minimization , with jointly convex and convex.
CVXPY's disciplined convex programming (DCP) rules apply these rules automatically. Products of convex functions and compositions like are not on the list.
Smoothness, strong convexity and the condition number #
Definition (-smooth). is differentiable and its gradient is -Lipschitz: for all . For twice-differentiable this is the same as for all .
Definition (-strongly convex, ). is convex. For differentiable this is the same as for all . For twice-differentiable : for all .
With both, for all . The condition number is .
- Quadratic with symmetric positive definite: the Hessian is everywhere, so and (Rayleigh quotient, linear algebra 06).
- Ridge regression with : . With the singular values of (set for when ), and . A larger lowers . With and , : convex, but not strongly convex.
Strictly convex is not strongly convex
and are strictly convex, but not strongly convex. For , ; for , as . No 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: has none).
The descent lemma #
Theorem 4 (descent lemma). If is -smooth, then for all :
No convexity is needed. This is the upper parabola of section 2, in any dimension.
Proof. Write for the points of the segment from to .
- By the fundamental theorem of calculus along the segment, .
- Subtract . The integrand becomes .
- By Cauchy–Schwarz, the integrand is at most . By the Lipschitz bound, . So the integrand is at most .
- .
Corollary (one gradient step). Put into the lemma:
The decrease is guaranteed for , and it is largest at , where . So the step 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 (, ): and . Each term on the left is at least the matching term on the right, because for every eigenvalue. So the gradient is large whenever the gap is large. This is what makes gradient descent converge fast.
Theorem 5 (Polyak–Łojasiewicz, PL). If is differentiable and -strongly convex with minimum value , then
Proof.
- Strong convexity at : for all .
- Take the minimum over of both sides. The left side becomes .
- The right side is a convex quadratic in . Its gradient is zero at , and its value there is .
- So . Rearrange.
The same argument on the descent lemma gives the upper twin, , for any -smooth with a minimum. Together they explain the stopping rule of lesson 01: a small gradient means a small gap, .
Lesson 03 combines PL with the corollary of the descent lemma: with , each step gives , a linear rate whose speed depends only on .
Which statement about the PL inequality is true?
Theorem 5 gives "strongly convex ⇒ PL". The converse fails: satisfies PL but is not convex. PL does force every stationary point to be a global minimizer (put ), but that is weaker than convexity. A convex function such as satisfies no PL inequality near 0, because is much smaller than there.
PL without convexity
PL is weaker than strong convexity. Two examples:
- has , so it is not convex. But its only stationary point is , and it satisfies PL: Karimi, Nutini and Schmidt (2016) show that works; numerically the best constant is about .
- Least squares with a singular has many minimizers (not strongly convex), but satisfies PL with equal to the smallest nonzero eigenvalue of .
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 , the least-squares Hessian is , so . Standardizing () gives a Hessian of about and .
In weight space this is a change of variables with :
- The new loss has gradient and Hessian (chain rule).
- A gradient step on is . Multiply by to map it back: .
- So gradient descent on the standardized problem is gradient descent on the original problem with one step size per coordinate, : a diagonal preconditioner.
A diagonal preconditioner cannot remove correlations. Two standardized features with correlation give the Hessian with eigenvalues , so . That needs a full-matrix preconditioner (whitening, or Newton's method in lesson 10).
Why controls first-order methods. On a quadratic, gradient descent multiplies the error along eigenvector by per step (lesson 01). The steep direction needs . Then the flat direction keeps a factor , so about steps or more are needed to reduce its error by a factor (lesson 03; momentum needs about , lesson 04).
Least squares with two centered, uncorrelated features, both with standard deviation 1 (). You multiply the second feature by 10. What is the new of the Hessian ?
The Hessian is about , so . The condition number depends on the variances, the squares of the scales. The fitted function is the same, but gradient descent needs about 100 times more steps.
Answers to section 1. (1) is convex and even strictly convex. But is unbounded, so no works on all of , and , so no works. The rates of lesson 03 do not apply: fixed-step gradient descent, , even diverges from every start with . (2) The variances are and , so before and after. The step estimate drops by a factor of about .
4. Worked example #
Least squares with samples and two centered, uncorrelated features with standard deviations 1 and 10:
and the loss is .
So and .
Step 1: the constants. The two columns are orthogonal: . So . is strongly convex with , , .
Step 2: ridge. Adding () gives the Hessian and : better conditioned, but the minimizer moves to , because ridge changes the problem.
Step 3: standardizing. Divide column 2 by its standard deviation 10. The Hessian becomes , so , and the minimizer in the new coordinates is (since ).
Step 4: the descent lemma at . . The gradient is , so and . One step with : . Then
So : the bound of the corollary holds, and it is almost tight.
Step 5: PL at . , and . ✓
Step 6: what the step did. The steep coordinate reached its optimum in one step. The flat coordinate moved only 1% of the way, from 0 to 0.01. Its error shrinks by per step, so a reduction by takes steps. After standardizing, the gradient at is , and one step with lands exactly on .
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, . Its Hessian is and , so and . 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 is the condition number of the (regularized)
feature covariance. For example, scikit-learn's documentation says that its
sagandsagasolvers 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 and . Its largest eigenvalue, the sharpness, limits the stable learning rate to about (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 grid points has , which grows like for grid spacing (about 48, 4,134 and 406,095 for = 10, 100, 1,000). A least-squares loss on the residual of the discretized equation, , has Hessian , 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. at a minimizer holds for many nonconvex functions. Convexity needs on the whole (convex) domain.
- Strict = strong. and are strictly convex, but gets arbitrarily close to 0, so they are not strongly convex.
- "Logistic regression is strongly convex." Only with an L2 term. Without it, on , and on linearly separable data there is no minimizer at all (the loss keeps decreasing as ).
- Reading off . The Hessian is , and , where is the ratio of singular values.
- "Standardization makes ." Only for uncorrelated features. Correlated features keep or worse.
- Ridge "fixes" conditioning for free. A larger lowers , but it also changes the minimizer. Feature scaling does not change the fitted function of an unregularized linear model.
- Notation. Bubeck writes (strong convexity) and (smoothness); Boyd and Vandenberghe write and . This track uses and .
8. Check yourself #
- For , and on : convex? -smooth? strongly convex?
Answer
: convex; not differentiable at 0, so not -smooth; not strongly convex. : convex; -smooth with (its derivative is 2-Lipschitz); not strongly convex (flat for ). : strictly convex; not -smooth ( is unbounded); not strongly convex ( as ). - A quadratic has Hessian eigenvalues , and . Give , , . What is after
adding ?
Answer
, , . The new term adds : eigenvalues , , , so . - Why does the PL inequality imply that every stationary point is a global minimizer?
Answer
At a point with , PL gives , so . Since always , we get . - Two standardized features have correlation . What is of ? Does more
rescaling of single features help?
Answer
The eigenvalues are and , so . No: a diagonal rescaling cannot remove a correlation. Whitening (a full-matrix preconditioner) can. - Show that with is convex without computing a Hessian.
Answer
is convex, and composition with the affine map keeps convexity. is a norm, hence convex (triangle inequality and homogeneity). A nonnegative weighted sum of convex functions is convex. - is -smooth with . Which step guarantees the largest decrease from the descent lemma, and
what is that guaranteed decrease if ?
Answer
. The guaranteed decrease is .
Define a convex function.
For all and : . The chord lies on or above the graph.
State the first-order condition for convexity.
A differentiable is convex if and only if for all : every tangent plane lies below the graph.
Why is a stationary point of a convex differentiable function a global minimizer?
The first-order condition with gives for every .
What is the second-order test for convexity?
A function on an open convex set is convex if and only if at every point .
Why is the loss of a neural network with two hidden units not convex?
Swapping the two units gives a second minimizer with the same loss; at the midpoint the units are equal and the loss is in general higher, so the set of minimizers is not convex.
Define -smooth and -strongly convex for a function.
-smooth: everywhere. -strongly convex: everywhere, with .
State the descent lemma.
If is -smooth: for all .
What does one step with guarantee for an -smooth ?
.
State the PL inequality.
for all . Every -strongly convex function satisfies it.
Two uncorrelated features have standard deviations 1 and 10. What is of the least-squares Hessian, and after standardizing?
About before, and about 1 after.
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: , with minimizers and and value at the midpoint.
- "What is the condition number, and why care?" — , the ratio of the largest to the smallest curvature. Gradient descent needs steps.
- "Why standardize features before gradient descent?" — It is a diagonal preconditioner that lowers (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?" — is strictly but not strongly convex. Strong convexity gives and linear rates.
- "Why is logistic regression convex?" — Its Hessian is .
10. Practice #
The notebook for this lesson runs in Colab. It has four exercises, each with a check cell and a folded solution:
- Test convexity on a grid with the second-order condition: logistic loss (convex) against a small two-unit network loss (not convex).
- Compute , and of ridge regression on the diabetes data, with raw and with standardized features.
- Verify the descent lemma numerically at many random points.
- Verify the PL inequality for a strongly convex quadratic and find the best constant.
11. Watch and read #
- Boyd and Vandenberghe, Convex Optimization, free PDF: §2.1–2.3 (convex sets), §3.1.3–3.1.4 (first- and second-order conditions), §3.2 (operations that preserve convexity), §9.1.2 (strong convexity, written with and ).
- Garrigos and Gower, Handbook of Convergence Theorems for (Stochastic) Gradient Methods, §2: the first-order condition, strong convexity, the descent lemma and PL, with proofs.
- Karimi, Nutini and Schmidt, Linear Convergence of Gradient and Proximal-Gradient Methods Under the Polyak–Łojasiewicz Condition (2016): PL and its relation to other conditions.
- CVXPY, Disciplined Convex Programming: the composition rules in practice.
- Rathore, Lei, Frangella, Lu and Udell, Challenges in Training PINNs: A Loss Landscape Perspective (ICML 2024): ill-conditioning in physics-informed losses.
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.
No Google account needed: Python runs inside your browser. The first start downloads about 40 MB; your changes are saved in this browser only.