Back to learning ↗

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 →
Completed 0 / 19Continue learning →
Your learning map · See all 19 challenges

Progress stays in this browser. All lessons are open; choose your own route.

Completed 0 / 4

Follow the chain rule

A scalar prediction is enough to reveal how gradients travel. The same error is used twice when computing error × error.

Mission

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.

Select a node to inspect the chain rule.

Run backward to start tracing.

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 ↗