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

The PyTorch training step

Watch one training step clear, fill and use the gradients, then resume from a checkpoint.


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.04 · TRAINING BAYSTEP 3 OF 30 · ADAM · 3 PARAMS, 3 BUFFERS
01
Stage 1 of 6 · The modules
nn.Linear holds weight (w₁, w₂) and bias b; the norm module holds 3 buffers. The Adam optimizer keeps m, v and a step count.
Explain it simply

You see the two modules of a tiny PyTorch program. The model box holds three weights that training changes. The norm box holds a running mean and variance: saved numbers that no gradient changes. The optimizer box below keeps its own notes about each weight.

Before step 3: weights w1 −0.39, w2 −0.53, bias b 0.21. The norm module keeps a mean, a variance and a count of 8. The optimizer is Adam.

x, x̂, y, buffersparametersgrads, loss, optimizer stateŷNone, not saved
normmeanvarcount92.4869.00.6350.0478model = nn.Linear(2, 1)w₁w₂bparam.grad−0.390−2.34−0.532−1.670.210−2.48opt = torch.optim.Adam(lr=0.05)m (exp_avg)v (exp_avg_sq)changestep t = 2−0.4400.0107−0.4040.0097−0.0840.0095the modulesŷ = −0.39·x̂₁ − 0.53·x̂₂ + 0.21normmeanvarcount92.4869.00.6350.0478model = nn.Linear(2, 1)w₁w₂bparam.grad−0.390−2.34−0.532−1.670.210−2.48opt = torch.optim.Adam(lr=0.05)m (exp_avg)v (exp_avg_sq)changestep t = 2−0.4400.0107−0.4040.0097−0.0840.0095the modulesŷ = −0.39·x̂₁ − 0.53·x̂₂ + 0.21
Toy: 8 examples, nn.Linear(2, 1), 30 steps
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
y^=w1 x^1+w2 x^2+b\sy{yh}{out}{\hat y} = \sy{w}{param}{w_1}\,\sy{xh}{data}{\hat x_1} + \sy{w}{param}{w_2}\,\sy{xh}{data}{\hat x_2} + \sy{w}{param}{b}
y^=−0.39 x^1−0.53 x^2+0.21\hat y = -0.39\,\hat x_1 - 0.53\,\hat x_2 + 0.21

nn.Linear weights each scaled input and adds the bias b.

Common mix-ups

MYTHbackward() replaces the old gradients.

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

Predict, then reveal
3 CHECKS · PRO
Q01If zero_grad() never runs, is w₁.grad at step 3 further from zero or closer?

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.