liveBook · Manning

Backpropagation

As explained in Grokking AI Algorithms, Second Edition

Backpropagation is an algorithm for computing gradients of a model's loss with respect to its parameters by propagating error derivatives backward through the model.

Backpropagation computes how a neural network’s loss changes with each of its weights and biases. It works backward from the prediction error, providing gradients that can guide updates to the parameters. Think of a coach tracing a relay team’s loss back through each handoff to see where improvements are needed. section 9.5 section 11.30

A forward pass produces a prediction; a loss measures how wrong it is. Backpropagation then uses the chain rule to trace how changes in each layer affect that loss. The chain rule combines the effects of successive steps, much like following a chain of falling dominoes. section 9.26 section 11.32

For a network with a hidden layer and a sigmoid output, the backward pass follows these steps:

  • Start at the output. Combine the prediction error with the slope of the sigmoid activation function, which describes how sensitive its output is at that point. section 9.26
  • Calculate the output-weight gradients using the hidden-layer values and the output error. A gradient collects the loss derivatives for the weights in a layer. section 9.26
  • Continue backward through the network, using the chain rule to calculate gradients for earlier parameters. An update step then adjusts the parameters using those gradients. section 11.30 section 11.32

Updating one weight

Suppose a weight is 0.5, its gradient is −0.5, and the learning rate is 0.01. Subtracting the learning rate times the gradient gives 0.5 − (0.01 × −0.5) = 0.505. The negative gradient means increasing this weight is the local direction for reducing loss; the learning rate controls the size of the step. section 12.22 section 9.26

def run_neural_network(epochs, feature_data, label_data, hidden_node_count):

   scaled_feature_data = scale_dataset(
       feature_data, FEATURE_COUNT, FEATURE_MIN, FEATURE_MAX
   ) #A

   nn = NeuralNetwork( #B
       scaled_feature_data,
       label_data,
       hidden_node_count
   )

   for epoch in range(epochs): #C
       nn.forward_propagation() #D
       nn.back_propagation() #E

       if epoch % 100 == 0:
           loss = np.mean(np.square(nn.expected_output - nn.output)) #F
           print(f"Epoch {epoch:03d}: Loss = {loss:.6f}")

   return nn

This training loop scales the data, then alternates forward propagation and backpropagation for a chosen number of epochs, reporting loss periodically. section 9.9

The same backward-pass idea applies beyond a small network. For language models, gradients pass through layers including attention and embeddings; for diffusion models, they pass through layers used to predict noise. Training repeats these calculations and updates over many steps. section 11.30 section 12.22 section 12.23

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through chain-rule gradients and a simple Python training loop, while sketching updates for a larger language model.
Examples
Uses a relay-team analogy, a single-weight update, and a neural-network training loop.
What you can do after
Build a simple neural-network training loop from scratch using forward propagation and backpropagation.
How it connects
Connects forward propagation, the chain rule, gradients, loss, and gradient descent.

Also explained in

Compare how 17 books explain backpropagation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.