Part VIII — Neural Network Fundamentals, Backpropagation & Optimizers · Chapter 8

Stochastic & mini-batch gradient descent

Hook

To take one gradient step, do you really need to look at every single data point first?

Intuition
Step 0 — using all 5 points every time

This is the exact same landscape and step button from the Part I capstone — except now the bowl is a real loss surface, fit to five data points, and each step uses every one of them to compute its direction. Watch how smoothly it heads for the minimum.

Formalize

That's batch gradient descent — the gradient is the average over every example:

Lbatch=1ni=1nLi\nabla L_{\text{batch}} = \frac{1}{n}\sum_{i=1}^n \nabla L_i
  • Lbatch\nabla L_{\text{batch}} — the batch gradient: the direction used for one step, averaged over the whole dataset.
  • nn — the total number of training examples.
  • Li\nabla L_i — the gradient of the loss computed from a single example ii.
  • ii — the index of one training example.
  1. Stochastic gradient descent

    Uses just one example per step, Li\nabla L_i for a single ii — cheap, but noisy.

  2. Mini-batch gradient descent

    Splits the difference, averaging over a small subset BB: 1BiBLi\frac{1}{|B|}\sum_{i \in B} \nabla L_i.

  3. All three trade accuracy for cost

    Batch, stochastic, and mini-batch are all estimates of the same true gradient — they just trade accuracy per step for cost per step.

Play
Step 0 — (w=0.00, b=0.00), |∇| = 8.14

Switch modes and take a few steps each time. Batch heads straight for the minimum. Stochastic zigzags — each step only "knows about" one point, so it's regularly a little wrong — while mini-batch sits somewhere in between.

Worked example

At (w,b)=(0,0)(w,b)=(0,0):

  1. Compute one point's stochastic gradient

    For squared-error loss, each point's gradient is (Li/w,Li/b)=(2xiri, 2ri)(\partial L_i/\partial w, \partial L_i/\partial b) = (-2x_i r_i,\ -2r_i), where ri=yi(wxi+b)r_i=y_i-(wx_i+b) is that point's residual. At (w,b)=(0,0)(w,b)=(0,0), the point (2,3.2)(-2,-3.2) has residual r=3.20=3.2r=-3.2-0=-3.2, giving:

    (2(2)(3.2), 2(3.2))=(12.8, 6.4)(-2(-2)(-3.2),\ -2(-3.2)) = (-12.8,\ 6.4)
  2. Compare it to the full batch gradient

    At the same (w,b)=(0,0)(w,b)=(0,0), every residual equals yiy_i (the prediction is 00 everywhere). Each point's (2xiyi, 2yi)(-2x_iy_i,\ -2y_i) pair:

    • x=2,y=3.2x=-2,y=-3.2: (12.8, 6.4)(-12.8,\ 6.4)
    • x=1,y=0.8x=-1,y=-0.8: (1.6, 1.6)(-1.6,\ 1.6)
    • x=0,y=1.3x=0,y=1.3: (0, 2.6)(0,\ -2.6)
    • x=1,y=3.1x=1,y=3.1: (6.2, 6.2)(-6.2,\ -6.2)
    • x=2,y=4.7x=2,y=4.7: (18.8, 9.4)(-18.8,\ -9.4)

    Averaging each column: ww-gradient =(12.81.6+06.218.8)/5=7.88=(-12.8-1.6+0-6.2-18.8)/5=-7.88, bb-gradient =(6.4+1.62.66.29.4)/5=2.04=(6.4+1.6-2.6-6.2-9.4)/5=-2.04 — same rough neighborhood as the single point above, but a different ww-magnitude and even a flipped sign on bb.

  3. Average over a full pass

    The individual gradients exactly reconstruct the batch gradient — noise that cancels out over time, not bias.

Checkpoint

Using stochastic steps — one data point at a time — get within 0.4 of the true optimum in 20 steps or fewer.

Step 0 / 20 — distance to optimum = 2.22
0/20 steps used
Take a step to try it
Summary
Lbatch=1ni=1nLi,Lstochastic=Li\nabla L_{\text{batch}} = \frac{1}{n}\sum_{i=1}^n \nabla L_i, \qquad \nabla L_{\text{stochastic}} = \nabla L_i

Every step is still exactly Chapter 2's descent rule from Part I — only which examples compute the gradient changes. Real training almost always uses mini-batches: batch is often too slow to even compute once, and pure stochastic is noisier than necessary.