5.6.10 · D4 · HinglishMachine Learning (Aerospace Applications)

ExercisesBatch, mini-batch, stochastic gradient descent

3,628 words16 min read↑ Read in English

5.6.10 · D4 · Coding › Machine Learning (Aerospace Applications) › Batch, mini-batch, stochastic gradient descent

Parent note se ek update rule pe sab kuch tika hai:

Yahan (theta) wo parameter hai jo hum tune karte hain, (eta) learning rate hai (step kitna bada ho), aur ("g-hat") estimated downhill direction hai. Neeche har symbol inhi mein se ek hai. Agar koi unfamiliar lage to Gradient Descent aur Learning Rate and Schedules dekho.


Level 1 — Recognition

L1.1 · Flavour pehchano

images ke dataset ke liye, aap har update se pehle saare 60,000 examples use karke gradient compute karte ho. Batch / SGD / Mini-batch mein se kaun sa hai yeh?

Recall Solution

Batch size , ke barabar hai (). Parent definition ke anusaar == matlab Batch GD== hai. Yeh har step pe exact average gradient use karta hai — zero noise, maximum cost per step.

L1.2 · Batch size padhna

Ek engineer likhta hai: "Main 64 random samples leta hoon, unke gradients average karta hoon, phir step leta hoon." kya hai, aur yeh kaun sa flavour hai?

Recall Solution

. Kyunki (yeh maante hue ki dataset 64 se bada hai), yeh Mini-batch GD hai. 64 gradient terms ka average ek unbiased estimate hai jiska noise (variance , jahan upar define ki gayi per-sample gradient variance hai) ek single sample se 64 guna kam hai.

L1.3 · Noise match karo

Batch, Mini-batch (), aur SGD ko gradient estimate ke least noisy se most noisy order mein rank karo.

Recall Solution

Estimate variance hai (yaad karo per-sample gradient variance hai), isliye bada = kam noise:

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

Level 2 — Application

L2.1 · Ek SGD step

fit karo squared loss se, jahan input hai aur sample ka target hai. Data point , start , learning rate . Ek SGD step ke baad naya compute karo.

Recall Solution

Gradient (chain rule): . Update: . Kyun upar? Gradient negative hai, isliye iske against step lena (negative subtract karna) ko true value ki taraf badhata hai.

L2.2 · Ek Batch step

Wohi model. Ab dono points use karo: add karo, aur rakhho. Batch update compute karo.

Recall Solution

(L2.1 se). . Batch gradient ( ka average): . Update: . Average kyun, sum nahi? Batch GD mean loss gradient estimate karta hai, isliye do alag-alag samples ( aur ) blend hokar ek trustworthy direction bante hain. SGD se step chhota kyun? Mild sample () average ko zero ki taraf khichta hai, isliye batch ek gentle, less jumpy step () leta hai — jo akele single hot sample se zyada hota () — exactly wo smoothing jo kam noise deti hai.

L2.3 · Epoch bookkeeping

Tumhare paas samples hain aur tum choose karte ho. Ek epoch (saare data ka ek poora pass) mein kitne parameter updates hote hain? 10 epochs mein kitne?

Recall Solution

Updates per epoch . 10 epochs mein updates. Kyun? Ek epoch ka matlab hai har sample ko ek baar dekhna; har mini-batch samples consume karta hai, isliye data cover karne ke liye batches chahiye.

L2.4 · Batch size se variance

Per-sample gradient variance hai. SGD (), mini-batch , aur mini-batch ke liye compute karo.

Recall Solution

use karo.

  • : .
  • : .
  • : . ko 4 se 16 tak quadruple karne se variance 4× cut hua — saaf shrinkage.
Figure — Batch, mini-batch, stochastic gradient descent

L2.5 · Jab , se divisible nahi ho

Tumhare paas samples hain aur tum pick karte ho. Ek epoch mein kitne mini-batches hote hain, aur har ek kitna bada hai?

Recall Solution

, jo poora number nahi hai. Tum full batches of 250 banate ho (1000 samples use karke), baaki remainder samples reh jaate hain. Standard practice: size 30 ka ek extra chhota last mini-batch banao. To epoch mein updates hoti hain: chaar ke saath aur ek ke saath. Yeh kyun matter karta hai? Us last batch ki variance zyada hai ( vs ), isliye uska step noisier hai — kuch libraries remainder ko drop karne deti hain (drop_last=True) taaki har step equally clean rahe. Dono tarike se, epoch per updates ki sankhya hai.


Level 3 — Analysis

L3.1 · Wall-clock, step count nahi

Maan lo ek sample ka gradient compute karne mein unit time lagta hai. Dataset . Per epoch compare karo: (a) time cost, (b) number of updates, Batch vs. mini-batch ke liye. Kaun zyada progress per unit time karta hai, aur kyun?

Recall Solution

Per epoch dono methods saare 1000 samples ek baar process karte hain, isliye dono ka cost time units per epoch hai (same total gradient computations).

  • Batch: update per epoch.
  • Mini-batch : updates per epoch. Same compute, lekin mini-batch 20 course-corrections karta hai jabki Batch sirf 1. Har mini-batch step "good enough" hai (unbiased, moderate noise), isliye 20 good-enough steps typically loss ko ek perfect step se kahin zyada neeche lete jaate hain. Isliye mini-batch wall-clock time mein jeetta hai, bhaale uski per-step direction kam exact ho.

L3.2 · Jab noise help karta hai

Non-convex loss pe do runs (dekho Saddle Points and Non-Convex Optimization): Run A Batch GD use karta hai, Run B SGD. Run A flat loss curve ke saath ruk jaata hai; Run B ka loss wiggle karta hai lekin girta rehta hai. Explain karo ki Run A ke saath kya hua hoga aur Run B kyun escape kar paaya.

Recall Solution

Run A (Batch) shayad ek saddle point ya shallow local minimum pe atak gaya: wahan exact gradient hota hai, isliye update barely move karta hai — yeh stall ho jaata hai. Run B (SGD) ka noisy estimate hai jiska variance nonzero hota hai even jab ho. Yeh random kick ko flat saddle se off push karta hai kisi slope pe jahan gradient phir se informative hai. To noise, precision ke liye harmful, flat traps escape karne ke liye helpful hai. Yahi central reason hai ki noisy small-batch methods aerospace ML ke non-convex surrogate models ke liye prefer ki jaati hain.

L3.3 · pe diminishing returns

Per-sample variance hai. Standard deviation mein reduction compute karo jaane pe, aur alag se jaane pe. 80/20 point pe comment karo.

Recall Solution

Standard deviation .

  • : . Drop of (half ho gaya).
  • : . Drop of . Same 4× increase in , lekin pehli jump doosri se 8× zyada noise remove karti hai. Kyunki noise ki tarah girta hai, ki early increases hugely pay off karti hain aur baad ki barely kuch kharti hain — yahi 80/20 sweet spot hai jo ko justify karta hai.

Level 4 — Synthesis

L4.1 · Training budget design karo

Tumhare paas CFD drag samples hain, exactly 1000 gradient computations total ka compute budget hai, aur tum variance rakhte hue zyada se zyada parameter updates chahte ho. Per-sample variance hai. choose karo aur un updates ki sankhya compute karo jo tum afford kar sakte ho.

Recall Solution

Constraint 1 (variance): chahiye. To smallest allowed batch hai. Constraint 2 (budget): har update gradient computations cost karta hai; total budget 1000 hai. Updates . Updates maximise karne ke liye hum smallest legal chahte hain, yaani . Updates updates. Smallest legal B kyun? Kam computations per step matlab same budget mein zyada steps, aur noise cap ko exactly satisfy karta hai. Koi bhi variance requirement todega; koi bhi unnecessary noise reduction pe budget waste karega.

L4.2 · Linear scaling rule in action

Ek model aur ke saath stably train karta hai. Tum bigger GPU use karne ke liye switch karte ho. Linear scaling rule use karke, kya learning rate use karni chahiye, aur kyun ab bada safe hai?

Recall Solution

Ratio hai, isliye learning rate 8 se scale karo: . Kyun safe? Bada gradient variance shrink karta hai ( vs — 8× kam). Clean, more trustworthy direction ke saath, tum noise ki wajah se raaste se bhatke bina bolder step le sakte ho. ke saath rakhne se low-noise estimate tiny, over-cautious steps pe waste hogi. Zyada saaf gradient ⇒ bolder step. Adaptive alternatives ke liye Momentum and Adam dekho.

L4.3 · Multi-step mini-batch trace

Model , loss (input , target ), , start . Do mini-batches: Batch A , Batch B . Pehle A pe ek update karo, phir B pe (isi order mein). Har ke baad do.

Recall Solution

Update 1 (Batch A, ): . 1 sample pe mean . . Update 2 (Batch B, ), ab : . . Mean . . Parameter climb karta hai ki taraf, doosra (averaged) step pehle se smoother hai.


Level 5 — Mastery

L5.1 · Convergence neighbourhood & schedules

Rigorously explain karo ki constant- SGD sirf minimum ke ek neighbourhood tak kyun converge karta hai, exact point tak nahi, aur ek learning-rate schedule ise kaise fix karta hai. Update rule se illustrate karo.

Recall Solution

True minimum pe, full gradient hota hai, lekin ek single-sample estimate ki wahan bhi nonzero variance hoti hai. To update phir bhi typical size se random move karta hai. Iterate kabhi settle nahi karta; yeh ke around radius ki ball ke andar bounce karta rehta hai. Bada ⇒ badi ball. Fix — ek schedule (e.g. ): jaise steps badhte hain, bounce amplitude hoti hai, isliye neighbourhood shrink hokar ek point ban jaata hai. Classic Robbins–Monro conditions (kaafi door travel karo) aur (noise damp out ho) tak convergence guarantee karte hain. Learning Rate and Schedules dekho.

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

Large batches often small ones se zyada bura generalise karte hain, training loss kam hone ke bावजूद. Reason through karo kyun, batch noise, minima geometry, aur Bias-Variance Tradeoff ko connect karke.

Recall Solution

Ek loss surface mein sharp minima (narrow, steep walls) aur flat minima (wide basins) hote hain. Sharp minima training set ko tightly fit karte hain lekin train aur test data ke beech ek chhoti si shift tumhe ek steep wall pe le jaati hai → bada test loss (poor generalisation). Flat minima robust hote hain: nearby points ka loss bhi kam hai, isliye train/test shift barely hurt karta hai. Batch size control karta hai ki tum kaun sa paate ho. Large-batch gradients almost noise-free hote hain, isliye optimiser seedha nearest minimum mein slide kar jaata hai — aksar ek sharp wala. Small-batch noise (variance ) ek random kick ki tarah act karta hai jo ek narrow sharp basin ke andar nahi reh sakta — yeh shake out ho jaata hai aur sirf wide flat basins ise hold kar sakti hain. Isliye small batches implicitly flat, generalising minima ki taraf bias karte hain. Bias–variance terms mein: noise ek form of regularisation hai jo thodi si training-set fit (slightly higher bias) trade karta hai kahin better test performance ke liye (learned function ki lower variance). Isliye practice mein mid-size batches dominate karte hain, huge wale nahi.

L5.3 · Aerospace pipeline ke liye method choose karo

Ek surrogate neural net wing geometry se drag predict karta hai. Training data continuously live wind-tunnel sensors se stream in ho raha hai (tum sab store nahi kar sakte). Kaun sa method — Batch, Mini-batch, ya SGD — aur kyun? Memory, noise, aur streaming nature address karo.

Recall Solution

Batch GD impossible hai: ise form karne ke liye memory mein saare samples chahiye, lekin data stream in hota rehta hai aur fully store nahi ho sakta — effectively unbounded/growing hai. Pure SGD () streaming ke saath kaam karta hai (per sample update) lekin bahut noisy hai aur har GPU pass ko under-use karta hai. Mini-batch sahi choice hai: incoming stream ka ek chhota window buffer karo (e.g. ), update karo, discard karo, repeat karo. Yeh (a) memory mein fit hota hai, (b) ek stable-enough direction ke liye variance tak reduce karta hai, (c) online/streaming setting ke liye suit karta hai, aur (d) woh useful noise rakhta hai jo non-convex surrogate landscape ke saddle points se escape karti hai. Ek decaying schedule ke saath pair karo taaki early exploration precise convergence mein badal sake, aur noisy stream ko aur smooth karne ke liye Momentum and Adam consider karo.


Parent note pe wapas jao · related: Gradient Descent, Backpropagation, Loss Functions.