Gradient descent is based on the observation that if the multivariable function
F
(
x
)
is defined and differentiable in a neighborhood of a point
a
, then
F
(
x
)
decreases fastest if one goes from
a
in the direction of the negative gradient of
F
at
a
,
−
∇
F
(
a
)
. It follows that, if
a
n
+
1
=
a
n
−
γ
∇
F
(
a
n
)
for
γ
small enough, then
F
(
a
)
≥
F
(
b
)
. In other words, the term
γ
∇
F
(
a
)
is subtracted from
a
because we want to move against the gradient, namely down toward the minimum. With this observation in mind, one starts with a guess
x
0
for a local minimum of
F
, and considers the sequence
x
0
,
x
1
,
x
2
,
…
such that
x
n
+
1
=
x
n
−
γ
n
∇
F
(
x
n
)
,
n
≥
0.
We have
F
(
x
0
)
≥
F
(
x
1
)
≥
F
(
x
2
)
≥
⋯
,
so hopefully the sequence
(
x
n
)
converges to the desired local minimum. Note that the value of the step size
γ
is allowed to change at every iteration. With certain assumptions on the function
F
(for example,
F
convex and
∇
F
Lipschitz) and particular choices of
γ
(e.g., chosen either via a line search that satisfies the Wolfe conditions or the BarzilaiBorwein method shown as following),
γ
n
=
(
x
n
−
x
n
−
1
)
T
[
∇
F
(
x
n
)
−
∇
F
(
x
n
−
1
)
]


∇
F
(
x
n
)
−
∇
F
(
x
n
−
1
)


2
convergence to a local minimum can be guaranteed. When the function
F
is convex, all local minima are also global minima, so in this case gradient descent can converge to the global solution.
This process is illustrated in the adjacent picture. Here
F
is assumed to be defined on the plane, and that its graph has a bowl shape. The blue curves are the contour lines, that is, the regions on which the value of
F
is constant. A red arrow originating at a point shows the direction of the negative gradient at that point. Note that the (negative) gradient at a point is orthogonal to the contour line going through that point. We see that gradient descent leads us to the bottom of the bowl, that is, to the point where the value of the function
F
is minimal.
Gradient descent has problems with pathological functions such as the Rosenbrock function shown here.
f
(
x
1
,
x
2
)
=
(
1
−
x
1
)
2
+
100
(
x
2
−
x
1
2
)
2
.
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 zigzagging slowly with small stepsizes towards the minimum.
The "ZigZagging" nature of the method is also evident below, where the gradient descent method is applied to
F
(
x
,
y
)
=
sin
(
1
2
x
2
−
1
4
y
2
+
3
)
cos
(
2
x
+
1
−
e
y
)
.
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 nondifferentiable functions, gradient methods are illdefined. For locally Lipschitz problems and especially for convex minimization problems, bundle methods of descent are welldefined. Nondescent methods, like subgradient projection methods, may also be used. These methods are typically slower than gradient descent. Another alternative for nondifferentiable 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 nonsmooth problem (occasionally, this can be made rigorous).
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
A
x
−
b
=
0
in the sense of linear least squares is defined as minimizing the function
F
(
x
)
=
∥
A
x
−
b
∥
2
.
In traditional linear least squares for real
A
and
b
the Euclidean norm is used, in which case
∇
F
(
x
)
=
2
A
T
(
A
x
−
b
)
.
In this case, the line search minimization, finding the locally optimal step size
γ
on every iteration, can be performed analytically, and explicit formulas for the locally optimal
γ
are known.
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
A
, while the speed of convergence of conjugate gradients has a more complex dependence on the eigenvalues, and can benefit from preconditioning. Gradient descent also benefits from preconditioning, but this is not done as commonly.
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, x_{1}, x_{2}, and x_{3}. This example shows one iteration of the gradient descent.
Consider a nonlinear system of equations:
{
3
x
1
−
cos
(
x
2
x
3
)
−
3
2
=
0
4
x
1
2
−
625
x
2
2
+
2
x
2
−
1
=
0
exp
(
−
x
1
x
2
)
+
20
x
3
+
10
π
−
3
3
=
0
suppose we have the function
G
(
x
)
=
[
3
x
1
−
cos
(
x
2
x
3
)
−
3
2
4
x
1
2
−
625
x
2
2
+
2
x
2
−
1
exp
(
−
x
1
x
2
)
+
20
x
3
+
10
π
−
3
3
]
where
x
=
[
x
1
x
2
x
3
]
and the objective function
F
(
x
)
=
1
2
G
T
(
x
)
G
(
x
)
=
1
2
(
(
3
x
1
−
cos
(
x
2
x
3
)
−
3
2
)
2
+
(
4
x
1
2
−
625
x
2
2
+
2
x
2
−
1
)
2
+
(
exp
(
−
x
1
x
2
)
+
20
x
3
+
1
3
(
10
π
−
3
)
)
2
)
With initial guess
x
(
0
)
=
[
x
1
x
2
x
3
]
=
[
0
0
0
]
We know that
x
(
1
)
=
x
(
0
)
−
γ
0
∇
F
(
x
(
0
)
)
where
∇
F
(
x
(
0
)
)
=
J
G
(
x
(
0
)
)
T
G
(
x
(
0
)
)
The Jacobian matrix
J
G
(
x
(
0
)
)
J
G
=
[
3
sin
(
x
2
x
3
)
x
3
sin
(
x
2
x
3
)
x
2
8
x
1
−
1250
x
2
+
2
0
−
x
2
exp
(
−
x
1
x
2
)
−
x
1
exp
(
−
x
1
x
2
)
20
]
Then evaluating these terms at
x
(
0
)
J
G
(
x
(
0
)
)
=
[
3
0
0
0
2
0
0
0
20
]
,
G
(
x
(
0
)
)
=
[
−
2.5
−
1
10.472
]
So that
x
(
1
)
=
0
−
γ
0
[
−
7.5
−
2
209.44
]
and
F
(
x
(
0
)
)
=
0.5
(
(
−
2.5
)
2
+
(
−
1
)
2
+
(
10.472
)
2
)
=
58.456
Now a suitable
γ
0
must be found such that
F
(
x
(
1
)
)
≤
F
(
x
(
0
)
)
. This can be done with any of a variety of line search algorithms. One might also simply guess
γ
0
=
0.001
which gives
x
(
1
)
=
[
0.0075
0.002
−
0.20944
]
Evaluating at this value,
F
(
x
(
1
)
)
=
0.5
(
(
−
2.48
)
2
+
(
−
1.00
)
2
+
(
6.28
)
2
)
=
23.306
The decrease from
F
(
x
(
0
)
)
=
58.456
to the next step's value of
F
(
x
(
1
)
)
=
23.306
is a sizable decrease in the objective function. Further steps would reduce its value until a solution to the system was found.
Gradient descent works in spaces of any number of dimensions, even in infinitedimensional 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
γ
on every iteration. Performing the line search can be timeconsuming. Conversely, using a fixed small
γ
can yield poor convergence.
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
γ
.
For extremely large problems, where the computer memory issues dominate, a limitedmemory method such as LBFGS should be used instead of BFGS or the steepest descent.
Gradient descent can be viewed as Euler's method for solving ordinary differential equations
x
′
(
t
)
=
−
∇
f
(
x
(
t
)
)
of a gradient flow.
The gradient descent algorithm is applied to find a local minimum of the function f(x)=x^{4}−3x^{3}+2, with derivative f'(x)=4x^{3}−9x^{2}. 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.
The following MATLAB code demonstrates a concrete solution for solving the nonlinear system of equations presented in the previous section:
{
3
x
1
−
cos
(
x
2
x
3
)
−
3
2
=
0
4
x
1
2
−
625
x
2
2
+
2
x
2
−
1
=
0
exp
(
−
x
1
x
2
)
+
20
x
3
+
10
π
−
3
3
=
0
The following R code is an example of implementing gradient descent algorithm to find the minimum of the function f(x)=x^{4}−3x^{3}+2 in previous section. Note that we are looking for f(x)'s minimum by solving its derivative being equal to zero.
∇
f
(
x
)
=
4
x
3
−
9
x
2
=
0
And the x can be updated with gradient descent method every iteration in the form of
x
(
k
+
1
)
=
x
(
k
)
−
α
∇
f
(
x
(
k
)
)
where k = 1, 2, ..., maximum iteration, and α is the step size.
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 forwardbackward algorithm for monotone inclusions (which includes convex programming and variational inequalities).
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
F
is convex and
∇
F
is Lipschitz, and it is not assumed that
F
is strongly convex, then the error in the objective value generated at each step
k
by the gradient descent method will be bounded by
O
(
1
/
k
)
. Using the Nesterov acceleration technique, the error decreases at
O
(
1
/
k
2
)
. It is known that the rate
O
(
1
/
k
2
)
for the decrease of the cost function is optimal for firstorder optimization methods. Nevertheless there is the opportunity to improve the algorithm by reducing the constant factor. The optimized gradient method (OGM) reduces that constant by a factor of two and is an optimal firstorder method for largescale problems.
For constrained or nonsmooth problems Nesterov's FGM is called the fast proximal gradient method (FPGM), an acceleration of the Proximal gradient 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 zigzag 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.