Worked examples — Batch, mini-batch, stochastic gradient descent
5.6.10 · D3· Coding › Machine Learning (Aerospace Applications) › Batch, mini-batch, stochastic gradient descent
Yeh page parent topic ka drill floor hai. Yahan theory dobara explain nahi ki gayi — hum har case ko cover karte hain jo yeh topic throw kar sakta hai, ek worked example per cell. Agar koi symbol unfamiliar lage, parent par wapas jaao, phir return karo.
Shuru karne se pehle, ek one-line reminder us machine ka jo hum chala rahe hain:
The scenario matrix
Is topic ka har problem in cells mein se kisi ek mein aata hai. Har row ek aisi cheez hai jo vary ho sakti hai; neeche ke examples us cell ke saath labelled hain jo woh cover karta hai.
| # | Case class | Kya mushkil banata hai | Example |
|---|---|---|---|
| C1 | Gradient ki sign — positive | step mein neeche jaana chahiye, upar nahi | Ex 1 |
| C2 | Gradient ki sign — negative | step mein upar jaata hai | Ex 2 |
| C3 | Zero gradient (degenerate) | already ek flat spot par — koi movement nahi | Ex 3 |
| C4 | Batch vs SGD same data par | noise vs exactness numerically dikhao | Ex 4 |
| C5 | Variance ki tarah shrink hoti hai | noise-reduction number prove karo | Ex 5 |
| C6 | Epoch bookkeeping, divisible by nahi | leftover last batch | Ex 6 |
| C7 | Limiting behaviour — bahut bada | divergence / overshoot | Ex 7 |
| C8 | Real-world word problem (aerospace) | wall-clock ko method choice mein translate karo | Ex 8 |
| C9 | Exam twist — linear-scaling rule | ko ke saath sahi se scale karo | Ex 9 |
| C10 | Saddle se escape (noise kyun help karta hai) | zero gradient jo noise break karta hai | Ex 10 |
Numeric examples ke liye running toy model sabse simple cheez hai jiske paas phir bhi gradient ho:
Beech wala formula chain rule se aata hai: ko differentiate karo aur inner bahar aa jaata hai. Hum ise har example mein use karte hain. Throughout, hum subscript rakhte hain par taaki yeh hamesha clear rahe ki hum ek sample ki baat kar rahe hain.
C1 — Positive gradient: step neeche jaana chahiye mein
Forecast: hum target ke upar se shuru kar rahe hain. Guess — ek step ke baad increase hoga ya decrease?
- Gradient compute karo. . Kyunki yeh SGD hai (), estimate is exact per-sample gradient ke equal hai: . Yeh step kyun? Yahi ek cheez hai jo update rule ko chahiye; iska sign direction decide karta hai.
- Sign padho. . Ek positive gradient ka matlab hai "loss badhne par badhti hai," toh hum ko neeche move karna chahiye. Yeh step kyun? Rule ek positive number subtract karta hai → shrink hota hai. ki sign = direction, automatically.
- Update. . Yeh step kyun? Humne general rule liya aur plug in kiya.
Verify: humne predict kiya tha ki ki taraf drop karega. Target se distance se ghatkar ho gaya, toh hum kareeб aaye. ✓ Positive gradient ⇒ downward step. ✓

C2 — Negative gradient: step upar jaata hai mein
Forecast: is baar target se neeche. Upar ya neeche?
- Gradient: . Phir se SGD, toh . Yeh step kyun? Same formula; sirf change hua.
- Sign: . Ek negative number subtract karna add karta hai, toh badhta hai. Yeh step kyun? — sign flip poora mechanism hai.
- Update: .
Verify: se distance ho gaya. Kareeб. ✓ Negative gradient ⇒ upward step. ✓ C1 aur C2 milkar dikhate hain ki ki sign hamesha humein minimum ki taraf steer karti hai, dono sides se.
C3 — Zero gradient: degenerate "already there" case
Forecast: hum par hain. Ek achha algorithm kya karna chahiye? (Hint: kuch nahi.)
- Gradient: , toh . Yeh step kyun? Minimum par prediction target ke equal hai; residual zero hai, poore gradient ko kill karta hai.
- Update: . Koi movement nahi. Yeh step kyun? step size hai chahe kuch bhi ho.
Verify: loss , already minimal; algorithm sahi se move karna band kar deta hai. ✓ Yeh update ka fixed point hai: stable hai iff . (Dhyan do: yahi condition saddle points par bhi hold karti hai — C10 dekho.)
C4 — Identical data par Batch vs SGD (noise vs exactness)
Forecast: kaun sa step bada hoga — Batch, ya worst-case single-sample SGD step?
- Per-sample gradients: , . Yeh step kyun? Humein har ek ki zaroorat hai dono average (Batch) aur pick (SGD) karne ke liye.
- Batch estimate: — yahan koi bhi single ; yeh par unka average hai, exact . Update: . Yeh step kyun? Batch saare samples par mean use karta hai.
- SGD estimate, sample 1: → . SGD, sample 2: → . Yeh step kyun? SGD () ek random sample pick karta hai, toh us sample ke exact gradient ke equal hota hai — kismat par depend karke ya produce karta hai. Woh kismat hi "noise" hai.
Verify: do SGD outcomes ka average Batch outcome. ✓ Yeh "SGD unbiased hai" ko numerically dikhaya gaya hai: on average SGD Batch ke equal hai, lekin koi bhi single SGD step (0.8 ya 0.1) uske around scatter karta hai. Woh scatter noise hai.

C5 — Variance sach mein ki tarah shrink hoti hai
Forecast: se jaane par — noise drop hoti hai ya ?
- Formula apply karo (dekho Bias-Variance Tradeoff). Yeh step kyun? i.i.d. gradients ka average hai; terms ke mean ka variance hota hai.
- Plug in: ; ; .
- Catch — standard deviation. Jo noise feel hoti hai woh standard deviation hai: . se std drop hoti hai, yani factor of , nahi . Yeh step kyun? Yeh parent ka "diminishing returns" fact hai: noise ki tarah girta hai, toh double karna kami kam help karta hai.
Verify: , , ; aur . Ratio . ✓ Cell C5 confirmed.
C6 — Epoch bookkeeping jab divisible by nahi hai
Forecast: — lekin aadha update ho nahi sakta. Kis taraf round karo?
- Full batches: full batches of 300 = 900 samples. Yeh step kyun? Har update exactly fresh samples consume karta hai.
- Leftover: samples bache hain — ek partial last batch. Yeh step kyun? Epoch finish karne ke liye humein saare data ek baar cover karne honge, toh remainder ko bhi apna update milta hai.
- Total updates: updates per epoch (3 full + 1 partial). Yeh step kyun? Ceiling, kyunki remainder phir bhi ek update trigger karta hai.
Verify: ✓, aur ✓. Contrast: Batch GD = 1 update/epoch, pure SGD = 1000 updates/epoch. Mini-batch 4 ke saath beech mein baitha hai.
C7 — Limiting behaviour: learning rate bahut bada ⇒ divergence
Forecast: bada = bade steps. Kya par settle hoga, ya hamesha ke liye uss se aage nikal jaayega?
- Step 1 gradient: . Update: . Yeh step kyun? Humne target overshoot kiya aur doosri side par land kiya ().
- Step 2 gradient par: . Update: . Yeh step kyun? Ab positive gradient wapas push karta hai — lekin same magnitude se, seedha par wapas.
- Diagnose: ping-pong karta hai Yeh kabhi converge nahi karta; loss hamesha high rehta hai.
Verify: par, ; par, — equal, koi progress nahi. ✓ Is quadratic ke liye stability boundary hai; hum exactly uس par baitha aur ek perpetual oscillation mili. Koi bhi bada aur yeh blow up hota. Isliye schedules aur Momentum and Adam jaisi methods exist karti hain.

C8 — Real-world aerospace word problem
Forecast: teeno us hi 2M samples ko per epoch touch karte hain. Toh kaun sa us pass ke andar sabse zyada update karta hai?
- Compute cost per epoch sabke liye same hai: saare = 2M samples ek baar touch karna seconds. 30 s ke andar bilkul. Achha — differentiator update count hai, total compute nahi. Yeh step kyun? Per epoch evaluate kiye gaye gradients ki sankhya hai har method ke liye; sirf aap unhe updates mein kaise group karte ho (via ) woh change hota hai.
- Batch GD: saare 2M ko 1 update mein group karta hai. Tum ko 2 seconds ke compute mein ek baar improve karte ho. Yeh step kyun? ⇒ .
- SGD: tiny updates — extreme lekin bahut jittery aur cache-unfriendly.
- Mini-batch : updates per epoch — bahut saare achhe steps, GPU-friendly. Yeh practical winner hai. Yeh step kyun? , ceiling (last batch mein samples hain).
Verify: s ✓ (< 30 s, toh sab feasible hain compute par). , remainder , toh ✓. Batch=1, SGD=2,000,000, mini-batch=7813 updates/epoch. Mini-batch har pass mein hazaron trustworthy steps deta hai — 80/20 sweet spot.
C9 — Exam twist: ke liye linear-scaling rule
Forecast: batch bada ho gaya. ko se multiply karo, ya se?
- Rule state karo. Linear scaling: , toh . Yeh step kyun? Bada ⇒ chhoti gradient variance ⇒ step zyada trustworthy hai, toh tum proportionally bolder step le sakte ho.
- Ratio compute karo: .
- Scale karo: . Yeh step kyun? Proportionality ka direct application.
Verify: ✓ aur ✓. (Sanity check: yeh linear rule hai, toh factor , nahi . Practice mein tum warm-up bhi add karte ho — dekho Learning Rate and Schedules — lekin exam answer hai.)
C10 — Thodi si noise saddle se kyun escape karaati hai
Forecast: par gradient hai... compute karo. Kya Batch GD kabhi move karega?
- Batch gradient at 0: . Update: . Hamesha ke liye stuck. Yeh step kyun? Flat spot par exact gradient exactly zero hai — Batch GD ke paas move karne ki koi wajah nahi, chahe ek real minimum nahi hai (loss ke liye girta rehta hai).
- SGD ka noisy estimate. Yaad karo (definitions se) SGD ka estimate hai, jahan sampling noise hai: mean zero, variance , jo kaun sa random sample draw hua usse aata hai. par mean part hai, lekin ka ek single draw generally nonzero hota hai — maano is draw se milta hai, toh . Update: . Yeh step kyun? Kyunki lekin , individual steps nonzero hote hain tab bhi jab true gradient vanish ho jaata hai. Woh nonzero kick exactly wahi hai jo ko flat spot se nudge karta hai — noiseless Batch GD ke liye impossible.
- Ab true gradient wapas aata hai aur humein downhill le jaata hai. par: , toh noiseless step bhi ab continue karta hai , aur negative ki taraf slide karta hai jahan (loss) decrease karta rehta hai. Yeh step kyun? Jab noise ne humein exact saddle se hata diya, gradient ab zero nahi hai, toh ordinary descent takeover karta hai aur hum real progress karte rehte hain. Noise ko sirf tie ek baar break karna tha.
Verify: (Batch saddle par frozen) ✓; ek noisy kick ✓; phir , giving next , jo se zyada negative hai ✓ — yaani abhi bhi downhill move kar raha hai. Conclusion: SGD ka zero-mean sampling noise parameter ko saddle se dislodge karta hai jahan Batch GD hamesha ke liye frozen baitha rehta; ek kick ke baad, deterministic descent kaam khatam karta hai. Yeh parent ka "thodi noise achhi hoti hai" concretely dikhaya gaya hai. Dekho Saddle Points and Non-Convex Optimization.
Recall Matrix par quick self-test
Kaun se cell(s) dikhate hain ki ki sign akele step direction decide karti hai? ::: C1 (positive) aur C2 (negative). C7 mein, kaun sa exact ke liye oscillation boundary par baitha hai? ::: . C6 mein, answer 4 updates kyun hai aur 3 kyun nahi? ::: 100 leftover samples ek partial final batch banate hain, toh hum ceiling lete hain . C5 mein, jaane par noise ka std kitne factor se cut hota hai? ::: (variance 9 se, std 3 se). aur kab coincide karte hain, aur kab differ karte hain? ::: Woh SGD ke liye coincide karte hain (, ek term ka average woh term hi hota hai); mini-batch aur Batch ke liye differ karte hain, jahan kaafi saare ko average karta hai. C10 mein, Batch GD par kyun freeze ho jaata hai lekin SGD nahi? ::: Wahan exact gradient 0 hai, toh Batch kabhi move nahi karta; SGD ka zero-mean noise ek nonzero kick deta hai jo flat spot se escape karaata hai.