Gradient descent: Difference between revisions
Data-point summation index i to k, freeing i for the optimisation step (via update-page on MediaWiki MCP Server) |
Notation: loss is L (via update-page on MediaWiki MCP Server) |
||
| (16 intermediate revisions by the same user not shown) | |||
| Line 12: | Line 12: | ||
Let $f(\vec x)$ be a [[differentiable function]]. | Let $f(\vec x)$ be a [[differentiable function]]. | ||
Gradient descent finds a value of $\vec | Gradient descent finds a value of $\vec x_n$ s.t $f_\text{min}=f(\vec x_n)$. | ||
=== Process === | === Process === | ||
| Line 31: | Line 31: | ||
$$ | $$ | ||
f=\sum_{k \in I} f_k | f=\frac{1}{|I|}\sum_{k \in I} f_k | ||
$$ | $$ | ||
then | then, because the gradient of $f$ is the average of the per-datapoint gradients, | ||
$$ | $$ | ||
\vec | \nabla f(\vec x_i) = \frac{1}{|I|}\sum_{k \in I}\nabla f_k(\vec x_i), | ||
$$ | $$ | ||
the update is | |||
$$ | |||
\vec x_{i+1} = \vec x_i - \frac{\eta}{|I|}\sum_{k \in I} \nabla f_k(\vec x_i). | |||
$$ | |||
The factor $1/|I|$ is the size of the dataset: without it the sum would weight the per-datapoint gradients $|I|$ times as heavily as the gradient of the normalised $f$. | |||
== Mathematical example == | == Mathematical example == | ||
| Line 47: | Line 55: | ||
$$ | $$ | ||
L(x,y)=x^2+2y^2+3x-4y+6. | |||
$$ | $$ | ||
| Line 55: | Line 63: | ||
$$ | $$ | ||
\nabla | \nabla L(x,y)= | ||
\begin{pmatrix} | \begin{pmatrix} | ||
2x+3\\ | 2x+3\\ | ||
| Line 66: | Line 74: | ||
$$ | $$ | ||
\begin{cases} | \begin{cases} | ||
x_{ | x_{i+1}=x_i-0.1(2x_i+3)=0.8x_i-0.3,\\ | ||
y_{ | y_{i+1}=y_i-0.1(4y_i-4)=0.6y_i+0.4. | ||
\end{cases} | \end{cases} | ||
$$ | $$ | ||
| Line 74: | Line 82: | ||
$$ | $$ | ||
\nabla | \nabla L(0,0)= | ||
\begin{pmatrix} | \begin{pmatrix} | ||
3\\ | 3\\ | ||
| Line 95: | Line 103: | ||
{| class="wikitable" | {| class="wikitable" | ||
! $ | ! $i$ !! $x_i$ !! $y_i$ !! $\frac{\partial L}{\partial x}(x_i,y_i)$ !! $\frac{\partial L}{\partial y}(x_i,y_i)$ | ||
|- | |- | ||
| 0 || 0 || 0 || 3 || -4 | | 0 || 0 || 0 || 3 || -4 | ||
| Line 150: | Line 158: | ||
|} | |} | ||
As observed, $( | As observed, $(x_i,y_i)$ 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)$. | ||
{{End hidden}} | {{End hidden}} | ||
| Line 157: | Line 165: | ||
=== Training [[artificial neural network (ANN)]] === | === Training [[artificial neural network (ANN)]] === | ||
[[File:Trashyard Machine learning.png|thumbnail|In some sense, [[ANN]] training is like stirring up a pile of trash: improvement is made by stirring up the weight&biases matrix ever so slightly according to current gradient, recalculate the new prediction values and new gradient, and stirring up the pile again, so on so forth until finding a $\mathbf{W} | [[File:Trashyard Machine learning.png|thumbnail|In some sense, [[ANN]] training is like stirring up a pile of trash: improvement is made by stirring up the weight&biases matrix ever so slightly according to current gradient, recalculate the new prediction values and new gradient, and stirring up the pile again, so on so forth until finding a $\mathbf{W}_i$ s.t. $\forall k: L_k(\mathbf{W}_i)<L_\text{target}$.]] | ||
Let $\mathbf{W}$ be the '''weight&biases matrix''' of an [[artificial neural network (ANN)]], $S$ be the '''training data set.''', $ | Let $\mathbf{W}$ be the '''weight&biases matrix''' of an [[artificial neural network (ANN)]], $S$ be the '''training data set.''', $L_k(\mathbf{W})$ its '''loss function''' for training data point $s_k \in S$, 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 | Then '''training''' is iteration with step | ||
$$ | $$ | ||
\mathbf{W}_{ | \mathbf{W}_{i+1} = \mathbf{W}_i - \sum_{s_k \in S} \eta \nabla L_k(\mathbf{W}_i). | ||
$$ | $$ | ||
Latest revision as of 16:20, 11 November 2026
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_{i+1} = \vec x_i - \eta \nabla f(\vec x_i)$$
until $\nabla f(\vec x_n) \to \vec 0$, then $\vec x=\vec x_n$ is a local minimum of $f$.
| $\vec{x}_0$ | Initial guess |
| $\eta\in(0,1)$ | Learning rate |
| $\nabla f(\vec{x}_i)$ | Gradient of $f$ at $\vec{x}_i$ |
| $i$ | Iteration index |
| $n$ | Index at convergence |
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.
Aggregation
If
$$ f=\frac{1}{|I|}\sum_{k \in I} f_k $$
then, because the gradient of $f$ is the average of the per-datapoint gradients,
$$ \nabla f(\vec x_i) = \frac{1}{|I|}\sum_{k \in I}\nabla f_k(\vec x_i), $$
the update is
$$ \vec x_{i+1} = \vec x_i - \frac{\eta}{|I|}\sum_{k \in I} \nabla f_k(\vec x_i). $$
The factor $1/|I|$ is the size of the dataset: without it the sum would weight the per-datapoint gradients $|I|$ times as heavily as the gradient of the normalised $f$.
Mathematical example
Applications
Training artificial neural network (ANN)

Let $\mathbf{W}$ be the weight&biases matrix of an artificial neural network (ANN), $S$ be the training data set., $L_k(\mathbf{W})$ its loss function for training data point $s_k \in S$, 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}_{i+1} = \mathbf{W}_i - \sum_{s_k \in S} \eta \nabla L_k(\mathbf{W}_i). $$
