![]() | ||
Gradient descent is a first-order iterative optimization algorithm. To find a local minimum of a function using gradient descent, one takes steps proportional to the negative of the gradient (or of the approximate gradient) of the function at the current point. If instead one takes steps proportional to the positive of the gradient, one approaches a local maximum of that function; the procedure is then known as gradient ascent.
Contents
- Description
- Examples
- Limitations
- Solution of a linear system
- Solution of a non linear system
- Comments
- Python
- MATLAB
- R
- Extensions
- Fast gradient methods
- The momentum method
- References
Gradient descent is also known as steepest descent, or the method of steepest descent. Gradient descent should not be confused with the method of steepest descent for approximating integrals.
Description
Gradient descent is based on the observation that if the multi-variable function
for
We have
so hopefully the sequence
convergence to a local minimum can be guaranteed. When the function
This process is illustrated in the adjacent picture. Here
Examples
Gradient descent has problems with pathological functions such as the Rosenbrock function shown here.
The Rosenbrock function has a narrow curved valley which contains the minimum. The bottom of the valley is very flat. Because of the curved flat valley the optimization is zig-zagging slowly with small stepsizes towards the minimum.
The "Zig-Zagging" nature of the method is also evident below, where the gradient descent method is applied to
Limitations
For some of the above examples, gradient descent is relatively slow close to the minimum: technically, its asymptotic rate of convergence is inferior to many other methods. For poorly conditioned convex problems, gradient descent increasingly 'zigzags' as the gradients point nearly orthogonally to the shortest direction to a minimum point. For more details, see the comments below.
For non-differentiable functions, gradient methods are ill-defined. For locally Lipschitz problems and especially for convex minimization problems, bundle methods of descent are well-defined. Non-descent methods, like subgradient projection methods, may also be used. These methods are typically slower than gradient descent. Another alternative for non-differentiable functions is to "smooth" the function, or bound the function by a smooth function. In this approach, the smooth problem is solved in the hope that the answer is close to the answer for the non-smooth problem (occasionally, this can be made rigorous).
Solution of a linear system
Gradient descent can be used to solve a system of linear equations, reformulated as a quadratic minimization problem, e.g., using linear least squares. The solution of
in the sense of linear least squares is defined as minimizing the function
In traditional linear least squares for real
In this case, the line search minimization, finding the locally optimal step size
For solving linear equations, gradient descent is rarely used, with the conjugate gradient method being one of the most popular alternatives. The speed of convergence of gradient descent depends on the ratio of the maximum to minimum eigenvalues of
Solution of a non-linear system
Gradient descent can also be used to solve a system of nonlinear equations. Below is an example that shows how to use the gradient descent to solve for three unknown variables, x1, x2, and x3. This example shows one iteration of the gradient descent.
Consider a nonlinear system of equations:
suppose we have the function
where
and the objective function
With initial guess
We know that
where
The Jacobian matrix
Then evaluating these terms at
So that
and
Now a suitable
Evaluating at this value,
The decrease from
Comments
Gradient descent works in spaces of any number of dimensions, even in infinite-dimensional ones. In the latter case the search space is typically a function space, and one calculates the Gâteaux derivative of the functional to be minimized to determine the descent direction.
The gradient descent can take many iterations to compute a local minimum with a required accuracy, if the curvature in different directions is very different for the given function. For such functions, preconditioning, which changes the geometry of the space to shape the function level sets like concentric circles, cures the slow convergence. Constructing and applying preconditioning can be computationally expensive, however.
The gradient descent can be combined with a line search, finding the locally optimal step size
Methods based on Newton's method and inversion of the Hessian using conjugate gradient techniques can be better alternatives. Generally, such methods converge in fewer iterations, but the cost of each iteration is higher. An example is the BFGS method which consists in calculating on every step a matrix by which the gradient vector is multiplied to go into a "better" direction, combined with a more sophisticated line search algorithm, to find the "best" value of
Gradient descent can be viewed as Euler's method for solving ordinary differential equations
Python
The gradient descent algorithm is applied to find a local minimum of the function f(x)=x4−3x3+2, with derivative f'(x)=4x3−9x2. Here is an implementation in the Python programming language.
The above piece of code has to be modified with regard to step size according to the system at hand and convergence can be made faster by using an adaptive step size. In the above case the step size is not adaptive. It stays at 0.01 in all the directions which can sometimes cause the method to fail by diverging from the minimum.
MATLAB
The following MATLAB code demonstrates a concrete solution for solving the non-linear system of equations presented in the previous section:
R
The following R code is an example of implementing gradient descent algorithm to find the minimum of the function f(x)=x4−3x3+2 in previous section. Note that we are looking for f(x)'s minimum by solving its derivative being equal to zero.
And the x can be updated with gradient descent method every iteration in the form of
where k = 1, 2, ..., maximum iteration, and α is the step size.
Extensions
Gradient descent can be extended to handle constraints by including a projection onto the set of constraints. This method is only feasible when the projection is efficiently computable on a computer. Under suitable assumptions, this method converges. This method is a specific case of the forward-backward algorithm for monotone inclusions (which includes convex programming and variational inequalities).
Fast gradient methods
Another extension of gradient descent is due to Yurii Nesterov from 1983, and has been subsequently generalized. He provides a simple modification of the algorithm that enables faster convergence for convex problems. For unconstrained smooth problems the method is called the Fast Gradient Method (FGM) or the Accelerated Gradient Method (AGM). Specifically, if the differentiable function
For constrained or non-smooth problems Nesterov's FGM is called the fast proximal gradient method (FPGM), an acceleration of the Proximal gradient method.
The momentum method
Yet another extension, that reduces the risk of getting stuck in a local minimum, as well as speeds up the convergence considerably in cases where the process would otherwise zig-zag heavily, is the momentum method, which uses a momentum term in analogy to "the mass of Newtonian particles that move through a viscous medium in a conservative force field". This method is often used as an extension to the backpropagation algorithms used to train artificial neural networks.