Two Stochastic Updates in Order
Chain two single-sample SGD updates back to back on row 0 (1, 2) and row 1 (2, 4), evaluating row 1 at the updated weight w1.
The problem
What happens when we process two samples in sequence under stochastic gradient descent? Suppose we start at w0 = 1 and visit row 0 (1, 2) followed by row 1 (2, 4). Does row 1 compute its gradient at the starting weight w0 = 1 (which gave g1 = -8 on the previous page)?
No! The defining rule of sequential stochastic updates is that row 0 immediately moves the weight from w0 to w1 = w0 - lr * g0, and row 1 then runs its forward and backward pass at that updated weight w1. Because w1 is already closer to the target slope than w0 was, row 1 sees a smaller residual and a different gradient than it would have seen at w0.
Work through the numbers
Start at w0 = 1 with learning rate lr = 0.05, and process row 0 (x0 = 1, y0 = 2) followed by row 1 (x1 = 2, y1 = 4):
Step 1 on row 0 (1, 2) at w0 = 1:
g0 = 2 * (1 * 1 - 2) * 1 = -2w1 = 1 - 0.05 * (-2) = 1 + 0.1 = 1.1
Step 2 on row 1 (2, 4) at the new weight w1 = 1.1:
yhat1 = w1 * x1 = 1.1 * 2 = 2.2g1 = 2 * (2.2 - 4) * 2 = 2 * (-1.8) * 2 = -7.2w2 = 1.1 - 0.05 * (-7.2) = 1.1 + 0.36 = 1.46
Compare g1 = -7.2 (evaluated at w1 = 1.1) with -8 (the gradient row 1 had at w0 = 1 on the previous page). Because row 0's step already nudged the weight up by +0.1, row 1's prediction rose from 2 to 2.2 and its gradient shrank in magnitude from 8 to 7.2.
The live model
The chart plots [w1, g1, w2] as you drag the lr slider from 0.02 to 0.10. At the default lr = 0.05, the values are [1.1, -7.2, 1.46].
Drag lr down to 0.02: row 0 nudges w1 to 1.04, row 1's gradient at 1.04 is g1 = -7.68, and w2 reaches 1.1936. Drag lr up to 0.10: row 0 moves w1 to 1.2, row 1's gradient shrinks to g1 = -6.4, and w2 reaches 1.84.

let lr = scene.parameter("lr", 0.05, 0.02, 0.10, 0.01);
let x0 = input.select(0, 0).select(0, 0);
let y0 = input.select(0, 0).select(0, 1);
let x1 = input.select(0, 1).select(0, 0);
let y1 = input.select(0, 1).select(0, 1);
let w0 = (x0 * 0.0 + 1.0).detach().clone().requires_grad(true);
let loss0 = (w0 * x0 - y0).square();
loss0.backward();
let w1 = (w0 - w0.grad() * lr).detach().clone().requires_grad(true);
let loss1 = (w1 * x1 - y1).square();
loss1.backward();
let g1 = w1.grad();
let w2 = w1 - g1 * lr;
let list = torch.list().add(w1.reshape(1)).add(g1.reshape(1));
let answer = torch.cat(list.add(w2.reshape(1)), 0);
out.set("w1", w1.item()).set("g1", g1.item()).set("w2", w2.item());
{
"w1": 1.1,
"g1": -7.199999999999999,
"w2": 1.46
}Your turn
After row 0 moves w from 1 to w1 = 1.1, row 1 (2, 4) sees a smaller residual 1.1 * 2 - 4 = -1.8, so its gradient is g1 = -7.2 and w2 = 1.46. The starter model reports [1.1, -8, 1.5]: it evaluates row 1's slope at the old starting weight 1.0 instead of the intermediate weight 1.1, overstepping to 1.5. Return [w1, g1, w2] = [1.1, -7.2, 1.46].

let lr = 0.05;
let x0 = input.select(0, 0).select(0, 0);
let y0 = input.select(0, 0).select(0, 1);
let x1 = input.select(0, 1).select(0, 0);
let y1 = input.select(0, 1).select(0, 1);
let w0 = (x0 * 0.0 + 1.0).detach().clone().requires_grad(true);
let loss0 = (w0 * x0 - y0).square();
loss0.backward();
let w1 = (w0 - w0.grad() * lr).detach().clone().requires_grad(true);
let g1 = (w0 * x1 - y1) * 2.0 * x1;
let w2 = w1 - g1 * lr;
let list = torch.list().add(w1.reshape(1)).add(g1.reshape(1));
let answer = torch.cat(list.add(w2.reshape(1)), 0);
out.set("w1", w1.item()).set("g1", g1.item()).set("w2", w2.item());
{
"w1": 1.1,
"g1": -8,
"w2": 1.5
}Evaluate the second sample's gradient at the updated weight after step 1 so answer holds [1.1, -7.2, 1.46].
Read the result carefully
Look at why two sequential SGD steps differ from summing the two gradients at w0. If you evaluated both gradients at w0 = 1, you would get g0(w0) = -2 and g1(w0) = -8, moving w by 0.05 * (2 + 8) = 0.5 to 1.5.
Because SGD updates w to w1 = 1.1 between the two samples, row 1's gradient is -7.2 rather than -8. Seen from the weight's side: the second update multiplies whatever the first step added by (1 - 2 * lr * x1^2), which is 0.6 here, so the first step's 0.1 survives as only 0.06 and w lands at w2 = 1.46 instead of 1.5.
Recap
- In sequential SGD, sample
0updatesw0 -> w1, and sample1evaluates its forward and backward pass atw1. - At
lr = 0.05, row0(1, 2)moveswfrom1tow1 = 1.1, which changes row1's gradient from-8(atw0) tog1 = -7.2(atw1). - Subtracting
lr * g1fromw1 = 1.1producesw2 = 1.46.