As we saw in Chapter 8.3, gradient descent can get stuck in a local minimum that isn’t a global minimum. Let’s revisit the example from that section.
Motivation¶
Use the buttons to change the initial guess, then drag the slider to step through the updates. Why do the two runs end at different points?
When we’re minimizing empirical risk, a local minimum that isn’t global corresponds to a parameter vector that doesn’t give us the smallest possible empirical risk. The gradient is there, so gradient descent stops updating the parameters.
In this section, we’ll study convex functions, for which every local minimum is a global minimum. We’ll also see what convexity tells us about the optimal parameters for least squares.
Formal Definition of Convexity¶
You may remember from calculus that if everywhere, then is convex. But this test requires second derivatives to exist. For instance, is convex even though it isn’t differentiable at .
Also, for a vector-to-scalar function, there isn’t a single second derivative to check. The corresponding test involves a matrix of second partial derivatives called the Hessian matrix, which we’ll introduce shortly.
We’d like a definition that works for both scalar-to-scalar and vector-to-scalar functions, whether or not they’re differentiable.
Let’s start with a scalar-to-scalar function. Pick two points on its graph and draw the line segment connecting them. For a convex function, this segment lies on or above the graph between the two points. Recall that the line through two points on a graph is called a secant line.
In each plot below, the orange segment connects the points on the graph corresponding to the inputs and . Here, is another input, not the height of the function. Drag to compare the height of the graph (blue) with the height of the segment (orange). Which function isn’t convex?
Suppose we pick two inputs and , and some .
The input that is a fraction of the way from to is .
The height of the secant line at that input is .
The segment lies on or above the graph if
For vector-to-scalar functions, we replace the scalar inputs and with vectors and .
In other words, for all inputs,
The two heights are compared at the same input, .
The inequality must hold for every pair of inputs and every . One segment that lies above the graph isn’t enough to prove convexity, but one counterexample is enough to disprove it.
For example, consider the third plot above, . If we choose , , and , the left side of the inequality is , while the right side is . So, isn’t convex.
Activity 1¶
Activity 1
Use the formal definition of convexity to prove that is convex. You may use the triangle inequality.
Solution
For any inputs and and any , the triangle inequality gives
Here, we used the fact that both and are nonnegative. This is exactly the inequality in the definition of convexity, so is convex.
Local Minimums are Global Minimums¶
What does this have to do with gradient descent?
Fact: For a convex function, every local minimum is a global minimum.
Suppose is convex, and suppose is a local minimum. We’ll prove by contradiction that is a global minimum.
Take any other input . Suppose it has a smaller function value,
Consider the line segment from to . Since is convex, the graph lies on or below this segment. For any ,
The strict inequality follows from our assumption that and the fact that .
Why is this a contradiction? By choosing as small as we want, the input can be made arbitrarily close to . But the inequality says its function value is smaller than . This contradicts the assumption that is a local minimum.
So no input can have a smaller function value, and is a global minimum.
This doesn’t mean that a convex function must have a global minimum. For instance, is convex, but approaches 0 as without ever reaching 0.
Strict Convexity¶
A convex function can have more than one minimizer. For example, the function below has a minimum value of 0, attained at every .
To rule out multiple minimizers, we can strengthen the definition of convexity.
We exclude and because the two sides are equal at the endpoints. Between the endpoints, the segment must lie strictly above the graph.
Fact: A strictly convex function has at most one global minimizer.
To prove this, suppose there are two different global minimizers, and , with the same minimum value . For any , strict convexity gives
This is a contradiction, since is the global minimum value. So, if a strictly convex function has a global minimizer, it is unique. A minimizer still doesn’t have to exist, as the example shows.
Activity 2¶
Activity 2
Is strictly convex? Use , , and to check the definition. Does having a unique minimizer imply that a function is strictly convex?
Solution
Both sides of the inequality equal , so the strict inequality fails. Thus, is convex but not strictly convex, even though its unique minimizer is .
Strict convexity guarantees uniqueness when a minimizer exists, but a function can have a unique minimizer without being strictly convex.
Strict Convexity and Mean Squared Error¶
Recall that the mean squared error for a multiple linear regression model is
When the columns of are linearly independent, there is a unique optimal parameter vector,
When the columns are linearly dependent, there are infinitely many optimal parameter vectors. We saw this in Chapter 6.4.
Let’s compare the mean squared error in two examples. On the left, we’ll use
On the right, we’ll use
In each case, the given has an MSE of 0, because and , so there is no error. The columns of on the left are linearly independent, while the columns of on the right are linearly dependent.
Drag to change the parameter vector in each plot according to
The MSE changes on the left, but stays at 0 on the right. Why?
On the right, the direction is in , so
Changing changes the parameters, but not the predictions or the mean squared error. This is a convex function with a whole line of minimizers. On the left, the mean squared error is strictly convex, and is the unique minimizer.
The Hessian matrix¶
Let’s return to the second derivative test from calculus. For a twice continuously differentiable scalar-to-scalar function , is convex if and only if for every .
For vector-to-scalar functions, we need to consider second partial derivatives. For example, suppose
Its gradient is
Each component of the gradient is a scalar-valued function of and . We can take the partial derivatives of each component:
We arrange these second partial derivatives in a matrix called the Hessian matrix.
For the example above,
For our purposes, the Hessian matrix is always symmetric: multivariable calculus rules tell us that . For now, we’ll take this as fact. There are edge cases where the second partial derivatives aren’t continuous and this equality can fail, but we won’t dwell on them here.
In this case, the Hessian doesn’t depend on . For more sophisticated functions, it could.
Convexity and the Hessian¶
For a scalar-to-scalar function, we check that . For a vector-to-scalar function, we can’t just check that each entry of the Hessian is nonnegative. Instead, we need to check a quadratic form.
Fact: A twice continuously differentiable function is convex if and only if
If the inequality is strict for every and every nonzero , then is strictly convex.
Remember that is called a quadratic form. For a fixed input , the condition that returns a nonnegative result for every possible means that is positive semidefinite. The convexity test requires this at every input .
We’ll take this convexity test as fact for now. We will revisit this idea in Chapter 9.6, when we study eigenvalues and eigenvectors.
Example: Checking Convexity¶
Consider the function
Its Hessian is . All of its entries are positive, but if we choose , then
So, isn’t convex. We can also see this by restricting to the line :
Compare this to the earlier function . The plots below show their graphs in . You can rotate each plot to examine its shape. The first is a convex bowl, while the second is a saddle.
Example: Mean Squared Error¶
In Chapter 8.2, we found that
How do we find the Hessian? The gradient is a vector-to-vector function, so we can’t apply the gradient rules from Chapter 8.2 directly to this entire expression. Instead, we’ll take the partial derivatives of its components and stack those into a matrix, as we did above. You can think of this as taking the gradient of each component of and writing its transpose as a row of the Hessian.
To simplify the notation, let and . These are both constant with respect to , so
If has columns, then has shape , and and each have components. Let’s expand the matrix-vector product:
The th component is the dot product of row of with :
To get component of the gradient, subtract component of and multiply by . By the definition of the gradient, this component is , so
Taking the partial derivative of this scalar expression with respect to leaves only the coefficient of :
This gives us entry of the Hessian. Since it holds for every and , we have
Now, for any ,
A squared norm can’t be negative, so the mean squared error for a linear model is convex in .
If has linearly independent columns, then for every nonzero . The squared norm is strictly positive, which makes the mean squared error strictly convex. Its minimizer exists because projecting onto gives a closest prediction vector, and independent columns give a unique parameter vector for that prediction.
If the columns are dependent, there is a nonzero . Moving along this direction leaves the predictions and mean squared error unchanged, as we saw in the slider example. The mean squared error is convex, but not strictly convex.
Aside: Tangent Hyperplanes¶
For a differentiable scalar-to-scalar function, there’s another way to check convexity: the graph must lie on or above every tangent line.
For vector-to-scalar functions, the tangent line becomes a tangent hyperplane. When there are two input variables, this is a plane in three dimensions.
The figure below uses . Drag the slider to change the point of tangency,
You can also rotate the plot. The orange plane touches the blue surface but never rises above it.
Suppose we fix a point . The tangent hyperplane to at is the linear approximation
This is the vector-to-scalar version of the tangent-line formula from calculus.
The graph of a differentiable convex function lies on or above every tangent hyperplane.
This gives us another useful result. If , then the inequality becomes
Fact: For a differentiable convex function, any point with zero gradient is a global minimizer. For a general differentiable function, a zero gradient could instead indicate a local maximum or a saddle point.
That still doesn’t let us choose an arbitrary step size for gradient descent. For example, on the strictly convex function , the update is . Starting from , a step size of makes the iterates alternate between 1 and -1 forever. Convexity tells us about the function’s minima, but the choice of step size still matters.