The Path to the Valley: Gradient Descent
How gradient descent finds the lowest point in a landscape with millions of hills — most of the time.
Every neural network starts with random weights — essentially random guesses. The entire training process is about turning those guesses into useful values. But how? The answer is an algorithm so simple it can be explained in one sentence: compute which direction is downhill, take a step.
This article explains gradient descent — the engine behind virtually all modern AI training — and why the seemingly minor detail of step size can make or break your model.
Core Thesis
Gradient descent is an optimization algorithm that iteratively adjusts model parameters by moving in the direction opposite to the gradient of the loss function. The size of each step is controlled by the learning rate — the single most important hyperparameter in deep learning. The algorithm's behavior in the complex, high-dimensional loss landscapes of real neural networks is governed by the interplay between gradient direction, step size, and noise.
The Algorithm — Downhill in the Fog
Gradient Descent
Limitation: On a real mountain, you can feel the slope perfectly. In high-dimensional parameter spaces, the gradient is computed from noisy data samples, making the measurement imprecise. Also, real mountains typically have one valley — loss landscapes can have many deceptive dips and saddle ridges.
The Update Rule
The Update Rule
Worked Example: Step by Step
Consider the loss function L(w) = (w - 3)² + 1 with starting weight w = 0 and learning rate 0.1. The gradient here is the first derivative: dL/dw = 2(w - 3). The minimum is at w = 3 where L = 1.
Three Variants of Gradient Descent
Uses the ENTIRE dataset to compute the gradient at each step. Advantage: stable, exact gradient. Disadvantage: slow with large datasets, gets stuck at saddle points because the gradient is exactly zero there.
Uses a SINGLE random sample per step. Advantage: fast, noisy updates help escape saddle points (more on this in the Loss Landscape section). Disadvantage: very noisy — the path to the minimum zigzags wildly.
In practice, Mini-Batch Gradient Descent is the golden middle and today's standard: it uses a small batch (typically 32-256 samples) per step. This combines the stability of Batch GD with the speed and noise benefits of SGD.
Interactive: Gradient Descent Step by Step
Click through the five steps and watch the ball roll down the loss curve. Notice how the steps get smaller as the ball approaches the minimum — the gradient becomes flatter.
The ball starts far left of the minimum. The loss is high (L=105.8). The gradient points steeply downhill to the right — the algorithm knows which direction to go.
The Dial — Learning Rate
The learning rate is a number set by the developer that controls how large each step is. It is the single most important hyperparameter in deep learning. It is NOT learned by the algorithm — the developer must choose it.
Three Scenarios — Same Function, Different Step Sizes
w = 0 → 9.0 → -9.0 → ... The weights oscillate wildly and diverge. The loss explodes instead of decreasing. The model has learned nothing.
w = 0 → 0.06 → 0.12 → ... The weights barely move. After hundreds of steps, the model is still far from the minimum. Training takes forever.
Just Right (lr = 0.5)
Common Misconception
Interactive: Computation Cost per Gradient Step
Move the slider to change the dataset size. Observe how the computation per step differs between SGD (single sample), mini-batch, and full-batch GD. This is why mini-batch is the standard.
At n=100, the difference becomes visible: O(n²) requires 10.000 operations, while O(n) needs only 100. O(log n) needs just 6.6 — that's 15x less than O(n).
Ratio to O(n)
| Complexity | Operations | Factor vs. O(n) |
|---|---|---|
| SGD (1 Sample) | 1 | 100x faster |
| Mini-batch | 6.6 | 15x faster |
| Full-batch GD | 100 | 1x (Reference) |
The Landscape — Saddle Points, Local Minima, Plateaus
The loss function of a real neural network with millions of parameters creates a complex, high-dimensional landscape. This landscape contains three types of obstacles where the gradient approaches zero — but for very different reasons.
Landscape Topology
Key insight: In high-dimensional spaces (millions of parameters), a true local minimum requires ALL dimensions to simultaneously curve upward. This is statistically extremely unlikely. Saddle points, where some dimensions curve up and others down, are far more common — and practically more dangerous.
Common Misconception
Common Misconception
Deep Dive: The Adam Optimizer
Deep Dive: 10-Step Computation
What Comes Next
Gradient descent tells us HOW to update weights — but for a network with many layers, we need to know how errors propagate backward through the network to compute the gradient for each weight. That is backpropagation — the subject of the next article, which builds on the chain rule.
Backpropagation Algorithm
The birth of modern machine learning through an elegant training algorithm. In October 1986, David Rumelhart, Geoffrey Hinton, and Ronald Williams published the paper 'Learning representations by back-propagating errors' in Nature. This algorithm considerably changed the training of neural networks by providing an efficient method for adjusting weights in multi-layer networks. The procedure repeatedly adjusts the connection weights to minimize the difference between actual and desired output. The decisive innovation lay in the ability to train hidden layers that automatically recognize important features of the task. The mathematical foundations had already been derived earlier — by Paul Werbos (1974) and Seppo Linnainmaa (1970), among others — but it was this paper that made backpropagation widely known and convincingly demonstrated its effectiveness. Backpropagation became the workhorse of machine learning and today enables all modern deep learning applications.
Takeaways
Knowledge Check: Gradient Descent
Self-Check
- Why is there a minus sign in the update rule — and what would happen if you added the gradient instead?
- Why does a learning rate that is too large not simply produce a slightly less accurate result, but can cause the model to diverge entirely?
- Imagine your algorithm is stuck at a saddle point. Why is it a major advantage if your compass (the gradient) wobbles slightly?