The true objective is the average loss over the whole dataset of size N:
L(θ)=N1∑i=1Nℓi(θ),∇L(θ)=N1∑i=1N∇ℓi(θ).
Step — build an estimator. Pick a mini-batch B of m examples uniformly at random. Define
gB(θ)=m1∑i∈B∇ℓi(θ).
Why this step? We want a cheap stand-in for the expensive full gradient.
Step — show it is unbiased. For any single randomly chosen index i,
Ei[∇ℓi(θ)]=∑i=1NN1∇ℓi(θ)=∇L(θ).
Averaging m such i.i.d. draws keeps the mean:
E[gB(θ)]=∇L(θ).
Why this step? Unbiasedness means "on average we point in the right (steepest-descent) direction." So in expectation, SGD does exactly what GD does.
Step — quantify the noise. The variance of the mini-batch estimator shrinks with m:
Var[gB]=mσ2,
where σ2 is the per-example gradient variance.
Why this step? This is the central trade-off: bigger m → less noise but more cost per step; smaller m → cheap, noisy steps. The 1/m (not 1/m2) means doubling the batch only halves the noise → diminishing returns, which is exactly why huge batches aren't automatically better.
Why Robbins–Monro?∑ηt=∞ → you can still travel any distance to reach the minimum. ∑ηt2<∞ → step sizes shrink fast enough that noise eventually averages out and you stop bouncing. A classic choice: ηt=η0/(1+kt).
initialize θ
for epoch in 1..E:
shuffle dataset # why: keep draws ~ i.i.d., break ordering bias
for each mini-batch B:
g = (1/m) Σ_{i∈B} ∇ℓ_i(θ)
θ = θ − η · g
One epoch = one full sweep through all N examples = N/m updates.
Imagine finding the lowest point in a huge foggy valley by feeling the ground slope under your feet. Checking the slope using the whole valley at once is exhausting. Instead, you check just the little patch you're standing on and take a step downhill. Each patch is a bit misleading, so you wobble — but if you keep stepping and slowly take smaller steps, all the wobbles cancel out and you end up at the bottom. Checking a small patch is fast, so you take tons of steps and get there quicker. That wobble even helps you skip over little ditches that aren't the real bottom.
Dekho, deep network train karna matlab ek loss functionL(θ) ko minimize karna hai — yaani woh sabse neeche wala point dhoondhna jahan galtiyan sabse kam ho. Agar hum poore dataset (millions of examples) ka gradient nikaal ke ek step lein, toh woh bilkul accurate hoga, par bohot slow. SGD kehta hai: har step pe sirf ek chhota random mini-batch utha lo, usi se gradient estimate karo, aur step maar do. Yeh estimate thoda noisy hota hai, par sasta hota hai — toh hum thousands of steps le sakte hain few expensive steps ke jagah.
Sabse important baat: yeh noisy gradient unbiased hota hai, matlab average mein woh sahi (true) gradient ki taraf hi point karta hai — E[gB]=∇L. Iska variance σ2/m hota hai, yaani batch size m badhao toh noise kam, par sirf 1/m ke rate se — isliye bahut bada batch lene ka faayda kam hota jaata hai. Yeh trade-off hi SGD ka dil hai.
Ek aur interesting cheez: yeh noise sirf tolerate karne wali problem nahi hai, balki helpful bhi hai. Deep networks mein bohot saare saddle points aur sharp minima hote hain jahan normal gradient descent atak sakta hai. SGD ka wobble usse dhakka de kar bahar nikaal deta hai, aur woh flat minima ki taraf le jaata hai jo generalize better karte hain (regularization jaisa effect).
Practical tips yaad rakho: har epoch pe data shuffle karo (taaki batches i.i.d. rahein), aur learning rate ko dheere-dheere kam karo (Robbins–Monro conditions: ∑ηt=∞, ∑ηt2<∞), warna constant η ke saath model minimum ke aas-paas bounce karta rahega, exactly settle nahi hoga. Mnemonic: Sample, Slope, Step, Shrink.