Course contentsShow
← Machine Learning and Deep Learning
Lesson 1466 of 3,53832. Generative Models: Autoencoders and VAEsFree lesson

Sampling and Generation from Trained VAEs

Drawing samples from the prior distribution and decoding them to generate new data points.

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

  1. Sample from the prior: Draw a random vector z from N(0, I)—a standard normal distribution
  2. Decode: Pass z through the trained decoder network
  3. 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.