4.5.3 · HinglishGenerative Models

Variational Autoencoders (VAE) theory

3,193 words15 min readRead in English

4.5.3 · AI-ML › Generative Models

#ai-ml/generative-models #unsupervised-learning #probabilistic-models #latent-variables

Overview

Variational Autoencoders (VAEs) generative models hain jo data ko ek latent space mein encode karna aur wapas decode karna seekhte hain, saath hi yeh ensure karte hain ki latent space ek jaani-pehchaani distribution (typically Gaussian) follow kare. Standard autoencoders ke unlike, VAEs probabilistic hote hain aur naye data ko sample karne ki suvidha dete hain.


Core Intuition

Hume probabilistic formulation ki zaroorat kyun hai?

  • Deterministic autoencoders latent space mein "memorize" kar sakte hain aur disconnected regions bana sakte hain
  • Hum latent space se sample karke naye samples generate karna chahte hain
  • Probabilistic interpretation hume reconstruction aur regularization ke beech balance karne ka ek principled tarika deta hai

Mathematical Foundation

The Generative Story

Gaussian prior kyun? Yeh mathematically convenient hai, infinite support hai, aur central limit theorem suggest karta hai ki bahut saari natural processes iske taraf converge hoti hain.

Problem: Intractable Marginal Likelihood

Hum maximize karna chahte hain:

Yeh mushkil kyun hai? Har data point ke liye, hume ALL possible latent codes par integrate karna padega yeh dekhne ke liye ki kaun se ise generate kar sakte the. High-dimensional ke saath, yeh integral computationally intractable hai.

Solution: Variational Inference

Ek inference network (encoder) introduce karo jo true posterior ko approximate karta hai.

ELBO ko scratch se derive karna:

Log-likelihood se shuru karo:

Andar se multiply karo:

Yeh par expectation hai:

Yeh step kyun? Jensen's inequality kehti hai ki kisi bhi random variable ke liye (log concave hota hai). Ise apply karo:

Logarithm ko split karo:

Doosra term negative KL divergence hai:

"Lower bound" kyun? Jensen se aaya inequality guarantee karta hai ki . ELBO maximize karna log-likelihood par ek lower bound ko upar dhakelta hai.

Gap: Approximate aur True Posterior ke Beech KL

Hum dikha sakte hain:

Kyun? Yahan se shuru karo:

Bayes rule use karo: :

Kyunki par depend nahi karta:

Kyunki KL divergence hamesha hoti hai, yeh confirm karta hai ki ELBO ek lower bound hai.


The Reparameterization Trick

Problem: Hume ke through gradients compute karne hain, lekin sampling differentiable nahi hai.

Maano .

Standard sampling (non-differentiable):

Intuition: Socho jaise "decision" (mean aur variance, jo parameters par depend karte hain) ko "randomness" (epsilon, jo nahi karta) se alag karna. Ab gradients decision part ke through flow kar sakte hain.


Practical VAE Architecture

Log variance predict kyun karte hain? Positivity ensure karta hai () aur numerical stability deta hai.

Practice Mein Loss Function

Dataset ke liye:

Monte Carlo estimate (typically 1 sample per data point):

jahan .

Gaussians ke Liye KL Divergence (Closed Form)

aur ke liye:

Derivation:

Multivariate Gaussian (dimension ) ke liye:

par expectations lete hue (use karke , ):

Subtract karo:


Worked Examples


Common Mistakes


Active Recall Checks

Recall VAEs Ko Ek 12-Saal-Ke-Bachche Ko Explain Karo

Socho tumhare paas ek magic box hai jo cat ki photos ko secret codes mein compress kar sakta hai (sirf kuch numbers), aur ek aur magic box hai jo un codes ko wapas cat photos mein badal sakta hai.

Ek regular magic box thoda messy ho sakta hai—kuch codes bahut door hain, kuch codes bilkul kaam nahi karte. Agar tum ek random code try karo, tumhe garbage mil sakta hai.

Ek VAE ek zyada smart magic box hai jo:

  1. Ensure karta hai ki saare secret codes ek pattern follow karein (jaise ek bell curve)
  2. Ensure karta hai ki paas-paas codes similar cats dein
  3. Koi empty spots nahi—har code jo tum try karo tumhe ek cat deta hai!

Trick yeh hai: Photos ko compress aur uncompress karna seekhte waqt, boxes codes ko nicely organize karna bhi seekhte hain. Ab tum:

  • Koi bhi random code do → ek naya cat photo pao jo real lagta hai
  • Do cat photos lo, unke codes mix karo, ek aisa cat pao jo beech-beech mein hai "Variational" part ka matlab hai hum probability aur smart math use kar rahe hain yeh ensure karne ke liye ki organization automatically ho jaye!

Memory Aids

Reparameterization: "Random ko learnable se alag karo"

  • Random: (standard normal, koi parameters nahi)
  • Learnable: aur (encoder outputs)

KL Divergence Terms: "My Variance Logically Matters"

  • Mean squared: (zero se door mean ki penalty)
  • Variance: (zyada spread ki penalty)
  • Log variance: (kam spread ki penalty)
  • Minus one: (normalization)

Connections

  • Autoencoders: VAEs autoencoders ko probabilistic formulation ke saath extend karte hain
  • Kullback-Leibler Divergence: Distribution mismatch measure karne ke liye core hai
  • Bayesian Inference: VAEs approximate posteriors ke liye variational inference use karte hain
  • Latent Variable Models: VAEs amortized inference ke saath latent variable models hain
  • Evidence Lower Bound (ELBO): Fundamental objective function
  • Reparameterization Trick: Gradient-based learning enable karta hai
  • Generative Adversarial Networks (GANs): Alternative generative model approach
  • Normalizing Flows: Probabilistic generative models ki ek aur family
  • Posterior Collapse: VAEs mein common training pathology
  • Disentangled Representations: Interpretability ke liye Beta-VAE aur doosre variants
  • Conditional VAE: Conditional generation ke liye extension
  • Importance-Weighted Autoencoders: Tighter ELBO bounds

#flashcards/ai-ml

Autoencoders aur VAEs mein fundamental difference kya hai? :: VAEs probabilistic hote hain (points ki jagah distributions encode/decode karte hain), inka latent space regularized hota hai (KL term prior enforce karta hai), aur prior se sampling karke generation enable karte hain.

ELBO ka full form kya hai aur iske do components kaun se hain?
Evidence Lower Bound. Components: (1) Reconstruction term - latent code diye gaye data ki expected log-likelihood, (2) KL divergence term - regularization jo posterior ko prior ke close rakhta hai.
Hume reparameterization trick ki zaroorat kyun hai?
Encoder distribution se standard sampling non-differentiable hai, encoder parameters mein gradient flow block karta hai. Reparameterization random component (ε) ko learnable parameters (μ, σ) se alag karta hai, sampling process ko differentiable banata hai.

Reparameterization trick ka formula likhо :: z = μ_φ(x) + σ_φ(x) ⊙ ε, jahan ε ~ N(0, I). Randomness ε mein hai (φ se independent), gradients ko μ aur σ ke through flow karne deta hai.

Posterior collapse kya hai aur yeh kyun hota hai? :: Jab KL term zero ho jata hai, encoder input ignore karta hai aur sabhi inputs ke liye prior distribution output karta hai. Hota hai jab decoder itna powerful hota hai ki bina z mein information ki zaroorat ke prior se seedha p(x) model kar sakta hai.

Diagonal Gaussian aur standard normal ke beech closed-form KL divergence likhо
D_KL(N(μ, σ²I) || N(0, I)) = (1/2) Σⱼ (μⱼ² + σ - log σⱼ² - 1)
ELBO ko "lower bound" kyun kaha jata hai?
Jensen's inequality (log E[X] ≥ E[log X]) ki wajah se, ELBO guaranteed hota hai ki ≤ log p_θ(x) hoga. ELBO maximize karna data log-likelihood par ek lower bound ko upar dhakelta hai.
KL divergence mein μ² term kya penalize karta hai?
Means jo zero (prior mean) se door hain. Encoder ko codes ko latent space ke origin ke around organize karne par majboor karta hai.
KL divergence mein -log σ² term kya encourage karta hai?
Kaafi variance rakhna encourage karta hai (bahut kam spread ki penalty). σ² term ke saath balance karta hai jo bahut zyada variance ko penalize karta hai.
Ek trained VAE se naye samples kaise generate karte hain?
(1) Prior se z ~ N(0, I) sample karo, (2) z ko decoder network p_θ(x|z) se pass karo, (3) Output distribution se x sample karo (e.g., binary images ke liye Bernoulli).
ELBO aur true posterior ke KL ke beech kya relationship hai?
log p_θ(x) = ELBO + D_KL(q_φ(z|x) || p_θ(z|x)). Kyunki KL ≥ 0, ELBO ≤ log p_θ(x). Tighter ELBO matlab better posterior approximation.
Hum typically σ² ki jagah log σ² predict kyun karte hain?
Positivity ensure karta hai (σ² = exp(log σ²) > 0 hamesha) aur numerical stability deta hai (optimization ke dauran bahut chhoti values se bachata hai).
KL annealing kya hai aur iska use kyun hota hai?
Training ke dauran KL weight ko β < 1 se β = 1 tak gradually increase karna. Posterior collapse rokne mein madad karta hai kyunki prior constraint enforce karne se pehle reconstruction seekhi ja sakti hai.
VAE objective mein, agar KL term bilkul hata do toh kya hoga?
Model standard autoencoder ban jata hai: latent space arbitrary/discontinuous ho sakta hai, codes bahut door, space mein holes, prior se sampling karke generate nahi kar sakte, overfit karne ka tendency hoti hai.
Ek VAE ki generative story kya hai?
(1) Latent code z ~ p(z) = N(0, I) sample karo, (2) Decoder network ke zariye x ~ p_θ(x|z) generate karo. Goal:

Concept Map

is a

uses

uses

maps x to

regularized to

reconstructs from

goal maximize

is

derives

approximates

enables

generates

Variational Autoencoder

Generative Model

Encoder q_phi z given x

Decoder p_theta x given z

Latent Space z

Gaussian Prior N 0 I

Intractable Marginal Likelihood

Variational Inference

ELBO Objective

Sample New Data