33 — Generative & Prob.

VAE — Variational Autoencoder#

Generative✓ Mathematical
◆ The PatternLearning a structured latent space for generation and interpolation

A VAE encodes inputs to a distribution over latent vectors, then decodes samples. The KL term forces a smooth, continuous latent space.

ELBO = E[log p(x|z)] − KL(q(z|x) || p(z))
ELBO = reconstruction + KL regularization
z = μ + σ·ε    ε ~ N(0,1)
Reparameterization trick: makes sampling differentiable
// VAE architecture diagram
class VAE(nn.Module):
    def forward(self, x):
        mu, log_var = self.encode(x)
        z = mu + torch.exp(0.5*log_var) * torch.randn_like(mu)
        return self.decode(z), mu, log_var

def vae_loss(recon_x, x, mu, log_var):
    recon = F.mse_loss(recon_x, x)
    kl    = -0.5 * torch.mean(1 + log_var - mu**2 - log_var.exp())
    return recon + kl
Pattern bridge: Encoding data into a structured latent space, then decoding it. The KL term is a normal distribution regularizer — pulling the latent space toward Gaussian structure.
← Previous
SVD
Open in the full reader, with the topic sidebar →