Speed

One Epoch Is One Pass

Trace one complete epoch of stochastic gradient descent across four training rows, recording the weight after each of the four updates.

The problem

In training logs and machine learning code, two words appear side by side: step (or iteration) and epoch. They are not the same thing! An epoch is one complete pass over every example in the training dataset. A step is one parameter update w <- w - lr * g.

In full-batch gradient descent, one epoch performs only one step because all N = 4 gradients are averaged before w moves. In single-sample stochastic gradient descent (SGD), one epoch over N = 4 rows performs four sequential steps: w0 -> w1 -> w2 -> w3 -> w4, where each row evaluates its gradient at the weight left behind by the previous row.

Work through the numbers

Take the four rows [(1, 2), (2, 4), (1, 3), (2, 5)], starting at w0 = 1 with learning rate lr = 0.05. Walk through all four updates of the epoch in order:

Row 0 (1, 2) at w0 = 1:

  • g0 = 2 * (1 * 1 - 2) * 1 = -2, so w1 = 1 - 0.05 * (-2) = 1.1

Row 1 (2, 4) at w1 = 1.1:

  • g1 = 2 * (1.1 * 2 - 4) * 2 = -7.2, so w2 = 1.1 - 0.05 * (-7.2) = 1.46

Row 2 (1, 3) at w2 = 1.46:

  • g2 = 2 * (1.46 * 1 - 3) * 1 = -3.08, so w3 = 1.46 - 0.05 * (-3.08) = 1.614

Row 3 (2, 5) at w3 = 1.614:

  • g3 = 2 * (1.614 * 2 - 5) * 2 = -7.088, so w4 = 1.614 - 0.05 * (-7.088) = 1.9684

Across this single four-row epoch, the weight progresses through the trajectory [1.1, 1.46, 1.614, 1.9684].

The live model

The chart plots the four weights [w1, w2, w3, w4] recorded after each row in the epoch. Drag the lr slider from 0.02 to 0.10: raising it from the default 0.05 to 0.10 stretches the same four rows from [1.1, 1.46, 1.614, 1.9684] to [1.2, 1.84, 2.072, 2.4144], because each later row inherits the larger step left by the row before it.

At lr = 0.02, the four-step trajectory reaches [1.04, 1.1936, 1.2659, 1.4633].

Weight trajectory across one 4-row epoch: w1=1.1, w2=1.46, w3=1.614, w4=1.9684.
let lr = scene.parameter("lr", 0.05, 0.02, 0.10, 0.01);
let w = input.select(0, 0).select(0, 0) * 0.0 + 1.0;
let steps = torch.list();
for (i in range(0, 4)) {
  let row = input.select(0, i);
  let x = row.select(0, 0);
  let y = row.select(0, 1);
  let wi = w.detach().clone().requires_grad(true);
  let loss = (wi * x - y).square();
  loss.backward();
  w = wi - wi.grad() * lr;
  steps.add(w.reshape(1));
}
let answer = torch.cat(steps, 0);
out.set("trajectory", answer).set("final_w", w.item());
{
  "trajectory": [
    1.1,
    1.46,
    1.6139999999999999,
    1.9684
  ],
  "final_w": 1.9684
}

Your turn

Across the four-row epoch at lr = 0.05, sequential SGD steps trace [1.1, 1.46, 1.614, 1.9684]. The starter model instead climbs all the way to [1.1, 1.5, 1.7, 2.3] because each row's gradient is evaluated at the fixed starting weight 1.0 rather than at the weight left behind by the previous row. Make each step evaluate its gradient at the current weight so answer holds [1.1, 1.46, 1.614, 1.9684].

Starter model detaches wi from the initial w0=1 on every loop iteration instead of the evolving weight w.
let lr = 0.05;
let w0 = input.select(0, 0).select(0, 0) * 0.0 + 1.0;
let w = w0;
let steps = torch.list();
for (i in range(0, 4)) {
  let row = input.select(0, i);
  let x = row.select(0, 0);
  let y = row.select(0, 1);
  let wi = w0.detach().clone().requires_grad(true);
  let loss = (wi * x - y).square();
  loss.backward();
  w = w - wi.grad() * lr;
  steps.add(w.reshape(1));
}
let answer = torch.cat(steps, 0);
out.set("trajectory", answer).set("final_w", w.item());
{
  "trajectory": [
    1.1,
    1.5,
    1.7,
    2.3
  ],
  "final_w": 2.3
}

Evaluate each row's gradient at the evolving weight across the four-step epoch so answer holds [1.1, 1.46, 1.614, 1.9684].

Read the result carefully

Look at the starter bug you just fixed: evaluating every sample's gradient at the fixed starting weight w0 = 1 and subtracting all four from w (1 - 0.05 * (-2 - 8 - 4 - 12) = 2.3) is actually full-batch gradient descent on the sum of losses! What makes an SGD epoch genuinely sequential is that wi is detached from the current w on every iteration i, so later samples in the same epoch benefit immediately from earlier updates.

Recap

  • One epoch is one complete pass over all N examples in the training dataset.
  • Under single-sample SGD, one epoch over N = 4 rows executes 4 sequential forward-backward-update steps w0 -> w1 -> w2 -> w3 -> w4.
  • On [(1, 2), (2, 4), (1, 3), (2, 5)] at lr = 0.05, one SGD epoch moves w from 1 through [1.1, 1.46, 1.614, 1.9684].