Machine learning models depend on parameters. Training means finding the best parameter values.
In machine learning, most models depend on some parameters. These parameters control how the model fits the data.
In all these cases, the model form is fixed, but the parameter values must be chosen correctly. If the parameters are not correct, the model gives poor predictions even if the formula is correct.
So the main task in training a machine learning model is to find the best parameter values that give the least error on the data. This is where gradient descent is used.
In basic calculus, we often find the minimum or maximum by solving
This works when the function is simple and the number of unknowns is small. Machine learning is different because the cost function usually depends on many parameters.
A real model may have thousands or millions of parameters:
To use the derivative method, we would need to set every partial derivative equal to zero and solve a huge system of equations. That is not practical for large models.
Many modern cost functions do not give a neat closed form solution. Even if the derivatives are known, solving them exactly can be impossible.
Gradient descent improves the parameters step by step. At each step, it checks the slope and moves in the direction that reduces the cost. This makes it suitable for large datasets and complex models.
We will use a quadratic cost function and run gradient descent by hand. This is a clean exam style example.
Let the cost function be
This is a quadratic function, so it has exactly one minimum point.
Here we choose
Let the starting value be
Use \(x_{n+1} = 0.5x_n + 1.5\):
The minimum occurs where the derivative is zero:
The iteration values move toward \(3\). That is the minimum point.
A cubic function can have two turning points. One is a local maximum and the other is a local minimum. Gradient descent moves toward a minimum, so it will not give the maximum point.
Let the function be
We use the update rule
For this example, take \(\alpha = 0.10\) and start at \(x_{0} = 2\).
To run the update, we need the slope expression:
Substitute into the update:
| n | xn | 3xn2 − 3 | xn+1 = xn − 0.10(3xn2 − 3) |
|---|---|---|---|
| 0 | 2.000000 | 9.000000 | 1.100000 |
| 1 | 1.100000 | 0.630000 | 1.037000 |
| 2 | 1.037000 | 0.225107 | 1.014489 |
| 3 | 1.014489 | 0.087604 | 1.005729 |
| 4 | 1.005729 | 0.034482 | 1.002281 |
| 5 | 1.002281 | 0.013707 | 1.000910 |
| 6 | 1.000910 | 0.005464 | 1.000364 |
| 7 | 1.000364 | 0.002184 | 1.000145 |
| 8 | 1.000145 | 0.000874 | 1.000058 |
| 9 | 1.000058 | 0.000350 | 1.000023 |
| 10 | 1.000023 | 0.000140 | 0.999... (keeps moving to 1) |
Starting from \(x_{0} = 2\), the values moved toward \(x = 1\). That point is the minimum for this cubic.
Now we do the usual calculus method to see all turning points.
So the cubic has two stationary points: one at \(x = -1\) and one at \(x = 1\). Gradient descent gave us only the \(x = 1\) point because it is the minimum side from our starting position.
Gradient descent gives only the minimum value. That is why we did not get the other critical value \(x = -1\) from the iterations.
If you want the maximum of \(J(x)\), convert it into a minimum problem by defining
Minimizing \(g(x)\) is the same as maximizing \(J(x)\).