Skip to content
,

How Neural Networks Learn: Gradient Descent and Backpropagation Explained Simply

Gradient descent and backpropagation are how every neural network, from image classifiers to ChatGPT, learns. A plain-English guide to loss, gradients, learning rate, SGD and Adam, the chain rule behind backprop, and why GPUs and tensor cores make it fast.

5 min read

How Neural Networks Learn — cover image from TensorCore.si

Short answer

Neural networks learn with two ideas working together. Backpropagation works out how much each of the model's millions or billions of weights contributed to an error. Gradient descent then nudges every weight a small step in the direction that reduces that error. Repeated over billions of examples, these tiny adjustments turn random numbers into a model that can recognise images or write text. Almost all of the work is matrix multiplication, which is exactly what GPUs and tensor cores are built for.

Step 1: Measure the error with a loss function

A neural network makes a prediction, for example "80% cat". We compare it with the right answer using a loss function, a single number that is large when the model is wrong and small when it is right. Common choices are cross-entropy for classification and next-token prediction, and mean squared error for predicting numbers.

Training means finding the weights that make the average loss as small as possible.

Step 2: Gradient descent, walking downhill

Imagine standing on a foggy mountain and wanting to reach the valley. You cannot see far, but you can feel the slope under your feet. The sensible strategy: take a step downhill, feel the slope again, repeat.

That is gradient descent:

  • The loss landscape is the mountain: every possible setting of the weights has a height (the loss).
  • The gradient is the slope: it tells you which direction increases the loss fastest, so you step the opposite way.
  • The learning rate is your step size.

The update rule for each weight is simply:

new_weight = old_weight - learning_rate × gradient

Choosing the learning rate

  • Too large: you overshoot the valley and the loss jumps around or explodes.
  • Too small: training takes forever or gets stuck.
  • In practice: most training uses a schedule, warming up the learning rate at the start and lowering it gradually later.

Step 3: Backpropagation, finding every gradient

A large model has billions of weights. Backpropagation computes the gradient for all of them efficiently in one backward sweep:

1. Forward pass: feed the input through the network, layer by layer, to get a prediction and the loss. 2. Backward pass: starting from the loss, apply the chain rule from calculus layer by layer in reverse, calculating how much each weight affected the final error. 3. Update: use gradient descent to adjust every weight.

The chain rule is the key idea: if A affects B and B affects C, then the effect of A on C is the effect of A on B multiplied by the effect of B on C. Backprop applies this through every layer. The method was popularised for neural networks by Rumelhart, Hinton and Williams in 1986, and modern frameworks such as PyTorch do it automatically ("autograd").

Variants of gradient descent

MethodHow it worksNotes
Batch gradient descentUses the whole dataset for each stepAccurate but impractical for big data
Stochastic gradient descent (SGD)Uses one example per stepNoisy but cheap
Mini-batch SGDUses a batch (e.g. 32 to millions of tokens)The standard approach, fits GPUs well
SGD with momentumKeeps a running direction to smooth stepsFaster, less zig-zagging
Adam / AdamWAdapts the step size for each weightDefault for training transformers and LLMs

Adam keeps two extra numbers per weight (running averages of gradients and squared gradients). That is one reason training needs far more memory than running a model: roughly 16 bytes per parameter in mixed precision. See AI training vs inference and mixed precision training.

Common problems and fixes

  • Vanishing gradients: gradients shrink to almost zero in deep networks, so early layers stop learning. Fixed by ReLU-style activations, residual connections and normalisation layers.
  • Exploding gradients: gradients grow huge and destabilise training. Fixed with gradient clipping and careful initialisation.
  • Overfitting: the model memorises training data. Countered with more data, regularisation, dropout and early stopping.
  • Local minima and saddle points: in very high-dimensional networks these are less of a problem than once feared; noise from mini-batches helps escape flat regions.
  • Number precision: very small gradients can underflow in FP16, which is why BF16 and loss scaling are used. See FP16 vs BF16 vs FP8.

Why GPUs make this fast

Both the forward and backward passes are dominated by multiplying large matrices: activations times weights, and gradients times activations. The backward pass costs about twice as much as the forward pass, giving the rule of thumb that training costs about 6 FLOPs per parameter per token. GPUs with thousands of cores and tensor cores run these matrix operations massively in parallel; see how a GPU works and What is a GPU?. Large models split the work across many GPUs in a GPU cluster, averaging gradients between them after each step.

A tiny example in Python

Fitting y = 3x with one weight:

w = 0.0
lr = 0.1
data = [(1, 3), (2, 6), (3, 9)]
for step in range(50):
    grad = sum(2 * (w * x - y) * x for x, y in data) / len(data)
    w -= lr * grad
print(round(w, 3))  # approaches 3.0

Real networks do the same thing with billions of weights at once.

Frequently asked questions

Is backpropagation the same as gradient descent?

No. Backpropagation calculates the gradients; gradient descent uses them to update the weights. Training needs both.

Does the brain use backpropagation?

Probably not in the same way. Neuroscientists are still debating how the brain learns, and backprop is considered biologically unlikely in its exact form.

Do LLMs learn during a conversation?

No. Gradient descent only happens during training or fine-tuning. During a chat the weights are fixed; the model only uses what is in its context window.

Why is the learning rate so important?

It controls the size of every update. A good learning rate and schedule often matter more than other training settings.

Sources and further reading

Think you understand it now? Prove it.

Solve real Tensor Core math in the TensorCore.si Challenge and earn a certificate with your name.

Take the challenge

Leave a Reply

Your email address will not be published. Required fields are marked *