Gradient descent is an iterative optimisation algorithm used to find the local minimum of a differentiable function.
It is the fundamental algorithm used to train many machine learning models, including artificial neural network (ANN).
Mathematical definition
Goal
Let $f(\vec x)$ be a differentiable function.
Gradient descent finds a value of $\vec x_n$ s.t $f_\text{min}=f(\vec x_n)$.
Process
Start with a reasonable guess $\vec x_0$ and iteratively updates according to the rule:
$$
\vec x_{t+1} = \vec x_t - \eta \nabla f(\vec x_t)
$$
until $\nabla f(\vec x_n) \to \vec 0$, then $\vec x=\vec x_n$ is a local minimum of $f$.
where:
- $\vec x_t$ is the value of the $t^\text{th}$ iteration.
- $\eta \in (0,1)$ is the learning rate
- $\nabla f(\vec x_t)$ is the gradient of the function at $\vec x_t$.
Why $-\nabla f$: The gradient $\nabla f$ points in the direction of the steepest ascent. To minimise the function, move in the opposite direction for the steepest descent.
Gradient descent is most useful for complex functions. For simple functions setting directly $\eta \nabla f(\vec x)=0$ and solve for $\vec x$ finds local extremum candidates. Evaluate $f$ at all candidate points and take the lowest value to obtain the global minimum of $f$, which is simpler and more accurate.
Mathematical example
Find $(x,y)$ that minimises
$$
f(x,y)=x^2+2y^2+3x-4y+6.
$$
around $(0,0)$.
Solution:
$$
\nabla f(x,y)=
\begin{pmatrix}
2x+3\\
4y-4
\end{pmatrix}.
$$
Using gradient descent with learning rate $\eta=0.1$ and initial point $(x_0,y_0)=(0,0)$,
$$
\begin{cases}
x_{t+1}=x_t-0.1(2x_t+3)=0.8x_t-0.3,\\
y_{t+1}=y_t-0.1(4y_t-4)=0.6y_t+0.4.
\end{cases}
$$
At $(x_0,y_0)=(0,0)$,
$$
\nabla f(0,0)=
\begin{pmatrix}
3\\
-4
\end{pmatrix},
$$
so
$$
(x_1,y_1)=(0,0)-0.1
\begin{pmatrix}
3\\
-4
\end{pmatrix}
=(-0.3,0.4).
$$
Iterate:
| $n$ |
$x_t$ |
$y_t$ |
$\frac{\partial f}{\partial x}(x_t,y_t)$ |
$\frac{\partial f}{\partial y}(x_t,y_t)$
|
| 0 |
0 |
0 |
3 |
-4
|
| 1 |
-0.3 |
0.4 |
2.4 |
-2.4
|
| 2 |
-0.54 |
0.64 |
1.92 |
-1.44
|
| 3 |
-0.732 |
0.784 |
1.536 |
-0.864
|
| 4 |
-0.8856 |
0.8704 |
1.2288 |
-0.5184
|
| 5 |
-1.00848 |
0.92224 |
0.98304 |
-0.31104
|
| 6 |
-1.106784 |
0.953344 |
0.786432 |
-0.186624
|
| 7 |
-1.1854272 |
0.9720064 |
0.6291456 |
-0.1119744
|
| 8 |
-1.24834176 |
0.98320384 |
0.50331648 |
-0.06718464
|
| 9 |
-1.298673408 |
0.989922304 |
0.402653184 |
-0.040310784
|
| 10 |
-1.3389387264 |
0.9939533824 |
0.3221225472 |
-0.0241864704
|
| 11 |
-1.37115098112 |
0.99637202944 |
0.25769803776 |
-0.01451188224
|
| 12 |
-1.396920784896 |
0.997823217664 |
0.206158430208 |
-0.008707129344
|
| 13 |
-1.4175366279168 |
0.9986939305984 |
0.1649267441664 |
-0.0052242776064
|
| 14 |
-1.43402930233344 |
0.99921635835904 |
0.13194139533312 |
-0.00313456576384
|
| 15 |
-1.447223441866752 |
0.999529815015424 |
0.105553116266496 |
-0.001880739938304
|
| 16 |
-1.4577787534934016 |
0.9997178890092544 |
0.0844424930131968 |
-0.0011284439629824
|
| 17 |
-1.4662230027947213 |
0.9998307334055526 |
0.06755399441055744 |
-0.00067706637778944
|
| 18 |
-1.472978402235777 |
0.9998984400433316 |
0.05404319552844595 |
-0.000406239826673664
|
| 19 |
-1.4783827217886217 |
0.999939064025999 |
0.04323455642275676 |
-0.0002437438960041984
|
| 20 |
-1.4827061774308974 |
0.9999634384155994 |
0.03458764513820541 |
-0.00014624633760251904
|
| 21 |
-1.4861649419447178 |
0.9999780630493596 |
0.02767011611056433 |
-0.00008774780256151142
|
| 22 |
-1.4889319535557742 |
0.9999868378296158 |
0.02213609288845146 |
-0.00005264868153690686
|
| 23 |
-1.4911455628446194 |
0.9999921026977695 |
0.01770887431076117 |
-0.00003158920892214411
|
| 24 |
-1.4929164502756956 |
0.9999952616186617 |
0.01416709944860894 |
-0.000018953525353286468
|
| 25 |
-1.4943331602205565 |
0.999997156971198 |
0.01133367955888715 |
-0.00001137211521197188
|
As observed, $(x_t,y_t)$ converges toward $(-1.5,1)$. Directly substituting the convergence values in, one finds $\nabla f(-1.5,1)=\vec 0$. Hence $(-1.5,1)$ is indeed a local minimum around $(0,0)$.
Applications
Let $\mathbf{W}$ be the weight&biases matrix of an artificial neural network (ANN), $C(\mathbf{W})$ its cost function, which measures the error between the model's predictions and the true values in the training set (e.g., sum of squared errors (SSE)).
Then training is iteration with step
$$
\mathbf{W}_{t+1} = \mathbf{W}_t - \eta \nabla C(\mathbf{W}_t).
$$