Skip to content
E
Educatum

Variational Auto Encoder (VAE)

By Educatum AI teamLast updated: Original walkthrough ↗
Key Intuition

A Variational Autoencoder (VAE) maps each input to a probabilistic latent distribution parameterized by mean mu and standard deviation sigma, sampling latent codes z = mu + sigma * epsilon via the reparameterization trick so gradients remain differentiable. Training optimizes the Evidence Lower Bound (ELBO), balancing a reconstruction loss (updating both decoder and encoder) against a KL-divergence term that regularizes the latent space toward N(0, I).

Reference Diagram & Materials
Variational Auto Encoder (VAE) matrix calculation diagram

Objective & Reparameterization

Instead of encoding an input xx into a single deterministic vector, a VAE encoder qϕ(z∣x)q_\phi(z \mid x) outputs distribution parameters—mean μ\mu and standard deviation σ\sigma (often predicted as log⁡σ2\log \sigma^2). To sample z∼N(μ,σ2I)z \sim \mathcal{N}(\mu, \sigma^2 I) while keeping the computation graph differentiable, the reparameterization trick moves randomness to an auxiliary noise variable:

z=μ+σ⊙ϵ,ϵ∼N(0,I)z = \mu + \sigma \odot \epsilon, \qquad \epsilon \sim \mathcal{N}(0, I)

Training minimizes the negative Evidence Lower Bound (ELBO):

LVAE(x)=−Eqϕ(z∣x) ⁣[log⁡pθ(x∣z)]⏟Reconstruction Loss+DKL ⁣(qϕ(z∣x) ∥ N(0,I))⏟KL Divergence Regularizer\mathcal{L}_{\text{VAE}}(x) = \underbrace{-\mathbb{E}_{q_\phi(z \mid x)}\!\left[\log p_\theta(x \mid z)\right]}_{\text{Reconstruction Loss}} + \underbrace{D_{\text{KL}}\!\left(q_\phi(z \mid x) \,\|\, \mathcal{N}(0, I)\right)}_{\text{KL Divergence Regularizer}}


Backpropagation in VAE Loss

  1. Gradient Split Between Two Objectives

    • Total loss gradients split cleanly across two complementary terms: the reconstruction loss (e.g., MSE or BCE between x^\hat{x} and xx) and the KL divergence regularizer.
  2. Gradients of the Reconstruction Loss (Updates Decoder AND Encoder)

    • Gradients begin with respect to the reconstructed output x^\hat{x} and flow backward through the decoder parameters θ\theta.
    • Crucially, because z=μ+σ⊙ϵz = \mu + \sigma \odot \epsilon is differentiable with respect to μ\mu and σ\sigma, reconstruction gradients also continue flowing through zz back into the encoder parameters ϕ\phi. Without this gradient path into the encoder, the latent representation would ignore xx and collapse to the prior!
  3. Gradients of the KL Divergence (Regularizes the Encoder)

    • The KL term acts directly on the encoder outputs μ\mu and σ\sigma (for a diagonal Gaussian posterior vs. standard normal prior, DKL=−12∑j(1+log⁡σj2−μj2−σj2)D_{\text{KL}} = -\frac{1}{2}\sum_j (1 + \log \sigma_j^2 - \mu_j^2 - \sigma_j^2)).
    • Its gradients pull the encoded posterior distribution toward N(0,I)\mathcal{N}(0, I), preventing the encoder from collapsing variances to zero or scattering inputs into isolated pockets—making the latent space smooth and generative when sampling fresh z∼N(0,I)z \sim \mathcal{N}(0, I).

VAE backpropagation diagram

More in GenAI by Hand

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.