Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our last lesson, we derived the full objective function for a Variational Autoencoder, the Evidence Lower Bound (ELBO), and decomposed it into two intuitive parts: a reconstruction term and a KL divergence regularizer. We ended with a critical cliffhanger. The reconstruction term, , involves an expectation over the approximate posterior . In practice, we estimate this expectation by sampling a latent vector from the distribution produced by our encoder.
This presents a fundamental problem for training. The parameters of our encoder, , define the very distribution we are sampling from. The sampling process itself is a stochastic, non-differentiable operation. How can we possibly use backpropagation to compute the gradient of the loss with respect to if there's a random "gap" in the computational graph?
This lesson introduces the elegant solution to this problem: the reparameterization trick. We will explore precisely why this trick is necessary from a first-principles perspective and then see how it masterfully restructures the network's data flow to enable end-to-end training with gradient descent.
The Problem: Gradients of Expectations
Let's formalize the issue. Our goal is to compute the gradient of the ELBO with respect to the encoder parameters, . The problematic part involves the reconstruction term:
where for simplicity, represents the reconstruction log-likelihood produced by the decoder.
If the expectation was over a distribution that did not depend on , say , we could simply swap the gradient and the expectation:
This resulting expectation can be easily approximated using Monte Carlo sampling: draw a few samples of from , compute the gradient for each, and average the results. Backpropagation does this automatically.
However, in our case, the distribution itself depends on . When we expand the definition of the expectation and apply the product rule for derivatives, we get a more complex expression:
The second term is fine and looks like the expectation of a gradient. But the first term, , is troublesome. It involves the gradient of the probability density function itself, , which we generally don't know how to compute or sample from. This means a naive Monte Carlo estimate of the gradient will be incorrect, as it would ignore this first term completely. This is the core mathematical reason why backpropagation fails here.
The following reading provides a more formal and rigorous explanation of this problem, cutting through the common but imprecise "we can't backpropagate through a random node" justification.
This blog post by Gregory Gundersen provides an excellent formal justification for the reparameterization trick.
Focus on the section Undifferentiable expectations. This part of the text mathematically demonstrates why taking the gradient of an expectation is problematic when the distribution itself is parameterized by the variables you're optimizing.
The Solution: Separating Randomness from Parameters
The insight behind the reparameterization trick is to re-express the process of sampling in a way that isolates the source of randomness.
Imagine you want to draw a number from a Gaussian distribution with a specific mean and standard deviation . One way is to sample directly from . Another, equivalent way is to:
- Sample a random number from a fixed, standard normal distribution, .
- Scale this number by .
- Shift it by .
The result, , is a random variable with the desired distribution . The key is that the randomness now comes from an external, fixed source (), while and are deterministic parameters that transform this randomness.
This is precisely the "trick" we apply in a VAE. Instead of having the encoder output a stochastic node , we have it output the deterministic parameters and . We then sample from a standard normal distribution and compute deterministically.
The video below provides a fantastic, high-level visual explanation of this process.
The video "The Reparameterization Trick" from the ML & DL Explained channel is dedicated to this single concept.
First, watch from the basic definitions to understand how sampling from any Gaussian can be simulated by transforming a sample from a standard Gaussian. Then, watch the next segment from how this helps, which uses flowcharts to show how this changes the network structure to make it differentiable.
Applying the Trick to the VAE
Let's see how this transforms our VAE's architecture.
Original (Problematic) Form:
- Input goes into the encoder network.
- The encoder outputs parameters and .
- A stochastic sampling operation draws .
- is passed to the decoder.
Reparameterized Form:
- Input goes into the encoder network.
- The encoder outputs deterministic parameters and .
- A random vector is sampled (this is considered an input to the system, not an internal operation).
- The latent vector is computed deterministically: , where denotes element-wise multiplication.
- This deterministic is passed to the decoder.
Notice the crucial change. The path from the parameters (which determine and ) to the final loss is now composed entirely of deterministic, differentiable operations. The randomness has been "pushed out" to an input node.


The Mathematical Payoff
Now let's revisit our gradient calculation. The reconstruction term was . With the trick, we can rewrite as a function . The expectation is no longer over , but over the fixed distribution of noise, .
Our gradient calculation becomes:
Since does not depend on , we can now safely swap the gradient and the expectation:
This expression is exactly what we need! It's an expectation of a gradient. We can get an unbiased estimate of it via Monte Carlo sampling:
- Sample from .
- Compute the deterministic value .
- Compute the reconstruction loss and its gradient via backpropagation.
- Average these gradients over a few samples (in practice, often just one sample per data point is sufficient).
The following resources provide a complete walkthrough, connecting the high-level intuition to the final, trainable loss function.
Variational Autoencoder - Model, ELBO, loss function and maths explained easily!
The video "Variational Autoencoder - Model, ELBO, loss function and maths explained easily!" by Umar Jamil clearly explains the challenge and the solution.
First, watch from the discussion on stochastic quantities to understand the problem of high variance and the inability to backpropagate. Then, continue from the introduction of the reparameterization trick to see how it solves this. Finally, the segment from how it is applied shows how this all comes together in the final VAE loss function.
Tutorial on Variational Autoencoders
This famous tutorial by Carl Doersch provides a very clear and concise explanation.
Read the section beginning with the solution. Pay close attention to Figure 4, which perfectly contrasts the non-differentiable and differentiable network structures.
Conclusion
In this lesson, we tackled the final major hurdle in training a Variational Autoencoder. We moved from an intuitive sense of "not being able to backpropagate through randomness" to a formal understanding of why the gradient of an expectation is problematic when the distribution's parameters are being optimized.
The reparameterization trick provides a mathematically sound and practical solution. By reframing the sampling process to isolate the source of randomness, it creates a fully differentiable path from the encoder's parameters to the final loss, enabling end-to-end training with standard backpropagation.
Key Takeaways:
- The Problem: Training a VAE requires taking the gradient of the ELBO with respect to the encoder parameters . This is problematic because the expectation in the reconstruction term is over a distribution that itself depends on , which prevents a simple interchange of the gradient and expectation operators.
- The Trick: The reparameterization trick rewrites the sampling of a latent vector as a deterministic transformation of a fixed noise variable . For a Gaussian VAE, this is , where .
- The Solution: This moves the stochasticity to an input layer, making the computational path from to the loss fully differentiable. The gradient of the expectation becomes the expectation of the gradient, which can be estimated via Monte Carlo sampling (i.e., backpropagation).
Preview of the Next Lesson:
Having mastered the architecture and training of VAEs, we have a complete understanding of a powerful generative model. Now, we will turn our attention to a completely different paradigm for generative modeling. In the next lesson, we will introduce Generative Adversarial Networks (GANs), exploring how an adversarial, two-player game between a generator and a discriminator can be used to produce stunningly realistic data.