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

Encoder Architecture Design for VAEs

Building convolutional or fully-connected encoders that output mean and log-variance for the latent distribution.

Encoder Architecture Design for VAEs

What you'll learn: How to build encoders that output both mean and log-variance parameters for your VAE's latent distribution.

The Core Challenge

In a VAE, the encoder doesn't just compress input into a single point—it outputs parameters of a distribution. Specifically, it produces two outputs: the mean (μ) and log-variance (log σ²) of a Gaussian distribution in latent space. Think of it like giving GPS coordinates plus an uncertainty radius, rather than just a single location.

Architecture Patterns

Fully-Connected Encoders

For simple data like MNIST, you can use dense layers that gradually compress dimensions:

  • Input → Hidden layers → Two parallel output heads
  • One head outputs μ, the other outputs log σ²

Convolutional Encoders

For images, use convolutional layers for spatial feature extraction:

  • Conv layers reduce spatial dimensions (e.g., 28×28 → 7×7 → flat)
  • Flatten → Two separate linear layers for μ and log σ²

Why Two Separate Outputs?

The encoder must learn where in latent space to place the encoding (μ) and how uncertain that placement should be (log σ²). Using log-variance instead of variance directly helps numerical stability—variances must stay positive, and working in log-space prevents negative values while avoiding tiny gradients.

The Split Architecture

Your encoder backbone processes features, then splits at the end into two heads with identical dimensions (your latent dimension z_dim). Both heads are learned during training—the model discovers not just good representations, but also appropriate uncertainty.

Key Takeaway: VAE encoders output two vectors—mean and log-variance—that parameterize the latent Gaussian distribution, typically using separate output heads after a shared feature extraction backbone.