Skip to content

Computational Graph and Chain Rule

Any calculation, including a neural network, can be drawn as a graph of simple operations. The chain rule then gives every input's effect on the output, one node at a time.

Also chain rule · forward pass · automatic differentiation · autodiff · autograd

Fig. / How it works
Role · kind
Core ideasConcept
Level
Intermediate

A recipe drawn as a flowchart: flour and eggs go into 'mix', which goes into 'bake'. If the cake comes out too dry, you can trace back step by step to see how much each ingredient was to blame.

Where it fits

+All relationships as a list

See it on the full map

Try it

Backpropagation, one multiplication at a time
Loading interactive widget…
TakeawayBackprop is just the chain rule applied backward: each node multiplies the gradient it receives by its own local slope and passes it on.

How it works

Write any calculation as a flowchart of tiny steps and you get a computational graph. For L=(w⋅x+b−y)2L = (w \cdot x + b - y)^2: multiply, add, subtract, square. Each node takes some inputs and produces one output.

Forward pass

Evaluate the nodes from inputs to output, remembering each intermediate result. In a neural network this is “running the model”: data flows through the layers to a prediction and a loss.

Backward pass and the chain rule

To train, we need the slope of the loss with respect to every weight. The chain rule says slopes multiply along a path: if LL depends on yy and yy depends on xx,

∂L∂x=∂L∂y⋅∂y∂x.\frac{\partial L}{\partial x} = \frac{\partial L}{\partial y} \cdot \frac{\partial y}{\partial x}.

So we walk the graph backward. Each node only needs to know its own local slope; it multiplies that by the gradient arriving from above and passes the result down. When a value feeds into several nodes, the gradients coming back are added.

That procedure is backpropagation. Step through it in the widget: values flow right, gradients flow left.

Modern frameworks do this automatically (“autograd”). You write ordinary code, they record the graph during the forward pass and differentiate it for you. The cost: all those remembered intermediate values, which is why training needs much more memory than inference. Long chains of multiplications in the backward pass are also the root of vanishing and exploding gradients.

Everyday use cases

  • Case 01

    Every deep learning framework

    PyTorch, JAX and TensorFlow record your code as a computational graph and differentiate it automatically.

Strengths, weaknesses and when to use it

Strengths

  • Turns gradient computation for any model into a mechanical, efficient procedure.

Weaknesses

  • Caching every intermediate value for the backward pass is what makes training memory-hungry.

When to use itTo understand how backpropagation works and why frameworks can train arbitrary models.

Compare with

Check yourself

  1. Q01

    If y = 3x and L = y², what is dL/dx when x = 1?

    Show answer

    C: 18 dL/dy = 2y = 6 and dy/dx = 3, so dL/dx = 6 × 3 = 18.

  2. Q02

    What does the forward pass do?

    Show answer

    A: Computes the output and stores intermediate values

Go deeper