Backprop, step by step: what loss.backward() actually does

Type an expression the way you would in PyTorch. The page runs it the way an autograd engine does, one node at a time and in four passes: build the graph as Python evaluates the expression left to right, sort the nodes into topological order, run the forward pass in that order, then run the backward pass in reverse. At every node, the chain rule multiplies the upstream gradient by the node's local gradient to get the downstream gradient for each input.

leaf values
Zoom in to one node
What happens at this step
Colors. Blue numbers are values from the forward pass. Orange numbers are gradients of the loss, ∂ℒ/∂(·), flowing backward; where a node feeds several others, the small orange numbers on its edges are the share each one sends back, before they are added up. Local gradients, written inside a node, are orange too: the node can work them out from its own inputs alone. After the sort, each node's output gets a small name with its number as subscript, and each input slot gets its own, so one operation can be reasoned about locally before the chain rule composes them. The letters avoid any name your expression uses.
Where this differs from PyTorch. PyTorch computes each operation the moment Python reaches it, so building the graph and the forward pass happen together. They are split here, as in the homework's autograd, so you can see the graph before any number moves. PyTorch's backward engine also never numbers the nodes: it counts how many gradients each node is still waiting for, which visits them in the same kind of order. PyTorch also keeps .grad only on leaves (call retain_grad() to keep an intermediate one) and stores plain Python numbers like 4 inside the operation instead of making nodes for them. Here every node shows its gradient, and constants are drawn as dashed boxes that get no gradient.
What you can type. + - * / **, parentheses, sin cos exp log tanh sigmoid relu sqrt abs pow square, and add sub mul div neg, each as a function (sin(x), torch.pow(x, y)) or a method (x.sin(), x.pow(y)), and several lines of assignments; the last one is the loss. Every unassigned name is a leaf tensor with requires_grad=True. Keys: → step, ← back, space play. Click a node to zoom into it, or an edge in the zoom to follow it; the dashed box on the graph shows where the zoom is looking.