Speed

Side-by-Side Updates

Compare full-batch, mini-batch, and single-sample gradient descent across one pass over the same four training rows.

The problem

We now have three ways to spend one epoch of gradient computation across N = 4 training rows:

  • Full-batch (B = 4): average all 4 gradients at w0 and take 1 update step.
  • Mini-batch (B = 2): average 2 gradients at a time and take 2 sequential update steps.
  • Single-sample SGD (B = 1): use 1 row's gradient at a time and take 4 sequential update steps.

All three methods differentiate the exact same four squared-error functions (w * x_i - y_i)^2 once per epoch. Yet because they update w at different frequencies inside the pass, they finish the epoch at three distinct weights [wBatch, wMini, wSgd].

Work through the numbers

Start all three optimizers at w0 = 1 with lr = 0.05 on the four rows [(1, 2), (2, 4), (1, 3), (2, 5)]:

Full-batch (1 step):

  • At w0 = 1, the four sample gradients are [-2, -8, -4, -12]. Their mean is gBatch = -26 / 4 = -6.5, giving wBatch = 1 - 0.05 * (-6.5) = 1.325.

Mini-batch B = 2 (2 steps):

  • Mini-batch 0 [(1, 2), (2, 4)] at w0 = 1: gmb0 = (-2 + -8) / 2 = -5, so wMini_1 = 1 - 0.05 * (-5) = 1.25.
  • Mini-batch 1 [(1, 3), (2, 5)] at w = 1.25: row gradients are -3.5 and -10, so gmb1 = -6.75 and wMini = 1.25 - 0.05 * (-6.75) = 1.5875.

Single-sample SGD B = 1 (4 steps):

  • Updating after every row moves w through 1 -> 1.1 -> 1.46 -> 1.614 -> 1.9684, finishing at wSgd = 1.9684.

After one epoch over the same four rows, [wBatch, wMini, wSgd] = [1.325, 1.5875, 1.9684].

The live model

The chart plots the end-of-epoch weights [wBatch, wMini, wSgd] side by side. Drag the lr slider from 0.02 to 0.08: the single-sample run gains the most (wSgd rises from 1.4633 to 2.2862) because it takes four steps, while full-batch moves only from 1.13 to 1.52.

At lr = 0.02, the three regimes finish at [1.13, 1.25, 1.4633]. At the default lr = 0.05, they reach [1.325, 1.5875, 1.9684]. At lr = 0.08, they climb to [1.52, 1.88, 2.2862].

End-of-epoch weights [wBatch, wMini, wSgd] = [1.325, 1.5875, 1.9684] at lr = 0.05.
let lr = scene.parameter("lr", 0.05, 0.02, 0.08, 0.01);
let x = input.t().select(0, 0);
let y = input.t().select(0, 1);
let wb = (x.select(0, 0) * 0.0 + 1.0).detach().clone().requires_grad(true);
let wm = wb.detach();
let ws = wb.detach();
let lb = (x * wb - y).square().mean();
lb.backward();
let wBatch = wb - wb.grad() * lr;
for (k in range(0, 2)) {
  let wk = wm.detach().clone().requires_grad(true);
  let em = (x * wk - y).square();
  let lm = (em.select(0, 2 * k) + em.select(0, 2 * k + 1)) * 0.5;
  lm.backward();
  wm = wk - wk.grad() * lr;
}
for (i in range(0, 4)) {
  let wi = ws.detach().clone().requires_grad(true);
  let ls = (wi * x.select(0, i) - y.select(0, i)).square();
  ls.backward();
  ws = wi - wi.grad() * lr;
}
let list = torch.list().add(wBatch.reshape(1)).add(wm.reshape(1));
let answer = torch.cat(list.add(ws.reshape(1)), 0);
{}

Your turn

After one four-row epoch at lr = 0.05, the three regimes land at [wBatch, wMini, wSgd] = [1.325, 1.5875, 1.9684]. The starter model reports [1.325, 1.65, 1.9684]: its mini-batch weight overshoots to 1.65 because the second mini-batch evaluates its gradient at the initial weight 1.0 instead of the intermediate weight 1.25 after the first mini-batch. Make each mini-batch evaluate its gradient at the running mini-batch weight so answer holds [1.325, 1.5875, 1.9684].

Starter model evaluates both mini-batches at w0 = 1.0 (giving wMini = 1.65) instead of re-evaluating at wm = 1.25 (giving 1.5875).
let lr = 0.05;
let x = input.t().select(0, 0);
let y = input.t().select(0, 1);
let wb = (x.select(0, 0) * 0.0 + 1.0).detach().clone().requires_grad(true);
let wm = wb.detach();
let ws = wb.detach();
let lb = (x * wb - y).square().mean();
lb.backward();
let wBatch = wb - wb.grad() * lr;
for (k in range(0, 2)) {
  let wk = wb.detach().clone().requires_grad(true);
  let em = (x * wk - y).square();
  let lm = (em.select(0, 2 * k) + em.select(0, 2 * k + 1)) * 0.5;
  lm.backward();
  wm = wm - wk.grad() * lr;
}
for (i in range(0, 4)) {
  let wi = ws.detach().clone().requires_grad(true);
  let ls = (wi * x.select(0, i) - y.select(0, i)).square();
  ls.backward();
  ws = wi - wi.grad() * lr;
}
let list = torch.list().add(wBatch.reshape(1)).add(wm.reshape(1));
let answer = torch.cat(list.add(ws.reshape(1)), 0);
{}

Update the mini-batch trajectory sequentially so answer holds [wBatch, wMini, wSgd] = [1.325, 1.5875, 1.9684].

Read the result carefully

Why does wMini = 1.5875 sit cleanly between wBatch = 1.325 and wSgd = 1.9684? Full-batch takes one step of size lr = 0.05 using the average of all four gradients. Mini-batch (B = 2) takes two steps of size lr = 0.05, and single-sample SGD (B = 1) takes four steps of size lr = 0.05.

More updates per pass move w further per epoch at a fixed learning rate lr, while re-evaluating the gradient at each intermediate weight prevents the later steps from overshooting as much as 4 * lr would.

Recap

  • Across one N = 4 epoch, full-batch takes 1 step, mini-batch (B = 2) takes 2 steps, and single-sample SGD (B = 1) takes 4 steps.
  • On [(1, 2), (2, 4), (1, 3), (2, 5)] at lr = 0.05, the three methods finish one pass at [wBatch, wMini, wSgd] = [1.325, 1.5875, 1.9684].
  • Mini-batch gradient descent interpolates directly between single-sample update frequency and full-batch gradient averaging.