5.6.10 · D4Machine Learning (Aerospace Applications)

Exercises — Batch, mini-batch, stochastic gradient descent

3,326 words15 min readBack to topic

Everything here rests on one update rule from the parent note:

Here (theta) is the parameter we tune, (eta) is the learning rate (how big a step), and ("g-hat") is the estimated downhill direction. Every symbol below is one of these. See Gradient Descent and Learning Rate and Schedules if any feels new.


Level 1 — Recognition

L1.1 · Name the flavour

For a dataset of images, you compute the gradient using all 60,000 examples before each update. Which of Batch / SGD / Mini-batch is this?

Recall Solution

The batch size equals (). By the parent definition == is Batch GD==. It uses the exact average gradient every step — zero noise, maximum cost per step.

L1.2 · Read the batch size

An engineer writes: "I draw 64 random samples, average their gradients, then step." What is , and what flavour is this?

Recall Solution

. Since (assuming the dataset is bigger than 64), this is Mini-batch GD. The average of 64 gradient terms is an unbiased estimate whose noise (variance , with the per-sample gradient variance defined above) is 64× smaller than a single sample's.

L1.3 · Match the noise

Rank Batch, Mini-batch (), and SGD from least noisy to most noisy gradient estimate.

Recall Solution

Estimate variance is (recall is the per-sample gradient variance), so bigger = less noise:

  • Batch (): least noisy (exact, variance ).
  • Mini-batch (): medium.
  • SGD (): most noisy. Order least → most: Batch < Mini-batch < SGD.

Level 2 — Application

L2.1 · One SGD step

Fit with squared loss , where is the input and the target of sample . Data point , start , learning rate . Compute the new after one SGD step.

Recall Solution

Gradient (chain rule): . Update: . Why up? The gradient is negative, so stepping against it (subtracting a negative) increases toward the true value .

L2.2 · One Batch step

Same model. Now use both points: add , keep , . Compute the batch update.

Recall Solution

(from L2.1). . Batch gradient (average of ): . Update: . Why average, not sum? Batch GD estimates the mean loss gradient , so the two disagreeing samples ( and ) are blended into one trustworthy direction . Why is the step smaller than SGD's? The mild sample () pulls the average toward zero, so batch takes a gentler, less jumpy step () than the single hot sample alone would () — exactly the smoothing that lower noise buys you.

L2.3 · Epoch bookkeeping

You have samples and choose . How many parameter updates happen in one epoch (one full pass over all data)? How many in 10 epochs?

Recall Solution

Updates per epoch . Over 10 epochs updates. Why? An epoch means seeing every sample once; each mini-batch consumes samples, so you need batches to cover the data.

L2.4 · Variance from batch size

Per-sample gradient variance is . Compute for SGD (), mini-batch , and mini-batch .

Recall Solution

Use .

  • : .
  • : .
  • : . Quadrupling from 4 to 16 cut variance by 4× — clean shrinkage.
Figure — Batch, mini-batch, stochastic gradient descent

L2.5 · When is not divisible by

You have samples and pick . How many mini-batches does one epoch contain, and how big is each?

Recall Solution

, which is not a whole number. You form full batches of 250 (using 1000 samples), leaving a remainder of samples. Standard practice: make one extra smaller last mini-batch of size 30. So the epoch has updates: four with and one with . Why does this matter? That last batch has higher variance ( vs ), so its step is noisier — some libraries let you drop the remainder (drop_last=True) to keep every step equally clean. Either way, the number of updates per epoch is .


Level 3 — Analysis

L3.1 · Wall-clock, not step count

Assume computing one sample's gradient costs unit of time. Dataset . Compare, per epoch: (a) time cost, (b) number of updates, for Batch vs. mini-batch . Which makes more progress per unit time, and why?

Recall Solution

Per epoch each method processes all 1000 samples once, so both cost time units per epoch (same total gradient computations).

  • Batch: update per epoch.
  • Mini-batch : updates per epoch. Same compute, but mini-batch makes 20 course-corrections while Batch makes 1. Each mini-batch step is "good enough" (unbiased, moderate noise), so 20 good-enough steps typically drive the loss down far more than a single perfect step. This is why mini-batch wins in wall-clock time even though its per-step direction is less exact.

L3.2 · When noise helps

Two runs on a non-convex loss (see Saddle Points and Non-Convex Optimization): Run A uses Batch GD, Run B uses SGD. Run A stalls with a flat loss curve; Run B's loss wiggles but keeps dropping. Explain what likely happened to Run A and why Run B escaped.

Recall Solution

Run A (Batch) likely got stuck at a saddle point or shallow local minimum: there the exact gradient , so the update barely moves — it stalls. Run B (SGD) has a noisy estimate whose variance is nonzero even when . That random kick pushes off the flat saddle onto a slope where the gradient is again informative. So the noise, harmful for precision, is helpful for escaping flat traps. This is a central reason noisy small-batch methods are favoured for the non-convex surrogate models in aerospace ML.

L3.3 · Diminishing returns on

Per-sample variance . Compute the reduction in standard deviation when going , and separately . Comment on the 80/20 point.

Recall Solution

Standard deviation .

  • : . Drop of (halved).
  • : . Drop of . Same 4× increase in , but the first jump removes 8× more noise than the second. Because noise falls like , early increases in pay off hugely and later ones barely move the needle — that is the 80/20 sweet spot justifying .

Level 4 — Synthesis

L4.1 · Design a training budget

You have CFD drag samples, a compute budget of exactly 1000 gradient computations total, and want the most parameter updates possible while keeping variance . Per-sample variance is . Choose and compute the number of updates you can afford.

Recall Solution

Constraint 1 (variance): need . So the smallest allowed batch is . Constraint 2 (budget): each update costs gradient computations; total budget is 1000. Updates . To maximise updates we want the smallest legal , i.e. . Updates updates. Why smallest legal B? Fewer computations per step means more steps for the same budget, and still satisfies the noise cap exactly. Any would break the variance requirement; any wastes budget on unnecessary noise reduction.

L4.2 · Linear scaling rule in action

A model trains stably with and . You switch to to use a bigger GPU. Using the linear scaling rule , what learning rate should you use, and why is a bigger now safe?

Recall Solution

Ratio , so scale the learning rate by 8: . Why safe? Bigger shrinks gradient variance ( vs — 8× less). With a cleaner, more trustworthy direction , you can take a bolder step without the noise throwing you off course. Keeping with would waste the low-noise estimate on tiny, over-cautious steps. See Momentum and Adam for adaptive alternatives that ease this tuning.

L4.3 · Multi-step mini-batch trace

Model , loss (input , target ), , start . Two mini-batches: Batch A , Batch B . Do one update on A, then one on B (in order). Give after each.

Recall Solution

Update 1 (Batch A, ): . Mean over 1 sample . . Update 2 (Batch B, ), now : . . Mean . . The parameter climbs toward , with the second (averaged) step smoother than the first.


Level 5 — Mastery

L5.1 · Convergence neighbourhood & schedules

Explain rigorously why constant- SGD converges only to a neighbourhood of the minimum, not the exact point, and how a learning-rate schedule fixes it. Illustrate with the update rule.

Recall Solution

At the true minimum , the full gradient , but a single-sample estimate has nonzero variance even there. So the update still moves by a random amount of typical size . The iterate never settles; it bounces inside a ball of radius around . A larger ⇒ bigger ball. Fix — a schedule (e.g. ): as steps grow the bounce amplitude , so the neighbourhood shrinks to a point. The classic Robbins–Monro conditions (still travel far enough) and (noise damps out) guarantee convergence to . See Learning Rate and Schedules.

L5.2 · Sharp vs. flat minima trade-off

Large batches often generalise worse than small ones despite lower training loss. Reason through why, connecting batch noise, minima geometry, and the Bias-Variance Tradeoff.

Recall Solution

A loss surface has sharp minima (narrow, steep walls) and flat minima (wide basins). Sharp minima fit the training set tightly but a small shift between train and test data lands you on a steep wall → large test loss (poor generalisation). Flat minima are robust: nearby points also have low loss, so train/test shift barely hurts. Batch size controls which you find. Large-batch gradients are nearly noise-free, so the optimiser slides straight into the nearest minimum — often a sharp one. Small-batch noise (variance ) acts like a random kick that cannot stay inside a narrow sharp basin — it gets shaken out and only wide flat basins can hold it. So small batches implicitly bias toward flat, generalising minima. In bias–variance terms: the noise is a form of regularisation that trades a little training-set fit (slightly higher bias) for much better test performance (lower variance of the learned function). Hence mid-size batches, not huge ones, dominate practice.

L5.3 · Choose the method for an aerospace pipeline

A surrogate neural net predicts drag from wing geometry. Training data streams in continuously from live wind-tunnel sensors (you cannot store it all). Which method — Batch, Mini-batch, or SGD — and why? Address memory, noise, and the streaming nature.

Recall Solution

Batch GD is impossible: it needs all samples in memory to form , but data streams in and cannot be fully stored — is effectively unbounded/growing. Pure SGD () works with streaming (update per sample) but is very noisy and under-uses each GPU pass. Mini-batch is the right choice: buffer a small window (e.g. ) of the incoming stream, update, discard, repeat. This (a) fits in memory, (b) reduces variance to for a stable-enough direction, (c) suits the online/streaming setting, and (d) keeps the useful noise that escapes the saddle points of the non-convex surrogate landscape. Pair it with a decaying schedule so early exploration gives way to precise convergence, and consider Momentum and Adam to smooth the noisy stream further.


Return to the parent note · related: Gradient Descent, Backpropagation, Loss Functions.