Log inStart free
LESSON 6.03 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Autograd

Watch autograd record a graph, then send gradients back through it.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 6.03 · AUTOGRAD BAYL = 3.17 · 5 GRAD_FN NODES
01
Stage 1 of 6 · Record the graph
Each operation in the forward pass records a grad_fn node, named above its box. The dashed slots wait for gradients.
Explain it simply

You see the forward pass run from left to right. Each result keeps a backward node, named above its box, that knows how to send a gradient back. W and B ask for gradients, and X does not. The dashed slots under each value wait for the gradients.

A graph from left to right. Leaves x, w and b feed MulBackward0, SumBackward0, AddBackward0, MyTanhBackward and MseLossBackward0. The loss L is 3.17. Only w and b ask for a gradient.

input x, target t, node valuesw, b and your own code (Function, hook)loss and gradientsoutput ygrad_fn nodesno gradient
xrequires_grad=False0.502.00−1.00wrequires_grad=True0.50−0.300.80brequires_grad=True0.10w * xMulBackward00.25−0.60−0.80sumSumBackward0−1.15+ bAddBackward0−1.05MyTanhMyTanhBackward−0.78mseMseLossBackward03.17ctx.saved_tensorsy = −0.78forward passforward →xrequires_grad=False0.502.00−1.00wrequires_grad=True0.50−0.300.80brequires_grad=True0.10w * xMulBackward00.25−0.60−0.80sumSumBackward0−1.15+ bAddBackward0−1.05MyTanhMyTanhBackward−0.78mseMseLossBackward03.17ctx.saved_tensorsy = −0.78forward passforward →
Toy: 3 weights, a bias, 2 hand-picked examples · float64
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
L=(y−t)2,y=f(s),s=∑iwixi+b\sy{L}{grad}{L} = (\sy{y}{out}{y} - \sy{t}{data}{t})^2,\quad \sy{y}{out}{y} = f(\sy{s}{data}{s}),\quad \sy{s}{data}{s} = \textstyle\sum_i \sy{w}{param}{w_i}\sy{x}{data}{x_i} + \sy{b}{param}{b}
s=0.25−0.60−0.80+0.1=−1.05, y=−0.78, L=3.17s = 0.25 - 0.60 - 0.80 + 0.1 = -1.05,\ y = -0.78,\ L = 3.17

The loss compares f of the weighted sum with the target t.

Common mix-ups

MYTHEach backward call replaces the old gradient in .grad.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01If MyTanh is swapped for the built-in tanh, does w.grad change?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.