Variational Auto Encoder (VAE)
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).

Objective & Reparameterization
Instead of encoding an input into a single deterministic vector, a VAE encoder outputs distribution parameters—mean and standard deviation (often predicted as ). To sample while keeping the computation graph differentiable, the reparameterization trick moves randomness to an auxiliary noise variable:
Training minimizes the negative Evidence Lower Bound (ELBO):
Backpropagation in VAE Loss
-
Gradient Split Between Two Objectives
- Total loss gradients split cleanly across two complementary terms: the reconstruction loss (e.g., MSE or BCE between and ) and the KL divergence regularizer.
-
Gradients of the Reconstruction Loss (Updates Decoder AND Encoder)
- Gradients begin with respect to the reconstructed output and flow backward through the decoder parameters .
- Crucially, because is differentiable with respect to and , reconstruction gradients also continue flowing through back into the encoder parameters . Without this gradient path into the encoder, the latent representation would ignore and collapse to the prior!
-
Gradients of the KL Divergence (Regularizes the Encoder)
- The KL term acts directly on the encoder outputs and (for a diagonal Gaussian posterior vs. standard normal prior, ).
- Its gradients pull the encoded posterior distribution toward , preventing the encoder from collapsing variances to zero or scattering inputs into isolated pockets—making the latent space smooth and generative when sampling fresh .
