loading

Variational Autoencoder — Step-by-Step Visualization

hardAIMLGenerative AIDeep LearningVAE

Step through a VAE — watch the encoder compress input to a latent distribution (μ, σ), reparameterize to sample z, then decode back to reconstruct the input.

Algorithm Pattern

Encode → Sample → Decode

Key Idea

Unlike a standard autoencoder, VAE encodes to a DISTRIBUTION (μ, σ²) not a point. The reparameterization trick (z = μ + σ·ε) keeps gradients flowing while allowing stochastic sampling.

Step-by-Step Approach

  1. Encoder maps x → μ and log(σ²) — the parameters of a Gaussian.
  2. Reparameterize: z = μ + σ·ε where ε ~ N(0,1) — enables backprop through sampling.
  3. Decoder maps z → x̂ (reconstruction).
  4. Reconstruction loss: MSE(x̂, x) — how well we rebuilt the input.
  5. KL loss: −0.5·Σ(1 + log(σ²) − μ² − σ²) — regularizes latent space toward N(0,1).

Common Gotchas

  • Without KL loss, the encoder collapses to a point — equivalent to a standard autoencoder.
  • The reparameterization trick is what makes VAEs trainable end-to-end.
  • VAE latent spaces are smooth — interpolating between two z vectors produces valid outputs.

Related Problems