Speed

Mini-Batch Gradient

Group a four-row dataset into two mini-batches of size two, averaging gradients inside each mini-batch before updating the weight.

The problem

Full-batch gradient descent averages all N = 4 rows before taking a single step, while single-sample SGD takes 4 noisy steps—one after every individual row. Modern deep learning almost always chooses the middle ground: mini-batch gradient descent.

With mini-batch size B = 2, a 4-row epoch splits into N / B = 2 mini-batches: rows {0, 1} form Mini-batch 0, and rows {2, 3} form Mini-batch 1. Inside each mini-batch, we average the B = 2 sample gradients into one mini-batch gradient g_mb and update w once before moving to the next mini-batch.

Work through the numbers

Use the four rows [(1, 2), (2, 4), (3, 5), (4, 9)], starting at w0 = 1 with lr = 0.05 and mini-batch size B = 2:

Mini-batch 0 (rows (1, 2) and (2, 4)) evaluated at w0 = 1:

  • Row 0 gradient: 2 * (1 * 1 - 2) * 1 = -2
  • Row 1 gradient: 2 * (1 * 2 - 4) * 2 = -8
  • Mini-batch 0 mean gradient: g0 = (-2 + -8) / 2 = -5
  • First mini-batch update: w1 = 1 - 0.05 * (-5) = 1.25

Mini-batch 1 (rows (3, 5) and (4, 9)) evaluated at w1 = 1.25:

  • Row 2 gradient: 2 * (1.25 * 3 - 5) * 3 = -7.5
  • Row 3 gradient: 2 * (1.25 * 4 - 9) * 4 = -32
  • Mini-batch 1 mean gradient: g1 = (-7.5 + -32) / 2 = -19.75
  • Second mini-batch update: w2 = 1.25 - 0.05 * (-19.75) = 2.2375

One epoch of mini-batch SGD (B = 2) produces [g0, w1, g1, w2] = [-5, 1.25, -19.75, 2.2375].

The live model

The chart displays [g0, w1, g1, w2] across both mini-batches. Drag the lr slider from 0.02 to 0.08 and watch w2 run from 1.57 to 2.68 while g0 stays pinned at -5, since Mini-batch 0 always starts from w0 = 1.

At lr = 0.02, w1 = 1.1, g1 = -23.5, and w2 = 1.57. At the default lr = 0.05, w1 = 1.25, g1 = -19.75, and w2 = 2.2375. At lr = 0.08, w1 = 1.4, g1 = -16, and w2 = 2.68.

Two mini-batches of size 2 produce [g0, w1, g1, w2] = [-5, 1.25, -19.75, 2.2375].
let lr = scene.parameter("lr", 0.05, 0.02, 0.08, 0.01);
let r0 = input.select(0, 0);
let r1 = input.select(0, 1);
let r2 = input.select(0, 2);
let r3 = input.select(0, 3);
let w0 = r0.select(0, 0) * 0.0 + 1.0;
let wa = w0.detach().clone().requires_grad(true);
let e0 = (wa * r0.select(0, 0) - r0.select(0, 1)).square();
let e1 = (wa * r1.select(0, 0) - r1.select(0, 1)).square();
let loss0 = (e0 + e1) * 0.5;
loss0.backward();
let g0 = wa.grad();
let w1 = wa - g0 * lr;
let wb = w1.detach().clone().requires_grad(true);
let e2 = (wb * r2.select(0, 0) - r2.select(0, 1)).square();
let e3 = (wb * r3.select(0, 0) - r3.select(0, 1)).square();
let loss1 = (e2 + e3) * 0.5;
loss1.backward();
let g1 = wb.grad();
let w2 = wb - g1 * lr;
let list = torch.list().add(g0.reshape(1)).add(w1.reshape(1));
list.add(g1.reshape(1)).add(w2.reshape(1));
let answer = torch.cat(list, 0);
out.set("g0", g0.item()).set("w1", w1.item()).set("w2", w2.item());
{
  "g0": -5,
  "w1": 1.25,
  "w2": 2.2375
}

Your turn

With mini-batch size B = 2 and lr = 0.05, the two mini-batch updates produce [g0, w1, g1, w2] = [-5, 1.25, -19.75, 2.2375]. The starter model reports [-10, 1.5, -30, 3.0]: its first mini-batch gradient is -10 instead of -5 because each mini-batch sums its two squared errors without averaging over the batch size B = 2, effectively doubling the step size. Average each mini-batch loss over its two rows so answer holds [-5, 1.25, -19.75, 2.2375].

Starter model sums each mini-batch loss instead of dividing by the batch size 2.
let lr = 0.05;
let r0 = input.select(0, 0);
let r1 = input.select(0, 1);
let r2 = input.select(0, 2);
let r3 = input.select(0, 3);
let w0 = r0.select(0, 0) * 0.0 + 1.0;
let wa = w0.detach().clone().requires_grad(true);
let e0 = (wa * r0.select(0, 0) - r0.select(0, 1)).square();
let e1 = (wa * r1.select(0, 0) - r1.select(0, 1)).square();
let loss0 = e0 + e1;
loss0.backward();
let g0 = wa.grad();
let w1 = wa - g0 * lr;
let wb = w1.detach().clone().requires_grad(true);
let e2 = (wb * r2.select(0, 0) - r2.select(0, 1)).square();
let e3 = (wb * r3.select(0, 0) - r3.select(0, 1)).square();
let loss1 = e2 + e3;
loss1.backward();
let g1 = wb.grad();
let w2 = wb - g1 * lr;
let list = torch.list().add(g0.reshape(1)).add(w1.reshape(1));
list.add(g1.reshape(1)).add(w2.reshape(1));
let answer = torch.cat(list, 0);
out.set("g0", g0.item()).set("w1", w1.item()).set("w2", w2.item());
{
  "g0": -10,
  "w1": 1.5,
  "w2": 2.85
}

Average each two-row mini-batch loss before differentiating so answer holds [-5, 1.25, -19.75, 2.2375].

Read the result carefully

Why divide each mini-batch loss by B instead of summing? When each mini-batch averages its B losses, the expected magnitude of g_mb stays independent of the batch size B. If you summed without dividing by B, doubling the mini-batch size from 2 to 4 would secretly double the effective learning rate lr!

Recap

  • Mini-batch gradient descent partitions N training rows into subsets of size B, performing N / B updates per epoch.
  • Inside each mini-batch, the loss (and therefore the gradient) is averaged over the B rows before updating w.
  • On [(1, 2), (2, 4), (3, 5), (4, 9)] with B = 2 and lr = 0.05, the two mini-batch updates yield [g0, w1, g1, w2] = [-5, 1.25, -19.75, 2.2375].