Sampling and Generation from Trained VAEs
What you'll learn: How to generate brand new data by sampling from a trained VAE's latent space.
The Generation Process
Once your VAE is trained, you've essentially learned two things: how to compress data into a latent space, and how to reconstruct it. But here's the magic: you can create entirely new data points by sampling directly from the prior distribution (typically a standard normal distribution) and passing those samples through the decoder.
Think of it like this: the encoder taught the decoder what "valid" latent codes look like by regularizing them toward the prior (via KL divergence). Now you can draw random samples from that same prior distribution, and the decoder will transform them into realistic outputs—images, sounds, or whatever your data represents.
How It Works
- Sample from the prior: Draw a random vector
zfrom N(0, I)—a standard normal distribution - Decode: Pass
zthrough the trained decoder network - Get your output: The decoder produces a new, synthetic data point
In code (using concepts you've learned):
# After training your VAE
with torch.no_grad():
# Sample random latent vectors
z = torch.randn(batch_size, latent_dim)
# Generate new samples
generated_samples = decoder(z)
No encoder needed during generation! You're directly sampling the latent space you've carefully shaped during training.
Why This Works
The KL divergence regularization you applied during training forced your encoder's outputs to stay close to the prior N(0, I). This means the decoder learned to work with inputs that look like samples from this distribution. When you sample from it directly, you're exploring the smooth, continuous latent space your VAE learned.
Key Takeaway: Trained VAEs generate new data by sampling random vectors from the prior distribution and decoding them—no input data required, just pure generation from the learned latent space.