Suppose a model has a single weight and training means making the objective small. The current weight gives . Should the weight go up or down, and by how much? The derivative answers the first question near the current point and a step size answers the second, yet a step in the right direction can still make the objective worse when it is too long. This lesson builds gradient descent from these two pieces, shows how the step size decides whether it converges, and uses convexity and the Hessian to tell what kind of point the iterations can stop at.
Key terms
Objective function
Definition An objective function is a real-valued function on a set of allowed parameter vectors, chosen so that smaller values are preferred. Optimization looks for a parameter vector in with the smallest value of , and maximizing a quantity is the same as minimizing its negative. In the examples on this page , so the parameters are unconstrained. For training data with one feature, the linear predictor and the squared error give the objective
where the error is the prediction minus the target for example . This is the average of the loss over the training examples, an empirical risk. The factor only simplifies the derivatives and does not change which parameters minimize .
In plain words The objective turns “fit the training data well” into one number for each choice of parameters, and optimization can only improve what that number measures. A small or steadily falling value says that the model fits the training examples better in the chosen sense. Even a global minimum of the objective does not show that the model will predict new data well, because the expected loss on new data, the population risk, is a different quantity, estimated with data that played no part in training. The lesson on loss functions and empirical risk treats this distinction in full.
Example The objective of the opening has one parameter and its smallest value at . For a regression example, two constructed training examples have inputs and with targets and . With the objective is
At both predictions are , the errors are and , and . At both predictions are exact, so , and since is never negative this is the smallest possible value. A line can always pass through two points with different inputs, so this zero says nothing about how well the line predicts at new inputs.
Gradient descent
Definition Let be differentiable. Gradient descent with learning rate starts from a chosen point and repeats
for until a set number of steps is done or the gradient norm falls below a chosen tolerance. Every coordinate is updated at once from the gradient at the old point . The negative gradient is a descent direction. If , the function has derivative , so for every sufficiently small . Among unit vectors , the directional derivative is then smallest for , so the negative gradient is the direction of steepest descent. At a stationary point, where the gradient is , the update leaves the parameters unchanged.
In plain words At the current parameters, find the direction in which the objective rises fastest, take a step the opposite way, and repeat from the new point. The gradient is recomputed after every step because the slope changes as the parameters move. The guarantee is local. A short enough step goes downhill, but the direction alone does not say how long a step is safe.
Example For the derivative is , so . With the first update is
and the objective falls from to . The derivative at is , so and the objective is . The second step is shorter because the slope has shrunk. For the regression objective of the previous card, the chain rule gives
with the from the square canceling the . At the errors and give and . One step with uses both of these old partial derivatives and gives . The new predictions and have errors and , so the objective drops from to . More steps are needed to approach the exact fit at .
Learning rate
Definition The learning rate, or step size, is the number in the gradient descent update. The step has length , and whether the iterates settle depends on together with how fast the gradient can change. Suppose is differentiable and -smooth for some , meaning that for all and
Then for every , with ,
so every learning rate in lowers at each step where . If is also convex, in the sense of the card below, and has a global minimum , then gradient descent with satisfies
for every , so the objective values converge to the minimum value. These conditions are sufficient, not necessary, and the bound concerns objective values. It does not say which minimum the iterates approach when there are several.
In plain words The gradient says which way is downhill right here, and the learning rate says how far to trust that local information. A rate that is too small makes progress slow. A rate that is too large can carry a step so far past the minimum that the objective rises, and for a bowl such as the iterates then move farther away with every step. Where the gradient changes quickly, the safe steps are short.
Example For with any constant , write for the error after steps. Subtracting from both sides of the update gives
Each step multiplies the error by the same factor, so from any start other than the iterates converge exactly when , that is, for . For they approach from one side, at they reach it in one step, and for they jump across it every time while the error still shrinks. With and , the rate sends the iterates to , back to and so on, with the objective stuck at . The rate gives and then , and the objective climbs from to and then to , although each step pointed downhill at its starting point. Here changes by exactly per unit of , so is -smooth. The general results then promise a decrease for and the convergence bound for , and the convergence for shows that the condition is not necessary.
Local and global minima
Definition Let be a real-valued function on a set . A point in is a global minimum of if for every in . It is a local minimum if there is a radius such that for every in with . Some books call such a point a minimizer and keep the word minimum for the value . Every global minimum is a local minimum. A stationary point is a point where is differentiable and its gradient is . If a local minimum lies in the interior of and is differentiable there, it is a stationary point. The converse fails, because a stationary point can also be a local maximum or a saddle point, a stationary point that is neither. A function may have several global minima or none at all.
In plain words A local minimum is the lowest point of some neighborhood around it, and a global minimum is the lowest point of everything allowed. A zero gradient only says that the graph is flat at that spot. The spot may be a valley floor, a hilltop or a pass between two hills. If an iterate of gradient descent lands exactly on a stationary point, every later iterate stays there, so the fact that the iterates have stopped moving does not say which kind of point they found.
Example For the only stationary point is , and it is the global minimum because a square is never negative and equals there. The function has derivative at , which is its global maximum. The function also has derivative at , but it is negative to the left of and positive to the right, so is neither a minimum nor a maximum. The function has the stationary points and . Since
is nonnegative for , the point is a local minimum with . It is not global, because , and has no global minimum at all, since as .
Convex function
Definition A set is convex if it contains the whole line segment between any two of its points. A function on a convex set is convex if
for all and in and every in . For a convex function every local minimum is a global minimum. If is convex and differentiable on all of , a point is a global minimum exactly when the gradient there is . If is twice continuously differentiable on an open convex set, it is convex exactly when its Hessian is positive semidefinite at every point of that set. Convexity alone does not promise that a minimum exists, that it is unique, or that gradient descent with a particular fixed learning rate converges.
In plain words Every chord between two points of the graph lies on or above the graph, so the graph has no false bottoms. If some point had a lower value than a local minimum , the inequality would put lower values on the segment toward arbitrarily close to , which a local minimum rules out. For a convex objective a stationary point settles the optimization question. It settles nothing about prediction, since a global minimum of the training objective can still describe new data poorly. The squared-error objective of a linear model is convex in its weights and intercept, while the training objectives of neural networks with hidden layers are in general not convex.
Example For and any , and in , a short computation gives
which is never negative, so the bowl is convex. The function of the previous card is not convex. At the midpoint of and its value lies above the chord value . Only a function that is not convex can have a local minimum that is not global, as this one does. The function is convex, and every point on the line is a global minimum. Flat valleys like this one appear when a model contains the same feature twice, because then only the sum of the two weights affects the predictions. The function is convex and has no minimum at all.
Hessian matrix
Definition Let have second partial derivatives at . The Hessian matrix is the matrix with entries
If the second partial derivatives are continuous near , the matrix is symmetric. In one dimension it is the second derivative . For a twice continuously differentiable ,
with an error that is small compared with as . A symmetric matrix is positive definite if for every , and positive semidefinite if for every . At a stationary point of a twice continuously differentiable , a positive definite Hessian makes a strict local minimum, a negative definite one makes it a strict local maximum, and a Hessian with both a positive and a negative eigenvalue makes it a saddle point. If the Hessian at is semidefinite but singular, this test decides nothing.
In plain words The gradient gives the slope, and the Hessian gives how the slope changes, the curvature, in every direction. A positive value of means that the graph bends upward along . At a flat spot, upward bending in every direction makes the bottom of a bowl, and bending up in some directions and down in others makes a pass. Where the curvature is large the slope changes quickly, and that is where long steps overshoot.
Example For the regression objective , differentiating the partial derivatives once more gives the constant Hessian
and for the inputs and it is
For any direction ,
so this squared-error objective is convex. For a linear model with several features the same computation gives the average squared change in the predictions per unit step along , so its squared-error objective is convex too. With the inputs and the sum is only when and , which forces . The Hessian is positive definite, and is the unique global minimum. If all inputs were equal, the sum would vanish for some nonzero and a flat valley would appear. Since the gradient changes by between two points, is -smooth with equal to the largest eigenvalue of . The eigenvalues are about and , so the rate is below . On this quadratic, fixed steps converge from every start when and diverge from almost every start for larger rates. For a saddle, take . Its gradient is at the origin and its Hessian there has diagonal entries and , so the origin is a saddle point. The functions and both have second derivative at , yet is a minimum of the first and a maximum of the second.
Check yourself
- Gradient descent minimizes from with learning rate . Compute the derivative at the start, the next iterate, and the objective before and after the step.
Show the answer
The derivative is , so . The update gives
The objective falls from to . The step moved toward the minimum at , and the remaining error is times the starting error .
- Repeat the first step with . A classmate argues that the step must lower the objective because it follows the negative gradient. Find the new iterate and the objective, explain what went wrong, and give the learning rates for which gradient descent on this function converges from .
Show the answer
The derivative at the start is still , so the new iterate is . The objective becomes , larger than . The negative gradient only promises a decrease for steps that are short enough. This step crosses the minimum and lands away from it, farther than the starting distance .
Write for the error. Subtracting from both sides of the update gives
At the factor is , so the error goes from to to , alternating in sign and growing, and the objective climbs from to to . The starting error is not zero, so the iterates converge exactly when , that is, for . The rate of the first question gives the factor and converges.
- Find the stationary point and the Hessian of . Both diagonal entries of the Hessian are positive. Is the stationary point a minimum, and is convex?
Show the answer
The gradient is . The first entry is zero when , and then the second is , which is zero only for . So the only stationary point is the origin. The Hessian is the same at every point,
with eigenvalue for the direction and eigenvalue for the direction . Positive diagonal entries are not enough, because the Hessian has a negative eigenvalue. Along the function is , which is negative for every , while along it is . So the origin is a saddle point, not a minimum.
The function is not convex, since its Hessian is not positive semidefinite, and it has no global minimum, since decreases without bound. Gradient descent started exactly at the origin never moves. Started slightly off it along , each step multiplies the distance from the origin by , so the iterates move away.