Selected topic
Variational Autoencoders
Prefer practical output? Use related tools below while reading.
x to a latent representation z.z back to the original input space.E(x)): x → zμ and σ²
D(z)): z → xELBO = E[log p(x|z)] - KL[q(z|x)||p(z)]
where:
E[log p(x|z)]: reconstruction term, measures how well the VAE can reconstruct the inputKL[q(z|x)||p(z)]: Kullback-Leibler divergence between the approximate posterior distribution q(z|x) and the prior distribution p(z), encourages the VAE to learn a meaningful representationLoss = -ELBO
The VAE is trained by minimizing this loss function.
python
import torch
import torch.nn as nnclass Encoder(nn.Module):
def __init__(self, input_dim=784, hidden_dim=256, latent_dim=2):
super(Encoder, self).__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, latent_dim*2) # mean and log variance
def forward(self, x):
x = torch.relu(self.fc1(x))
z_mean_logvar = self.fc2(x)
return z_mean_logvar
class Decoder(nn.Module):
def __init__(self, latent_dim=2, hidden_dim=256, output_dim=784):
super(Decoder, self).__init__()
self.fc1 = nn.Linear(latent_dim, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, output_dim)
def forward(self, z):
x = torch.relu(self.fc1(z))
x = torch.sigmoid(self.fc2(x))
return x
# Initialize the VAE
vae = VariationalAutoencoder(Encoder, Decoder)
# Train the VAE using SGD and ELBO as loss function
optimizer = torch.optim.Adam(vae.parameters(), lr=0.001)
for epoch in range(100):
# Forward pass
z_mean_logvar = vae.encoder(x)
x_reconstructed = vae.decoder(z_mean_logvar)
# Compute the ELBO
elbo = -vae.loss_function(x, z_mean_logvar)
# Backward pass and update parameters
optimizer.zero_grad()
elbo.backward()
optimizer.step()
# Example use case: generate new samples from the learned distribution
new_samples = vae.decoder(z_mean_logvar)