Speed

What SGD Is and Is Not

Separate what stochastic gradient descent accomplishes on training rows from what it cannot guarantee on held-out test data or in a single epoch.

The problem

After tracing full-batch, mini-batch, and single-sample updates by hand, it is crucial to draw a sharp boundary between what stochastic gradient descent (SGD) is and what it is not:

  • What SGD IS: a scalable way to update parameters using 1 (or B) training rows per step instead of scanning all N training rows before moving w.
  • What SGD IS NOT: a guarantee that every step lowers the full-dataset loss, a one-epoch solver for the exact optimum, or a substitute for evaluating on a held-out test split.

In particular, SGD updates must only touch the training rows (i = 0, 1), while held-out test rows (i = 2) are evaluated afterward without ever contributing a gradient.

Work through the numbers

Take two training rows [(1, 2), (2, 4)] and one held-out test row (3, 5.5), starting from w0 = 0.5 with lr = 0.05:

Step 1 on Train Row 0 (1, 2) at w0 = 0.5:

  • g0 = 2 * (0.5 * 1 - 2) * 1 = -3, so w1 = 0.5 - 0.05 * (-3) = 0.65.

Step 2 on Train Row 1 (2, 4) at w1 = 0.65:

  • g1 = 2 * (0.65 * 2 - 4) * 2 = -10.8, so w = 0.65 - 0.05 * (-10.8) = 1.19.

Evaluate after the epoch at w = 1.19:

  • Training mean squared error on rows 0 and 1:
  • trainMse = ((1.19 * 1 - 2)^2 + (1.19 * 2 - 4)^2) / 2 = (0.6561 + 2.6244) / 2 = 1.64025 (down from 5.625 at w0 = 0.5).
  • Held-out test loss on row 2 (3, 5.5):
  • testLoss = (1.19 * 3 - 5.5)^2 = (-1.93)^2 = 3.7249.

The post-epoch summary vector is [w, trainMse, testLoss] = [1.19, 1.64025, 3.7249].

The live model

The chart plots [w, trainMse, testLoss] after one two-sample training epoch. Drag the lr slider from 0.02 to 0.10: at 0.02 the epoch leaves w = 0.7904 and trainMse = 3.6578, while at 0.10 it reaches w = 1.76 and trainMse = 0.144, far beyond the 0.05 default.

At lr = 0.02, [w, trainMse, testLoss] is [0.7904, 3.6578, 9.7894]. At the default lr = 0.05, it reaches [1.19, 1.64025, 3.7249]. At lr = 0.10, testLoss falls to 0.0484.

One SGD pass on train rows 0..1 reaches [w, trainMse, testLoss] = [1.19, 1.64025, 3.7249].
let lr = scene.parameter("lr", 0.05, 0.02, 0.10, 0.01);
let r0 = input.select(0, 0);
let r1 = input.select(0, 1);
let r2 = input.select(0, 2);
let w = r0.select(0, 0) * 0.0 + 0.5;
for (i in range(0, 2)) {
  let row = input.select(0, i);
  let wi = w.detach().clone().requires_grad(true);
  let loss = (wi * row.select(0, 0) - row.select(0, 1)).square();
  loss.backward();
  w = wi - wi.grad() * lr;
}
let e0 = (w * r0.select(0, 0) - r0.select(0, 1)).square();
let e1 = (w * r1.select(0, 0) - r1.select(0, 1)).square();
let trainMse = (e0 + e1) * 0.5;
let testLoss = (w * r2.select(0, 0) - r2.select(0, 1)).square();
let list = torch.list().add(w.reshape(1)).add(trainMse.reshape(1));
let answer = torch.cat(list.add(testLoss.reshape(1)), 0);
out.set("w", w.item()).set("trainMse", trainMse.item());
{
  "w": 1.19,
  "trainMse": 1.6402500000000004
}

Your turn

Training on the two training rows (1, 2) and (2, 4) moves w from 0.5 to 1.19, giving training MSE 1.64025 and held-out test loss 3.7249 on (3, 5.5). The starter model reports [1.769, 0.1334, 0.0372]: its test error looks deceptively tiny because the training loop took a third gradient step on the held-out test row itself! Restrict training updates to the two training rows so answer holds [1.19, 1.64025, 3.7249].

Starter model leaks held-out row 2 into the SGD training loop (giving w = 1.769) instead of training only on rows 0 and 1 (w = 1.19).
let lr = 0.05;
let r0 = input.select(0, 0);
let r1 = input.select(0, 1);
let r2 = input.select(0, 2);
let w = r0.select(0, 0) * 0.0 + 0.5;
for (i in range(0, 3)) {
  let row = input.select(0, i);
  let wi = w.detach().clone().requires_grad(true);
  let loss = (wi * row.select(0, 0) - row.select(0, 1)).square();
  loss.backward();
  w = wi - wi.grad() * lr;
}
let e0 = (w * r0.select(0, 0) - r0.select(0, 1)).square();
let e1 = (w * r1.select(0, 0) - r1.select(0, 1)).square();
let trainMse = (e0 + e1) * 0.5;
let testLoss = (w * r2.select(0, 0) - r2.select(0, 1)).square();
let list = torch.list().add(w.reshape(1)).add(trainMse.reshape(1));
let answer = torch.cat(list.add(testLoss.reshape(1)), 0);
out.set("w", w.item()).set("trainMse", trainMse.item());
{
  "w": 1.7690000000000001,
  "trainMse": 0.13340249999999987
}

Keep the held-out test row out of the SGD update loop so answer holds [w, trainMse, testLoss] = [1.19, 1.64025, 3.7249].

Read the result carefully

Compare what the starter bug did: when row 2 (3, 5.5) leaked into the SGD loop, it performed a third gradient step (g2 = 2 * (1.19 * 3 - 5.5) * 3 = -11.58) that pushed w to 1.769 and made the test error look artificially tiny (0.0372). Keeping test rows strictly outside .backward() is what separates genuine generalization from data leakage.

Recap

  • SGD replaces one expensive N-row gradient average with frequent 1-row or B-row updates that make rapid progress per pass.
  • SGD does not eliminate gradient variance at a fixed learning rate, nor does one epoch reach the exact minimum.
  • Only training rows may participate in .backward() and parameter updates; held-out test rows are evaluated strictly without gradients.