Machine Learning Engineeringsimulator

Backprop step-through

Backpropagation is the chain rule, applied in an order that reuses work. The forward pass computes every value from the inputs to the loss. The backward pass then walks the same graph in reverse, and at each node multiplies the gradient arriving from the right by that node’s own local derivative.

Step 1 of 10
Forward pass: Hidden unit 1

Weight the two inputs, add the bias, then apply the sigmoid activation.

Loss L0.1916ŷ = 0.5623, t = 1
Network diagram with 2 inputs, 2 hidden units, one output and the loss. Step 1 of 10, forward pass: Hidden unit 1.InputsHiddenOutputLossForward pass: values flow left to right →0.4−0.2x₁1x₂0.5h₁0.5987h₂ŷL
Round nodes are units, the square is the loss. Edge labels show the weight going forward and ∂L/∂weight going back; the highlighted node is the one being worked on.

Forward pass

Hidden unit 1

z₁ = w₁₁·x₁ + w₁₂·x₂ + b₁

= 0.4 · 1 + (−0.2) · 0.5 + 0.1

= 0.4

h₁ = σ(z₁) = 1 / (1 + e^(−z₁))

= 1 / (1 + e^(−0.4))

= 0.5987

Network and example

Hidden units

wⱼₖ: input k to hidden unit j. bⱼ: hidden bias. vⱼ: hidden unit j to the output. c: output bias.

Every step of the forward and backward pass; the highlighted row is the step shown above
StepPassWorks on
Forward passHidden unit 1
Forward passHidden unit 2
Forward passOutput
Forward passLoss
Backward passLoss
Backward passOutput
Backward passWeight v₁ (hidden 1 to output)
Backward passWeight v₂ (hidden 2 to output)
Backward passHidden unit 1
Backward passHidden unit 2

What it shows

A network learns by finding how much each weight changes the loss. Backpropagation does this by working backwards, so each step reuses what the last one found.

This network is small enough to follow by eye: two inputs, two or three hidden units and one output. You choose the weights, activations and loss, then step through one node or edge at a time. Each step shows the rule in symbols, the same rule with numbers, and the result.

How to read it

  • The forward pass runs first. Each hidden unit computes a weighted sum z, then its activation h. The output unit does the same, and the loss comes last.
  • The backward pass starts at the loss. Each line multiplies local derivatives. The last factor is the input a weight multiplied, or 1 for a bias.
  • Round nodes are units and the square is the loss. Edge labels show the weight going forward and ∂L/∂weight going back.
  • Apply update subtracts the learning rate times each gradient from its parameter, then restarts the steps. Watch the loss fall.
  • The gradient check compares each gradient with a numerical estimate. Relative error below 1e−7 is a match. 1e−2 or more is likely a bug.

Questions

Why does backpropagation go backwards?

The loss depends on early weights only through every node after them. Starting from the loss and moving back lets each node reuse the gradient already computed for the nodes to its right, so the whole gradient costs about as much as one forward pass instead of one pass per weight.

Why does cross-entropy give ŷ − t?

With a sigmoid output and binary cross-entropy, the sigmoid’s slope ŷ(1 − ŷ) cancels the 1/(ŷ(1 − ŷ)) in the loss derivative, leaving ∂L/∂s = ŷ − t. Choose cross-entropy and compare the output step with the mean squared error one.

What does a gradient check tell me?

It compares the gradient from your backward pass with a slow numerical estimate. If the relative error is around 1e−7 or smaller, the backward pass is almost certainly right. Around 1e−2 or larger, it is probably wrong. Checks are run on small networks and then switched off.

Why does a ReLU unit sometimes show zero gradients?

ReLU has slope 0 for negative inputs, so a unit whose sum z is below zero passes no gradient back to its weights. This tool also uses slope 0 at exactly z = 0. Numerical checks near that kink can disagree slightly, which is why a ReLU error of 1e−4 is acceptable.

Learn it properly

Sources

Explore further

  • microgradAndrej Karpathy’s tiny autograd engine: backpropagation you can read in one sitting.

Last updated

More tools

All tools

Your first senior-level
lesson is free.

Choose a course, open a free lesson, and explore an idea you’ve wanted to understand.