Lesson 18 of 18
Gradient Descent
Gradient Descent
Gradient descent is the most widely used optimization algorithm in machine learning. It minimizes a function by repeatedly stepping in the direction of the negative gradient (steepest descent):
where is the learning rate (step size).
Intuition
Imagine standing on a hilly landscape, trying to reach the lowest point. At each step, you look at the slope (gradient) under your feet and take a step downhill. With a small enough step size, you converge to a local minimum.
Convergence
For a convex function , the update rule gives:
The error shrinks by factor at each step. For , the factor is — the error halves approximately every 3 steps.
Example
Minimize , so :
double df(double x) { return 2.0 * (x - 3.0); }
double x = 0.0;
for (int i = 0; i < 100; i++) {
x = x - 0.1 * df(x);
}
printf("%.4f\n", x); // 3.0000
Choosing the Learning Rate
- Too large: diverges (overshoots the minimum)
- Too small: converges very slowly
- Just right: converges reliably
Your Task
Implement double grad_descent(df, start, lr, steps) that performs gradient descent on the function with derivative df.
TCC compiler loading...
Loading...
Click "Run" to execute your code.