3.2.1 · HinglishTraining Deep Networks

Stochastic gradient descent (SGD)

1,835 words8 min readRead in English

3.2.1 · AI-ML › Training Deep Networks


WHAT is SGD?

Teen flavors, batch size ke hisaab se:

  • (pura data) → Full-batch / Batch GD
  • pure (online) SGD
  • mini-batch SGD (yahi sab actually use karte hain)

WHY does averaging a random sample work? (Derivation from scratch)

Asli objective, size ki poori dataset par average loss hai:

Step — ek estimator banao. examples ka ek mini-batch uniformly at random chuno. Define karo:

Yeh step kyun? Hum ek sasta substitute chahte hain expensive full gradient ke liye.

Step — dikhao ki yeh unbiased hai. Kisi bhi randomly chosen index ke liye, aisi i.i.d. draws ka average karne par mean same rehta hai:

Yeh step kyun? Unbiasedness ka matlab hai "average par hum sahi (steepest-descent) direction mein point karte hain." Toh expectation mein, SGD bilkul wahi karta hai jo GD karta hai.

Step — noise ko quantify karo. Mini-batch estimator ki variance ke saath shrink hoti hai: jahan per-example gradient variance hai.

Yeh step kyun? Yeh central trade-off hai: bada → kam noise par har step mein zyada cost; chota → sasta, noisy steps. (na ki ) ka matlab hai batch double karne se noise sirf half hoti hai → diminishing returns, aur yahi reason hai ki huge batches automatically better nahi hote.

Robbins–Monro kyun? → tum phir bhi koi bhi distance travel kar sakte ho minimum tak pahunchne ke liye. → step sizes itni tezi se shrink hoti hain ki noise eventually average out ho jaati hai aur tum bouncing band kar dete ho. Ek classic choice: .


Figure — Stochastic gradient descent (SGD)

HOW to run it (algorithm)

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

Ek epoch = saare examples ka ek full sweep = updates.


Worked Examples


Common Mistakes (Steel-manned)


Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho tum ek bade foggy valley mein sabse neecha point dhundh rahe ho, sirf apne paon ke neeche ki zameen ki slope feel karke. Poori valley ki slope ek saath check karna bahut thaka dene wala hai. Iske bajaye, tum sirf woh chota sa patch check karte ho jahan tum khade ho aur neecha ek step lete ho. Har patch thoda misleading hota hai, toh tum wobble karte ho — par agar tum step karte rehte ho aur dhire dhire chote steps lete ho, toh saari wobbling cancel ho jaati hai aur tum bottom par pahunch jaate ho. Ek chota patch check karna fast hai, toh tum tons of steps lete ho aur wahan jaldi pahunchte ho. Woh wobble tumhe choti khaiyon ko skip karne mein bhi madad karta hai jo asli bottom nahi hai.


Flashcards

SGD mein "stochastic" ka kya matlab hai?
Gradient ek randomly sampled mini-batch se estimate hota hai, jo har update ko true gradient ka ek noisy random estimate banata hai.
SGD update rule likho.
Kya mini-batch gradient, full gradient ka biased estimator hai?
Nahi — yeh unbiased hai: (uniform random sampling).
Mini-batch size gradient-estimate variance ko kaise affect karta hai?
Variance ke roop mein scale hoti hai; std-dev ke roop mein → bade ke liye diminishing returns.
Convergence ke liye Robbins–Monro conditions batao.
(min tak pahunch sako) aur (noise average out ho jaaye).
Constant learning rate exact convergence kyun rokti hai?
Gradient noise kabhi gayab nahi hoti, toh parameters minimum ke aas-paas radius ki ball mein bounce karte rehte hain.
Updates ke terms mein ek epoch kya hota hai?
updates — saare training examples ka ek full pass, size ke batches mein.
SGD ki noise kabhi kabhi beneficial kyun hoti hai?
Yeh saddle points/sharp minima se escape karne mein madad karta hai aur ek implicit regularizer ka kaam karta hai jo flat, better-generalizing minima ki taraf bias karta hai.
Har epoch data shuffle kyun karo?
Mini-batch draws ko approximately i.i.d. rakhne ke liye aur ordered data ki wajah se correlated/biased updates se bachne ke liye.
Linear scaling rule kya hai?
Jab batch size ko factor se badhao, toh training dynamics similar rakhne ke liye learning rate ko bhi se scale karo.

Connections

Concept Map

exact but slow

sampled estimate

samples

averages gradients

proven

scales as

drives

points right way

sets step size

converges under

noise helps

special case m equals N

Loss L theta over N examples

Full-batch GD exact gradient

Stochastic Gradient Descent

Random mini-batch B size m

Estimator g_B

Unbiased E of g_B equals grad L

Variance sigma^2 over m

Noise vs cost trade-off

Update rule theta minus eta g_B

Learning rate eta

Robbins-Monro conditions

Escapes bad regions