OSS4AI AI Stack · Season1 · #8

by Yujian Tang

Gradient descent & backprop — educational series cover by Yujian Tang.

An illustrated introduction to Gradient descent & backprop. Treat the headline as a topic introduction, not a universal performance or capability claim. Open full-size image

Two jobs inside training

Training a neural network means making predictions, measuring error and adjusting parameters. Two ideas are often bundled together: backpropagation computes how the loss changes with each parameter, while gradient descent uses those gradients to make an update.

A downhill walk is a useful analogy. You cannot see the entire landscape, but the local slope tells you which direction increases height. Move the other way to reduce it. In training, height is the loss, and your position is a collection of weights rather than a location on a hillside.

The analogy has limits. A real neural-network loss depends on many parameters and is not usually a single smooth valley with one obvious bottom.

Forward, loss, backward, step

The forward pass turns an input into a prediction. A loss function then measures how that prediction differs from the target. The backward pass applies the chain rule through the computation, producing a gradient for each trainable parameter.

Ordinary gradient descent updates a weight as:

w_new = w_old - learning_rate × gradient

A positive gradient says increasing the weight locally increases loss, so the update decreases the weight. A negative gradient leads to an increase. The learning rate controls the step size.

Backpropagation is not a separate source of knowledge. It is an efficient way to calculate derivatives of the operations used in the forward computation.

Local downhill-gradient analogy.

A local slope guides a gradient-based update; the full loss landscape need not be one simple valley. Open full-size image

Follow one weight numerically

Use the toy model guess = w × x, with x = 2 and target 6. Choose squared error, loss = (2w - 6)^2, and start at w = 1. The derivative is 4 × (2w - 6).

With a learning rate of 0.05, the first prediction is 2, loss is 16 and gradient is -16. The next weight is 1 - 0.05 × (-16) = 1.8.

At w = 1.8, the prediction is 3.6, loss is 5.76 and gradient is -9.6. Another step gives w = 2.28. The following step gives w = 2.568. The loss falls to about 0.75 at that weight, and the parameter approaches 3, which produces the desired prediction of 6.

These numbers describe a hand-chosen teaching problem, not a model benchmark. Their value is that every step can be checked with arithmetic.

The chain rule carries the signal backward

Now use two weights: h = a × x and guess = b × h. Set a = 0.5, b = 2, x = 3 and target = 6. The hidden value is 1.5, the prediction is 3 and squared loss is 9.

The derivative of loss with respect to the prediction is -6. Multiplying by h gives the derivative with respect to b: -9. Multiplying instead by b and x gives the derivative with respect to a: -36.

The earlier parameter receives its gradient through the later computation. Backpropagation repeats this bookkeeping through the network, rather than trying a separate perturbation for every weight.

One-weight squared-loss teaching calculation.

For guess=2w, target6 and learningrate0.05, the hand-checkable update moves w toward3. Open full-size image

Why the learning rate matters

Return to the one-weight example and raise the learning rate to 0.3. The loss grows from 16 to 31.36, then about 61.47 and 120.47. The steps overshoot the minimum and land progressively farther away.

Too small a step can make progress slow; too large a step can destabilize training. The useful range depends on the objective, parameters and optimizer. A setting that works in this example is not a recommended value for a language model.

In PyTorch, loss.backward() computes gradients and optimizer.step() applies the optimizer's update. A working loop also manages gradient clearing, batches and repeated passes. In an ordinary repeated PyTorch loop, use optimizer.zero_grad() to clear gradients between updates unless accumulation is intentional. The two calls summarize distinct jobs, not the entirety of a training program.

Learning is not ordinary inference

In standard inference, the trained weights stay fixed while the model generates an answer. Conversation history can change the input and therefore the response without changing the parameters.

Pretraining, fine-tuning and preference-based training use related optimization machinery, but differ in their data and objectives. Seeing a loss decline is not enough to establish useful behavior: evaluate on held-out tasks and inspect failures. Optimization makes the training objective smaller; it does not guarantee that the objective captures everything you care about.

Two-weight chain-rule calculation.

The chain rule carries derivatives backward through the two-weight computation. Open full-size image

Sources and scope

Reply

Avatar

or to participate