Skip to content
Training

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 LL, and backpropagation computes ∇WL\nabla_\mathbf{W} L 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 ∇wL\nabla_w L at a given weight ww tells us how the loss responds to a small change in that weight. If we increase ww by a tiny ϵ\epsilon, the loss changes by approximately ϵ⋅∂L∂w\epsilon \cdot \frac{\partial L}{\partial w}. This means:

  • If ∂L∂w>0\frac{\partial L}{\partial w} > 0, increasing ww increases the loss — so we should decrease ww.
  • If ∂L∂w<0\frac{\partial L}{\partial w} < 0, increasing ww decreases the loss — so we should increase ww.
  • If ∂L∂w=0\frac{\partial L}{\partial w} = 0, the loss is locally flat with respect to ww.

The gradient points uphill. Subtracting it takes us downhill.

The Update Rule

The gradient descent update for a single scalar weight is:

w←w−η⋅∂L∂ww \leftarrow w - \eta \cdot \frac{\partial L}{\partial w}

where η>0\eta > 0 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 W\mathbf{W}, the update applies element-wise:

W←W−η⋅∇WL\mathbf{W} \leftarrow \mathbf{W} - \eta \cdot \nabla_\mathbf{W} L

Since ∇WL\nabla_\mathbf{W} L has exactly the same shape as W\mathbf{W}, this is a subtraction of two same-size matrices scaled by η\eta. Backpropagation provides ∇WL\nabla_\mathbf{W} L for every weight matrix simultaneously, so all weights can be updated in one pass.

A Numerical Walkthrough

Take L(w)=(w−2)2L(w) = (w - 2)^2, starting at w0=4.0w_0 = 4.0 with learning rate η=0.3\eta = 0.3. The gradient at any point is ∂L∂w=2(w−2)\frac{\partial L}{\partial w} = 2(w - 2).

Step 1.

∂L∂w∣w=4.0=2(4.0−2)=4.0w1=4.0−0.3⋅4.0=2.8\frac{\partial L}{\partial w}\bigg|_{w=4.0} = 2(4.0 - 2) = 4.0 \qquad w_1 = 4.0 - 0.3 \cdot 4.0 = 2.8

Step 2.

∂L∂w∣w=2.8=2(2.8−2)=1.6w2=2.8−0.3⋅1.6=2.32\frac{\partial L}{\partial w}\bigg|_{w=2.8} = 2(2.8 - 2) = 1.6 \qquad w_2 = 2.8 - 0.3 \cdot 1.6 = 2.32

Step 3.

∂L∂w∣w=2.32=2(2.32−2)=0.64w3=2.32−0.3⋅0.64=2.128\frac{\partial L}{\partial w}\bigg|_{w=2.32} = 2(2.32 - 2) = 0.64 \qquad w_3 = 2.32 - 0.3 \cdot 0.64 = 2.128

Each step, the gradient shrinks because ww is getting closer to the minimum at w=2w = 2. 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 w=2w = 2 (green point).

The Learning Rate

The learning rate η\eta 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 10−410^{-4} to 10−110^{-1}, 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 ∇WL\nabla_\mathbf{W} L for every weight. Backpropagation computes exactly this in a single backward pass. The two together form a training cycle:

  1. Forward pass. Compute the model's output and evaluate the loss.
  2. Backward pass. Call .backward() to propagate gradients to every weight.
  3. Update. Apply the gradient descent rule to every weight matrix.
  4. 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.