Prediction → experiment → explanation
LearnPyTorch · Autograd Lab
See the chain rule, repair detached paths, inspect gradient accumulation, and debug a tiny training loop.
Change the controls, predict the outcome, and inspect the actual backward contributions. Every graph node is selectable; playback can be paused or stepped manually.
Tensor lab: nine lessons →Your learning map · See all 19 challenges
Tensors: get the shapes right
9 challenges · Python basics helpful
Autograd: follow the gradients
4 challenges · derivatives and training
Attention: catch the hidden bugs
6 challenges · tokens, masks and values
Progress stays in this browser. All lessons are open; choose your own route.
Follow the chain rule
A scalar prediction is enough to reveal how gradients travel. The same error is used twice when computing error × error.
Predict w.grad correctly on two different examples, then trace each backward contribution.
prediction = w * x + b
loss = (prediction - target)²
Inspect the computation graph
The graph shows contributions within this backward pass. Parameter .grad buffers accumulate across passes; non-leaf .grad is not retained in ordinary PyTorch.
A dashed link carries the forward value but blocks the backward path.
Select a node to inspect the chain rule.
Training-loop order
Watch the training loop
The loss chart uses log(1 + loss) to keep divergent and convergent runs visible. The table shows exact numeric values.
Training examples / Parameters
Run backward to start tracing.
Mission complete ✓
Continue to the next chapter: LearnPyTorch · Attention Detective →
Storage is unavailable; progress lasts for this visit.
Equivalent PyTorch code
Predict a gradient
Predict w.grad correctly on two different examples, then trace each backward contribution.
Each edge multiplies an incoming gradient by a local derivative. When a value is used twice, both contributions must be added.
What changes when x is negative? Why can a parameter receive zero gradient even when the loss is positive?
Repair detach()
Reconnect w to the loss so its gradient is computed correctly on four examples.
detach() preserves the numerical value but stops gradient tracking through that branch. Here b still receives a gradient, so backward itself can succeed.
When would intentionally detaching a target or teacher output be useful? Which model should receive gradients?
Control accumulation
Run two fresh forward/backward passes while keeping w.grad equal to the derivative from one pass.
Backward adds to leaf .grad buffers. Clear them between independent optimization steps. Deliberate microbatch accumulation is useful, but requires appropriate loss scaling.
For two unequal microbatches, how would you scale their mean losses to match the full-batch mean gradient?
Repair the training loop
Reorder the four operations so 12 steps bring mean squared error below 2% of its initial value on both the visible dataset and a second dataset.
A typical step clears old gradients, computes a fresh loss, calls backward, then updates parameters. optimizer.step() does not calculate gradients or clear them.
Why does clearing gradients immediately after backward prevent learning? Export the repaired loop and check it in real PyTorch.
A deliberately small model
This browser lab implements first-order scalar reverse-mode differentiation for +, −, ×, ÷ and detach. It is not the full PyTorch runtime: no tensor autograd, GPU, higher-order derivatives, in-place operations or arbitrary Python execution. Training uses full-batch SGD on four points. Non-leaf values in the diagram are explanatory adjoints, not automatically retained .grad fields.
Progress is stored locally when available. Experiments run on your device. Python downloads reproduce your selected settings and require PyTorch to run.
PyTorch documentation · autograd ↗PyTorch documentation · zero_grad ↗
