Lesson 155 — Intermediate

The Generative Model Family: VAEs & GANs

Generative models ka poora khandan aaj ke diffusion pipelines ke peeche khada hai — bas unke tareeke alag hain. Is lesson mein tum do mental models ghar le jaoge: Compress → perturb → reconstruct (VAE) aur generate → judge → improve (GAN). Dono ek hi kaam karte hain — apne data ki distribution seekhna — aur har concept ke saath hand-checkable chhote numbers rahenge, taaki formula ratne ki zarurat na pade.

⏱ 10 min• Intermediate• Basic computer usage

Generative family — distribution seekhna hi asli kaam hai

Kya hai?

Discriminative model sikhata hai ki input ka label kya hai; generative model sikhata hai data kaise banta hai — uski distribution. Ek baar distribution andar aa gayi toh naye samples bana sakte ho. Is level ke teen pillars: autoencoder (compression ka base), VAE (uske upar distribution) aur GAN (do-player game) — diffusion inke baad aaya.

Kab kaam aata hai?

Jab output hona chahiye data ya andaza hona chahiye ki "normal kaisa dikhta hai" — anomaly detection, data augmentation, voice conversion, image synthesis. Sirf label chahiye toh classifier kaam hai, generative model ki zarurat nahi.

Kahan milta hai?

VAE compact latent representations aur anomaly detection mein, GAN image synthesis mein — aur diffusion pipelines ke andar ka VAE jo agle do lessons ka base hai. Lineage isliye interview currency hai kyunki har modern generator isi family se nikla hai.

Funda ek line mein: ye sab models apne data ki distribution ko andar approximate karte hain, aur generation ka matlab hai us approx distribution se ek naya sample kheenchna. Classifier ka sawaal hai "ye kis class ka hai", generative ka "aise data ke niyam kya hain" — isi farq se loss bhi badal jaata hai: reconstruction, adversarial ya denoising, teeno ek hi jagah point karte hain, data ki shakal samajhna.

Rule of thumb: Jab tak pata nahi ki output "data" hona chahiye ya "faisla", generative mat chuno. Classification, routing aur retrieval pehle try karo — generative tab, jab naye samples ya structure chahiye.

Autoencoder recap — bottleneck ka compression side effect hai

# autoencoder ka kaam — 4 values -> bottleneck 2 -> 4 (hand-checkable)
x     = [1.0, 0.0, 0.8, 0.2]
enc   = [0.9, 0.5]                 # bottleneck z
dec   = [0.95, 0.05, 0.75, 0.25]   # decoder ka reconstruction

# reconstruction loss (MSE, 4 values pe)
err  = [(a - b) ** 2 for a, b in zip(x, dec)]
loss = sum(err) / 4                # (4 * 0.0025) / 4 = 0.0025
print(loss)                        # chhota = bottleneck ne shakal rakh li
Autoencoder generate nahi karta: Bahut log sochte hain AE ka use karke seedha sample le lo — z ki jagah koi bhi random vector daalo. Wo training mein kabhi define hi nahi hua tha, isliye kaam nahi karta. Yahi gap VAE bharta hai.

VAE — latent space ko point ki jagah distribution bana do

from math import exp

# encoder ab do cheezein deta hai: mean (mu) aur spread (logvar)
mu, logvar = 0.4, -1.4

# reparameterization: z = mu + sigma * eps (sampling ko differentiable rakhta hai)
sigma = exp(0.5 * logvar)        # exp(-0.7) ~ 0.50
z     = mu + sigma * 0.3         # eps = 0.3 -> 0.4 + 0.5 * 0.3 = 0.55

# KL term: encoded distribution ko N(0,1) ke paas rakhta hai (hand-check)
kl = -0.5 * (1 + logvar - mu ** 2 - exp(logvar))   # ~ 0.40
print(round(z, 2), round(kl, 2))  # 0.55 0.40
KL weight ek dial hai: Zyada weight = shakal dheemi par space smooth; kam weight = reconstruction sharp par sampling bigadti hai. Dono extreme pe VAE ka faida hi khatam ho jaata hai — isliye beta tune karna training ka hissa hai.

GAN — generate karne wala aur judge karne wala ek hi game mein

# GAN ka scoreboard — sample batches ke saath
d_real, d_fake = 0.9, 0.2   # D: real pe 0.9 confident, fake pe 0.2
# D ka target: real -> 1, fake -> 0        (score abhi bilkul sahi hai)
# G ka target: d_fake ko 1 ke paas laana   (D ko fool karna)

# match ka signal: d_fake 0.5 ke paas aa jaaye
if abs(d_fake - 0.5) < 0.1:
    print("match chal raha hai — dono ek doosre se seekh rahe hain")

# failure 1: d_fake -> 0.1 par output hamesha ek hi chehra
#            -> G ek safe shortcut pe stuck = mode collapse
# failure 2: d_fake oscillate kare 0.1 / 0.9 beech beech
#            -> dono ek doosre ke peeche bhaag rahe, koi settle nahi
VAE vs GAN ek line mein: VAE distribution sikhata hai — smooth space, par soft output. GAN directly output pe ladta hai — sharp output, par training unstable. Do tareeke, ek hi problem.

Lineage — aaj ka diffusion inhi se nikla hai

autoencoder  -> bottleneck seekhna (compression + features)
   + VAE     -> latent = distribution            (sampling + smooth space)
   + GAN     -> adversarial judge                (sharp output, hard training)
   + diffusion -> dheere-dheere denoise karo      (stable loss, zyada compute)

aaj ke pipelines mein:
Stable Diffusion ka latent autoencoder = VAE (pixels <-> latent)
Denoising UNet + scheduler             = diffusion wala hissa
sharp/aligned output ki chahat          = GAN se aayi (adversarial losses aaj bhi)
Do line taiyaar rakho: VAE = compress → perturb → reconstruct; GAN = generate → judge → improve. Ye mental model hai, formula nahi — sun kar interviewer aage badhega.

Exercise

Question: Plain autoencoder ke bottleneck ko VAE kis cheez se badalta hai — point ya distribution?

Question: VAE mein mean ke saath doosra output sample karke z banane wale trick ka kya naam hai?

Question: GAN ke do players — generate karne wala aur judge karne wala, dono ke naam?

Question: GAN ka wo failure mode jismein model ek hi output pe baar baar jaake atak jaata hai? (ek shabd)

Question: Generator ko feedback kaun deta hai — kaunsa doosra network?

Common mistakes

Desi example: Ghar ka khana — compressor vs do-player game

Autoencoder matlab recipe ko chhote note mein likhna aur note se wapas khana banana. VAE note ki jagah "andaza" rakhta hai — thoda teekha, thoda kam — taaki koi bhi naya note sample banaya ja sake. GAN bilkul naya cook (generator) aur quality checker (discriminator) jaisa hai — cook banata hai, checker taste karke bolta hai, tab tak bolta rehta hai jab tak khana asli jaisa na lagne lage:

python
x    = ["dal", "chawal", "sabzi"]           # asli tiffin
note = {"teekha": 0.7, "namak": 0.4}       # VAE: point nahi, andaza (mu, sigma)
menu = note_se_naya_khana(note)            # decoding se naya menu bhi ban sakta hai

dish  = naya_khana(random_noise)           # GAN: naya cook roz naya dish
score = checker(dish)                      # 0.1 = pakka asli, 0.5 = confuse
if score < 0.5: cook.seekho()              # judge feedback dega — yahi training
Interview angle — yahan se sawaal aate hain
  • Q: Autoencoder aur VAE ka farq? — AE ek fixed point deta hai jiske beech ka space undefined; VAE distribution deta hai (mu, sigma), isliye sampling aur interpolation dono chalte hain.
  • Q: KL term kya karta hai, hata do toh kya hoga? — Encoded distribution ko prior ke paas rakhta hai; hata do toh holes ban jaate hain aur random sampling toot jaati hai.
  • Q: GAN training itni unstable kyun hai? — Do network ek doosre ke against optimum dhundhte hain; balance bigadne par mode collapse, oscillation ya vanishing gradient aate hain.
  • Q: VAE ki images aksar blurry kyun lagti hain? — Per-pixel reconstruction loss average ko prefer karta hai; sharpness ka direct term loss mein nahi hota.
  • Q: Modern diffusion pipeline mein ye family kahan baithi hai? — Latent autoencoder VAE ka (pixels <-> latent), denoiser diffusion ka — isi lineage se components bante hain.
Lesson complete?

Family ka base clear — ab tum bologe ki VAE distribution sikhata hai aur GAN judge se. Agla lesson — Diffusion Fundamentals: Learning to Denoise — mein dekhenge ki data ko dheere-dheere tabah karke wapas karne se generation kaise stable ho jaati hai.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course