5.6.10Machine Learning (Aerospace Applications)

Batch, mini-batch, stochastic gradient descent

2,034 words9 min readdifficulty · medium

WHY does this distinction even exist?

The loss we truly want to minimise is the average over all NN training samples:

J(θ)=1Ni=1NL(fθ(xi),yi)J(\theta) = \frac{1}{N}\sum_{i=1}^{N} L\big(f_\theta(x_i),\, y_i\big)

To move downhill we need the gradient θJ\nabla_\theta J. The exact gradient is:

θJ(θ)=1Ni=1NθLi\nabla_\theta J(\theta) = \frac{1}{N}\sum_{i=1}^{N} \nabla_\theta L_i

The catch: computing this full sum is expensive when NN is huge (millions of aerodynamic CFD samples, sensor logs, etc.). So we approximate the gradient using a subset of data. The three methods are three choices of subset size.


HOW the update rule is derived (from first principles)

WHAT we want: parameters θ\theta that minimise J(θ)J(\theta).

Step 1 — Taylor expand the loss around current θ\theta for a small step Δθ\Delta\theta:

J(θ+Δθ)J(θ)+θJ(θ)ΔθJ(\theta + \Delta\theta) \approx J(\theta) + \nabla_\theta J(\theta)^\top \Delta\theta

Why this step? Near θ\theta, the loss is locally linear; the linear term dominates the change.

Step 2 — Choose Δθ\Delta\theta to decrease JJ fastest. To make JΔθ\nabla J^\top \Delta\theta as negative as possible for a fixed step length, point opposite the gradient:

Δθ=ηθJ(θ)\Delta\theta = -\eta\,\nabla_\theta J(\theta)

Why this step? The dot product JΔθ\nabla J^\top \Delta\theta is minimised when Δθ\Delta\theta is anti-parallel to J\nabla J (Cauchy–Schwarz). η>0\eta > 0 is the learning rate (step size).

Step 3 — The general update:

θθηg^\boxed{\theta \leftarrow \theta - \eta\, \hat{g}}

where g^\hat{g} is our estimate of θJ\nabla_\theta J. The three methods differ only in g^\hat g:

Why is SGD "unbiased"? If we pick kk uniformly at random from {1,,N}\{1,\dots,N\}:

Ek[θLk]=k=1N1NθLk=θJ\mathbb{E}_k[\nabla_\theta L_k] = \sum_{k=1}^{N}\frac{1}{N}\nabla_\theta L_k = \nabla_\theta J

So on average one random sample points the right way — it's just noisy. That noise is the whole story.


The variance trade-off (the 80/20 core)

For a mini-batch of size BB drawn from data with per-sample gradient variance σ2\sigma^2:

Var[g^]=σ2B\operatorname{Var}[\hat g] = \frac{\sigma^2}{B}

Why? Variance of an average of BB i.i.d. terms shrinks like 1/B1/B.

Figure — Batch, mini-batch, stochastic gradient descent

Worked Example 1 — one SGD step by hand

Fit y=θxy = \theta x with squared loss Li=12(θxiyi)2L_i = \tfrac12(\theta x_i - y_i)^2. Data: (x1,y1)=(2,4)(x_1,y_1)=(2,4), start θ=0\theta = 0, η=0.1\eta = 0.1.

Step: θL1=(θx1y1)x1=(024)2=8\nabla_\theta L_1 = (\theta x_1 - y_1)\,x_1 = (0\cdot2 - 4)\cdot2 = -8. Why this step? Chain rule: ddθ12(θxy)2=(θxy)x\frac{d}{d\theta}\tfrac12(\theta x - y)^2 = (\theta x - y)\cdot x.

Update: θ00.1(8)=0.8\theta \leftarrow 0 - 0.1(-8) = 0.8. Why? Gradient is negative, so we step up in θ\theta toward the true value θ=2\theta^*=2.

We used one sample — this is SGD.

Worked Example 2 — one Batch step

Same model, now use both points: add (x2,y2)=(1,1)(x_2,y_2)=(1,1), θ=0\theta = 0.

L1=8\nabla L_1 = -8 (as above). L2=(011)1=1\nabla L_2 = (0\cdot1 - 1)\cdot1 = -1.

Batch gradient: g^=12(8+1)=4.5\hat g = \tfrac12(-8 + -1) = -4.5. Why average? Batch GD uses the mean over all N=2N=2 samples — the true JJ gradient.

Update: θ00.1(4.5)=0.45\theta \leftarrow 0 - 0.1(-4.5) = 0.45. Smoother, single trustworthy step.

Worked Example 3 — mini-batch epoch bookkeeping

N=1000N = 1000 samples, B=100B = 100. How many updates per epoch (one full pass over data)?

updates=NB=1000100=10.\text{updates} = \frac{N}{B} = \frac{1000}{100} = 10. Why? Each mini-batch consumes BB samples; you need N/BN/B batches to cover all data once. Batch GD would give 1 update/epoch; pure SGD gives 1000.



Recall Feynman: explain to a 12-year-old

Imagine you're blindfolded on a hill and want to reach the bottom. To feel which way is downhill you tap the ground with your foot.

  • Batch GD: you carefully measure the slope in every direction all around you before taking one slow, perfect step. Accurate but sloooow.
  • SGD: you tap the ground once in a random spot and immediately take a step. Sometimes you step slightly wrong, but you take tons of quick steps and get down fast.
  • Mini-batch: you tap a handful of spots, average them, then step. Not perfect, not random — the smart middle. That's why almost everyone uses it.

Flashcards

What is the batch size BB for Batch GD, SGD, and Mini-batch GD?
Batch: B=NB=N; SGD: B=1B=1; Mini-batch: 1<B<N1<B<N.
What is the single general update rule for all three methods?
θθηg^\theta \leftarrow \theta - \eta\,\hat g, where g^\hat g is the gradient estimate.
Why is the SGD gradient estimate called "unbiased"?
Because Ek[Lk]=J\mathbb{E}_k[\nabla L_k] = \nabla J; on average a random sample points in the true gradient direction.
How does gradient-estimate variance scale with batch size BB?
Var[g^]σ2/B\operatorname{Var}[\hat g] \propto \sigma^2 / B — bigger batch, less noise.
How many parameter updates happen per epoch with NN samples and batch size BB?
N/BN/B updates.
Why can a little noise in SGD be beneficial?
It helps escape saddle points and shallow local minima in non-convex loss landscapes.
State the derivation reason the step direction is J-\nabla J.
Taylor: ΔJJΔθ\Delta J \approx \nabla J^\top \Delta\theta; this is most negative when Δθ\Delta\theta is anti-parallel to J\nabla J.
Why does Batch GD often lose to mini-batch in wall-clock time?
It makes only one (expensive) update per epoch; mini-batch makes many cheap good-enough updates.
What is the linear scaling rule for learning rate?
As batch size grows, scale learning rate roughly proportionally: ηB\eta \propto B.
What trick makes noisy SGD actually converge to the minimum?
A decreasing learning-rate schedule ηt0\eta_t \to 0.

Connections

  • Gradient Descent — parent algorithm; these are its data-sampling variants.
  • Learning Rate and Schedulesη\eta must adapt to batch size and time.
  • Momentum and Adam — smooth out mini-batch noise using running averages.
  • Loss Functions — the LiL_i we sum over.
  • Bias-Variance Tradeoff — the same variance logic governs g^\hat g.
  • Saddle Points and Non-Convex Optimization — why noise helps in aerospace surrogate models.
  • Backpropagation — how each θLi\nabla_\theta L_i is actually computed.

Concept Map

differentiate

full sum

forces

controlled by

leads to

gives

plugs g-hat into

B equals N

B equals 1

middle

exact, no

unbiased but high

unbiased, medium

scales as sigma over sqrt B

True loss J avg over N samples

Exact gradient grad J

Full sum expensive when N huge

Approximate gradient g-hat

Batch size B

Update theta minus eta g-hat

Taylor expansion, local linear

Step anti-parallel to gradient

Batch GD, B equals N

SGD, B equals 1

Mini-batch, 1 lt B lt N

Gradient noise / variance

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, teeno methods — Batch, Mini-batch, aur Stochastic (SGD) — ka basic kaam ek hi hai: loss ke pahaad par neeche utarna (downhill jaana). Farq sirf itna hai ki har ek step lene se pehle tum kitne training examples dekh kar direction decide karte ho. Batch GD saare NN examples dekhta hai — bahut accurate direction, lekin ek step lene mein bahut time. SGD sirf ek random example dekh kar step maar deta hai — direction thoda galat ho sakta hai (noisy), par steps bahut fast aur sasti hoti hain. Mini-batch beech ka smart raasta hai: 32 ya 64 examples ka chunk le kar average nikaalo, phir step. Isiliye real-world mein 90% log mini-batch hi use karte hain.

Update rule sab ke liye same hai: θθηg^\theta \leftarrow \theta - \eta\,\hat g. Yahan g^\hat g gradient ka estimate hai aur η\eta learning rate (step size). Derivation simple hai — Taylor expansion se loss ka change JΔθ\nabla J^\top \Delta\theta nikalta hai, aur ise sabse zyada negative banane ke liye tumhe gradient ke opposite jaana padta hai. Bas yahi downhill direction hai.

Key insight yaad rakho: noise ka variance 1/B\propto 1/B hota hai. Yaani jitna bada batch, utna kam shor, par utni mehngi compute. Chhota batch = zyada shor par sasti aur fast steps. Aur mazedaar baat — thoda noise actually achha hai, kyunki aerospace ke non-convex loss surfaces mein wo tumhe saddle points aur shallow minima se bahar nikaal deta hai. Isiliye "exact gradient wala Batch GD best hai" ye galat soch hai — accuracy per step matlab nahi, per second progress matlab rakhti hai.

Ek common galti: log sochte hain bada batch hamesha better trains karta hai. Lekin bahut bade batch aksar generalise kharaab karte hain (sharp minima mein fas jaate hain) aur noise reduction bhi 1/B1/\sqrt B se slow hoti hai — matlab diminishing returns. Isliye 32 se 256 ka mid-size batch practical sweet spot hai. Ye 80/20 rule hai: thodi si tuning se maximum fayda.

Go deeper — visual, from zero

Test yourself — Machine Learning (Aerospace Applications)

Connections