Convex Optimization
For general functions, gradient descent may stop at a poor local minimum. For convex functions that cannot happen: every local minimum is a global minimum. Linear regression, logistic regression, ridge, lasso and SVMs all have convex training problems, which is why they train reliably; neural networks do not, which is why their training is an art.
Convex sets and functions
A set is convex if the segment between any two of its points stays inside: for , . A function is convex if its graph lies below every chord:
Equivalent tests for smooth :
| Test | Condition |
|---|---|
| first order | : tangent planes lie below the graph |
| second order | the Hessian is positive semidefinite everywhere |
Building convex functions
Convexity is preserved by non-negative weighted sums, by composing with an affine map (), and by pointwise maxima. Basic convex functions: (any norm), , , , , . From these:
- least squares : norm squared of an affine map;
- logistic loss : sum of a convex function of affine maps;
- hinge loss plus (the SVM): sum of convex pieces.
import numpy as np
rng = np.random.default_rng(0)
X = rng.normal(size=(50, 3)); y = np.sign(rng.normal(size=50))
def logistic_loss(t): return np.sum(np.log1p(np.exp(-y * (X @ t))))
def hessian(t):
s = 1 / (1 + np.exp(-y * (X @ t)))
return X.T @ (X * (s * (1 - s))[:, None]) # X^T diag(s(1-s)) X
worst = min(np.linalg.eigvalsh(hessian(rng.normal(size=3) * 3)).min() for _ in range(200))
print("smallest Hessian eigenvalue over 200 random points:", f"{worst:.4f}", "(>= 0: convex)")
# chord test along random segments
ok = True
for _ in range(1000):
a, b, th = rng.normal(size=3) * 3, rng.normal(size=3) * 3, rng.uniform()
ok &= logistic_loss(th * a + (1 - th) * b) <= th * logistic_loss(a) + (1 - th) * logistic_loss(b) + 1e-9
print("chord inequality held 1000 times:", bool(ok))smallest Hessian eigenvalue over 200 random points: 0.0391 (>= 0: convex)
chord inequality held 1000 times: True
Why convexity is so valuable
- Local is global. If for convex , then by the first-order test for every .
- Convergence guarantees. Gradient descent provably reaches ; with strong convexity it converges at a linear (geometric) rate.
- Duality is tight under mild conditions, so dual solvers (as for SVMs) find the true optimum.
A non-convex example shows what can go wrong:
import numpy as np
f = lambda x: x**4 - 4 * x**2 + x # two valleys
df = lambda x: 4 * x**3 - 8 * x + 1
for x0 in (-2.0, 2.0):
x = x0
for _ in range(500):
x -= 0.01 * df(x)
print(f"start {x0:+.0f}: ends at x = {x:.4f}, f = {f(x):.4f}")start -2: ends at x = -1.4730, f = -5.4442
start +2: ends at x = 1.3470, f = -2.6186
Depending on the start, gradient descent finds a different minimum, and only one of them is global.
In software
cvxpylets you state convex problems declaratively and checks convexity by these composition rules ("disciplined convex programming").- scikit-learn's
LogisticRegression,Ridge,LassoandLinearSVCsolve convex problems, so their results do not depend on initialization. - Deep networks are non-convex; random initialization, SGD noise and over-parameterization make training work in practice anyway.
Exercises
Exercise 1: Convex or not?
Which are convex on ? (a) ; (b) ; (c) ; (d) .
Answer
(a) and (c).
Solution
(a) has second derivative . (b) has , which changes sign. (c) is a norm of an affine map. (d) has second derivative : it is concave.
Exercise 2: A convex set?
Is convex?
Answer
No.
Solution
and are in the set, but their midpoint is not. (The unit ball is convex.)
Exercise 3: Hessian test (code)
Is convex? Check the Hessian's eigenvalues.
Answer
Yes: eigenvalues 1 and 3.
Solution
is constant with positive eigenvalues, so is (strongly) convex.
import numpy as np
print(np.linalg.eigvalsh(np.array([[2.0, 1.0], [1.0, 2.0]])))[1. 3.]
Test yourself
For a convex differentiable function, a point with zero gradient is…
- possibly a saddle point
- a global minimum
- a maximum
- a local minimum that may not be global
Which training problem is NOT convex?
- Least squares
- Logistic regression
- A two-layer neural network with tanh units
- Ridge regression
A twice-differentiable function is convex if…
- its Hessian is positive semidefinite everywhere
- its gradient is zero somewhere
- it is bounded
- its Hessian is diagonal