Gradient Descent
Using the gradient to iteratively move weights in the direction that reduces the loss.
The previous article established how to measure how wrong a model is: the loss function returns a scalar , and backpropagation computes for every weight matrix. What remains is an algorithm to act on that information — to actually move the weights in a direction that reduces the loss. That algorithm is gradient descent.
From Gradient to Update
The gradient at a given weight tells us how the loss responds to a small change in that weight. If we increase by a tiny , the loss changes by approximately . This means:
- If , increasing increases the loss — so we should decrease .
- If , increasing decreases the loss — so we should increase .
- If , the loss is locally flat with respect to .
The gradient points uphill. Subtracting it takes us downhill.
The Update Rule
The gradient descent update for a single scalar weight is:
where is the learning rate, a scalar controlling the step size. The negative sign ensures the update moves in the direction of decreasing loss.
For a weight matrix , the update applies element-wise:
Since has exactly the same shape as , this is a subtraction of two same-size matrices scaled by . Backpropagation provides for every weight matrix simultaneously, so all weights can be updated in one pass.
A Numerical Walkthrough
Take , starting at with learning rate . The gradient at any point is .
Step 1.
Step 2.
Step 3.
Each step, the gradient shrinks because is getting closer to the minimum at . The update is large when we are far from the minimum and small when we are near it.
The orange points show three successive weight positions on the loss curve. The arrows on the horizontal axis show each step moving left toward the minimum at (green point).
The Learning Rate
The learning rate has a large effect on training behavior.
Too small. Updates are tiny: many steps are needed before the loss decreases meaningfully. Training is stable but slow.
Too large. Updates overshoot the minimum. The weight jumps past it, the gradient flips sign, and the weight jumps back — causing oscillation or divergence rather than convergence.
Well-chosen. The loss decreases steadily each step and reaches a low value in a reasonable number of steps.
A good starting value is typically in the range to , varying with the problem and architecture. Finding the right value usually requires experimentation.
Convergence
Gradient descent converges when the gradient approaches zero. At that point, updates become negligible and the weights stop changing — the network has found a critical point of the loss surface.
For a convex loss (such as MSE on a linear model), the only critical point is the global minimum, and gradient descent will find it given a small enough learning rate. For the non-convex losses produced by deep networks, convergence may land at a local minimum or a saddle point, depending on the starting point and learning rate.
In practice, deep networks have loss landscapes with many local minima of similar quality. Gradient descent rarely reaches the theoretical global minimum, but it reliably finds configurations that generalize well.
Connection to Backpropagation
Gradient descent requires for every weight. Backpropagation computes exactly this in a single backward pass. The two together form a training cycle:
- Forward pass. Compute the model's output and evaluate the loss.
- Backward pass. Call
.backward()to propagate gradients to every weight. - Update. Apply the gradient descent rule to every weight matrix.
- Repeat.
Each iteration of this cycle is one gradient descent step. Running many iterations is how the loss gradually decreases and the model improves.
In Code
import torch
w = torch.tensor([4.0], requires_grad=True)
target = 2.0
eta = 0.3
for step in range(3):
loss = (w - target) ** 2 # forward pass
loss.backward() # backward pass: computes ∂L/∂w and stores it in w.grad
with torch.no_grad(): # the update itself must not be tracked in the graph
w -= eta * w.grad # update step
w.grad.zero_() # reset the gradient before the next step
print(f"step {step+1}: w={w.item():.4f} loss={(w.item()-target)**2:.4f}")
# step 1: w=2.8000 loss=0.6400
# step 2: w=2.3200 loss=0.1024
# step 3: w=2.1280 loss=0.0164
Gradient descent provides the basic mechanism for training: use the gradient to move every weight a small step downhill, and repeat until the loss is low enough. The update rule is simple, but choosing how to apply it well — adapting the step size, using momentum, handling sparse gradients — is the subject of the next article.