This is a comprehensive question about Variational Autoencoders (VAEs). I'll break it down into parts:
- Derive the VAE ELBO
- Explain the reparameterization trick
- Explain why it's necessary for backpropagation
Let me work through this systematically.
Part 1: Derive the VAE ELBO
Starting from first principles:
We have:
- Model: p(x|z) (decoder) and p(z) (prior)
- Inference: q(z|x) (encoder) - our variational approximation to the intractable p(z|x)
The evidence for a data point x is:
p(x)=∫p(x∣z)p(z)dz
Taking log:
logp(x)=log∫p(x∣z)p(z)dz
By Jensen's inequality (or by introducing a probability distribution q):
logp(x)=log∫p(x∣z)p(z)
=logEq(z∣x)[
By Jensen's inequality:
≥Eq(z∣x)[log
=Eq(z∣x)[logp(
=Eq(z∣x)[logp(x∣z)]−
L(x)=Eq(z∣x)[log
This lower bound on the log evidence consists of a reconstruction term—how well the decoder recovers x from z—minus a regularization term that pushes the encoder's distribution toward the prior. The difference between the true log evidence and this bound is exactly KL(p(z|x)||q(z|x)), which vanishes when the encoder perfectly matches the true posterior.
To optimize this objective, I need to maximize the reconstruction accuracy while keeping the encoder's distribution close to p(z). But there's a fundamental problem: computing gradients through the expectation over q(z|x) when q's parameters are what I'm trying to optimize. The reparameterization trick addresses this by decoupling the randomness from the parameters, allowing me to differentiate through the sampling process itself. Eϵ∼N(0,I)[∇
Using the chain rule, this becomes:
Eϵ∼N(0,I)[∇zf(z)∇
where z = μ(x;θ) + σ(x;θ) ⊙ ε. This allows gradients to flow through the deterministic transformation of the parameters, while the stochasticity remains isolated in ε.
Without this reparameterization, backpropagation fails because sampling operations don't have well-defined gradients—the computational graph breaks at the stochastic node. Reparameterization fixes this by making the parameters flow through a deterministic path, letting autodiff compute gradients end-to-end. We express the randomness in a parameter-independent way—ε remains constant during optimization while the parameters only shift the location (μ) and scale (σ), preserving the computational graph for backpropagation.
Let me verify the ELBO derivation. Starting from the marginal likelihood and introducing a variational approximation q(z|x), I can decompose it into the ELBO plus a KL divergence term. Since KL divergence is always non-negative, the ELBO becomes a lower bound on the log marginal likelihood. Expanding p(x,z) as p(x|z)p(z), the objective becomes maximizing the expected log likelihood under the encoder distribution minus the divergence between the variational and prior distributions.
Variational Autoencoder (VAE) ELBO Derivation
Part 1: Deriving the ELBO
Starting from the Intractable Evidence
We want to maximize the log evidence for data x:
logp(x)=log∫zp(x,z)dz
This integral is intractable. We introduce a variational distribution q(z|x) to help:
Applying Jensen's Inequality
logp(x)=∫zq(z∣x)logp(x)d
=∫zq(z∣
=ELBO
Since KL divergence is non-negative, we have:
logp(x)≥L(x)=E
Decomposing the ELBO
Using p(x,z)=p(x∣z)p(z):
L(x;θ,
Interpretation:
- Reconstruction term: Decoder should reconstruct x from z
- KL term: Encoder q should stay close to prior p(z), typically N(0,I)
Part 2: The Reparameterization Trick
The Problem: Non-Differentiable Sampling
Naively, we want to optimize:
∇ϕEz∼qϕ(z
Issue: We need gradients w.r.t. φ, but z is sampled from a distribution parameterized by φ. Sampling breaks the computational graph—backpropagation cannot flow through randomness!
The Solution: Reparameterize the Randomness
Express z as a deterministic transformation of parameter-independent randomness:
z=μϕ(x)+σ
Where:
- μϕ(x): mean from encoder (depends on φ)
- σϕ(x): standard deviation from encoder (depends on φ)
- : standard normal noise (independent of φ)
Transformed Expectation
Ez∼q
Now ε is constant during optimization—only μ and σ depend on φ!
Computing Gradients
By chain rule:
∇ϕEϵ[
=Eϵ[∇z
This is now fully differentiable!
Part 3: Why Necessary for Backpropagation
| Aspect | Without Reparameterization | With Reparameterization |
|---|
| Sampling | z ~ q(z|x) | z = μ + σ⊙ε; ε ~ N(0,I) |
| Dependency | Distribution depends on φ | Only deterministic params depend on φ |
| Computational Graph | ❌ Broken—sampling is non-differentiable | ✅ Intact—randomness is parameter-free |
| Gradient Flow | ❌ Cannot backprop through random sampling | ✅ Can differentiate μ, σ w.r.t. φ |
| Implementation | Would need score function estimators (high variance) | Direct gradient estimates |
Concrete Example
Without reparameterization:
x → [Encoder] → (μ, σ)
↓ STOCHASTIC SAMPLE (no gradient!)
z → [Decoder] → x̂ → Loss
↑
Cannot backprop here ✗
With reparameterization:
x → [Encoder] → (μ, σ) ──┐
├→ z = μ + σ⊙ε → [Decoder] → x̂ → Loss
ε ~ N(0,I) ─────────────┘ ↓
Can backprop through μ,σ ✓
The reparameterization trick is essential for making VAEs trainable with gradient descent via standard autodiff frameworks (PyTorch, TensorFlow).