Variational Autoencoders (VAE)

अब हम Autoencoders की एक शक्तिशाली और probabilistic form को समझेंगे —
🔮 Variational Autoencoders (VAE)
जो deep generative models की दुनिया में एक foundation की तरह माने जाते हैं।


🔶 1. What is a Variational Autoencoder?

VAE एक तरह का Autoencoder है, जो ना केवल input को compress करता है,
बल्कि उसे एक probability distribution में encode करता है।

🎯 “VAE compress करता है input को एक distribution के रूप में, जिससे हम new data generate कर सकते हैं।”


🧠 2. Traditional Autoencoder vs VAE

FeatureAutoencoderVariational Autoencoder
OutputReconstruct inputReconstruct + Sample new
Latent VectorFixed valuesProbability distribution
GenerationNoYes (Generative model)
LearningDeterministicProbabilistic
RegularizationNoneKL Divergence

🔬 3. VAE Structure

Diagram:

        Input x

[Encoder Network]

Latent Distribution (μ, σ)

Sampling (z ~ N(μ, σ))

[Decoder Network]

Reconstructed x̂

🧮 4. Latent Distribution

VAE encoder predicts:

  • Mean vector μ
  • Log-variance log⁡σ2

From these, we sample latent variable

👉 इसे कहते हैं reparameterization trick
ताकि gradient backpropagation संभव हो।


🧮 5. VAE Loss Function

VAE का कुल loss दो हिस्सों से मिलकर बनता है:

L=Reconstruction Loss+ KL Divergence Loss

✅ Reconstruction Loss:

✅ KL Divergence Loss:


🔧 6. PyTorch Sketch

class VAE(nn.Module):
def __init__(self, input_dim, hidden_dim, latent_dim):
super(VAE, self).__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.mu = nn.Linear(hidden_dim, latent_dim)
self.log_var = nn.Linear(hidden_dim, latent_dim)
self.decoder = nn.Sequential(
nn.Linear(latent_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, input_dim),
nn.Sigmoid()
)

def encode(self, x):
h = F.relu(self.fc1(x))
return self.mu(h), self.log_var(h)

def reparameterize(self, mu, log_var):
std = torch.exp(0.5 * log_var)
eps = torch.randn_like(std)
return mu + eps * std

def forward(self, x):
mu, log_var = self.encode(x)
z = self.reparameterize(mu, log_var)
return self.decoder(z), mu, log_var

📊 7. Applications of VAE

ApplicationDescription
✅ Image GenerationNew samples create करना
✅ Data ImputationMissing values भरना
✅ Representation LearningCompressed features
✅ Anomaly DetectionRare patterns पकड़ना
✅ Drug DesignMolecule generation

🎨 8. Generated Sample Example (MNIST)

Train VAE on MNIST, then sample new digits by:

z = torch.randn(1, latent_dim)
generated = model.decoder(z)

👉 ऐसा output real handwritten digit जैसा दिखेगा, despite not being in the original dataset.


📝 Practice Questions:

  1. VAE क्या है और Autoencoder से कैसे अलग है?
  2. Latent vector के लिए reparameterization क्यों ज़रूरी है?
  3. VAE का loss function किन भागों में बँटा होता है?
  4. KL divergence का क्या उद्देश्य होता है?
  5. VAE से synthetic data कैसे generate किया जाता है?

🎯 Summary

FeatureDescription
VAEProbabilistic autoencoder
Encoder Outputμ और σ (mean & std)
Samplingz = μ + σ * ε
LossReconstruction + KL divergence
Use CasesGeneration, anomaly detection, compression