Why the Steps Differ
Start at the exact full-batch optimum w0 = 2 for two noisy samples (1, 1) and (1, 3) to see how per-sample gradient variance makes SGD oscillate around the minimum.
The problem
Why do single-sample SGD steps differ from the full-batch step even when we are very close to—or sitting right on top of—the optimal weight?
Consider two data points sharing the same input x = 1 with different targets: Sample 0 is (1, 1) and Sample 1 is (1, 3). The mean squared error across the dataset is L(w) = ((w - 1)^2 + (w - 3)^2) / 2, which reaches its exact minimum at **w* = 2**. At w0 = 2, the full-batch gradient is zero (gBatch = 0), yet neither individual sample has zero gradient!
Work through the numbers
Start at the full-batch optimum w0 = 2 on [(1, 1), (1, 3)] with learning rate lr = 0.1:
Full-batch gradient at w0 = 2:
- Sample
0(1, 1)gradient atw0 = 2:g0 = 2 * (2 * 1 - 1) * 1 = +2 - Sample
1(1, 3)gradient atw0 = 2:g1 = 2 * (2 * 1 - 3) * 1 = -2 - Full-batch mean gradient:
gBatch = (+2 + -2) / 2 = 0
Two sequential SGD steps starting from w0 = 2:
- Step 1 on (1, 1): using
g0 = +2alone pullswdownward away from2tow1 = 2 - 0.1 * 2 = 1.8. - Step 2 on (1, 3) at w1 = 1.8: at the lower weight
1.8, Sample1has a steeper gradientg1(w1) = 2 * (1.8 * 1 - 3) * 1 = -2.4. Stepping fromw1 = 1.8pusheswpast2tow2 = 1.8 - 0.1 * (-2.4) = 2.04.
Instead of staying at w0 = 2, the two stochastic steps trace 2 -> 1.8 -> 2.04, yielding [gBatch, w1, w2] = [0, 1.8, 2.04].
The live model
The chart plots [gBatch, w1, w2]. Drag the lr slider across 0.05, 0.10, 0.15, 0.20, 0.25.
The full-batch gradient gBatch remains 0 at every learning rate. Meanwhile, [w1, w2] swings from [1.9, 2.01] at lr = 0.05 to [1.8, 2.04] at lr = 0.10, [1.7, 2.09] at lr = 0.15, [1.6, 2.16] at lr = 0.20, and [1.5, 2.25] at lr = 0.25.

let lr = scene.parameter("lr", 0.10, 0.05, 0.25, 0.05);
let w0 = input.select(0, 0);
let x0 = input.select(0, 1);
let y0 = input.select(0, 2);
let x1 = input.select(0, 3);
let y1 = input.select(0, 4);
let wb = w0.detach().clone().requires_grad(true);
let l0 = (wb * x0 - y0).square();
let l1 = (wb * x1 - y1).square();
let lb = (l0 + l1) * 0.5;
lb.backward();
let gBatch = wb.grad();
let ws0 = w0.detach().clone().requires_grad(true);
let ls0 = (ws0 * x0 - y0).square();
ls0.backward();
let w1 = ws0 - ws0.grad() * lr;
let ws1 = w1.detach().clone().requires_grad(true);
let ls1 = (ws1 * x1 - y1).square();
ls1.backward();
let w2 = ws1 - ws1.grad() * lr;
let list = torch.list().add(gBatch.reshape(1)).add(w1.reshape(1));
let answer = torch.cat(list.add(w2.reshape(1)), 0);
out.set("gBatch", gBatch.item()).set("w1", w1.item()).set("w2", w2.item());
{
"gBatch": 0,
"w1": 1.8,
"w2": 2.04
}Your turn
Starting at the full-batch optimum w0 = 2 with lr = 0.1, the first sample pulls the weight down to w1 = 1.8, and the second sample's gradient at 1.8 (-2.4) rebounds past 2 to w2 = 2.04. The starter model reports [0, 1.8, 2.0]: its second step lands right back at 2.0 because it evaluates the second sample's slope at w0 = 2.0 (-2.0) instead of at w1 = 1.8. Evaluate the second step at w1 so answer holds [0, 1.8, 2.04].

let lr = 0.10;
let w0 = input.select(0, 0);
let x0 = input.select(0, 1);
let y0 = input.select(0, 2);
let x1 = input.select(0, 3);
let y1 = input.select(0, 4);
let wb = w0.detach().clone().requires_grad(true);
let l0 = (wb * x0 - y0).square();
let l1 = (wb * x1 - y1).square();
let lb = (l0 + l1) * 0.5;
lb.backward();
let gBatch = wb.grad();
let ws0 = w0.detach().clone().requires_grad(true);
let ls0 = (ws0 * x0 - y0).square();
ls0.backward();
let w1 = ws0 - ws0.grad() * lr;
let ws1 = w0.detach().clone().requires_grad(true);
let ls1 = (ws1 * x1 - y1).square();
ls1.backward();
let w2 = w1 - ws1.grad() * lr;
let list = torch.list().add(gBatch.reshape(1)).add(w1.reshape(1));
let answer = torch.cat(list.add(w2.reshape(1)), 0);
out.set("gBatch", gBatch.item()).set("w1", w1.item()).set("w2", w2.item());
{
"gBatch": 0,
"w1": 1.8,
"w2": 2
}Evaluate the second sample's gradient at the intermediate weight w1 = 1.8 so answer holds [gBatch, w1, w2] = [0, 1.8, 2.04].
Read the result carefully
Look at the two causes working together:
- Within-step disagreement (gradient variance): at
w0 = 2,g0 = +2andg1 = -2pull in opposite directions, forcingw1 = 2 - 2 * lraway from the minimum. - Between-step drift: because
w1moved down to2 - 2 * lr, Sample1sees a larger residualw1 - 3 = -1 - 2 * lr, making its rebound step2 * lr * (1 + 2 * lr)overshoot2by4 * lr^2. Shrinkinglrover time is the standard way to quiet this oscillation.
Recap
- Even when the full-batch gradient is zero (
gBatch = 0), individual sample gradients can be non-zero (+2and-2) whenever the data points do not all lie on one line through the origin (the only lines this modelw * xcan fit). - Starting at
w0 = 2on(1, 1)and(1, 3)withlr = 0.1, oneSGDpass moveswtow1 = 1.8and thenw2 = 2.04. - Re-evaluating the second gradient at
w1creates anO(lr^2)net drift (w2 = 2 + 4 * lr^2), which shrinks rapidly aslrdecreases.