Lensa ML
Lensa ML

How Autoencoders & VAEs Work

Step 1 of 6

The Intuition

What does an autoencoder learn?

inputencoderSmile:0.4Eye size:0.0Face width:0.0Glasses:-0.6latent attributesdecoderoutput
Smile0.4
Eye size0.0
Face width0.0
Glasses-0.6
Attributes
4
Active
1
Dims
4

An autoencoder compresses high-dimensional data (like a face) into a small set of latent attributes. The decoder reconstructs the original from just these few numbers. Drag the sliders to see how each attribute controls a different aspect of the face.

Step 1 of 6: The Intuition

What does an autoencoder learn?

inputencoderSmile:0.4Eye size:0.0Face width:0.0Glasses:-0.6latent attributesdecoderoutput
Smile0.4
Eye size0.0
Face width0.0
Glasses-0.6
Attributes
4
Active
1
Dims
4

An autoencoder compresses high-dimensional data (like a face) into a small set of latent attributes. The decoder reconstructs the original from just these few numbers. Drag the sliders to see how each attribute controls a different aspect of the face.

Step 2 of 6: VAE Architecture

Encoding as distributions, not points

encoderμ, σsampledecoderdefine latentdistributionssample fromdistributionsμ1μ2μ3σ1σ2σ3z1z2z3z = μ + σ · ε

The VAE encoder outputs two vectors: μ (mean) and σ (standard deviation) — defining a distribution for each latent dimension, not a single point. To sample from this distribution while keeping gradients flowing, the VAE uses the reparameterization trick: it draws ε from a standard normal N(0,1), then computes z = μ + σ · ε. This separates the randomness (ε) from the learnable parameters (μ, σ), so backpropagation can update the encoder through z.

Step 3 of 6: Statistical Motivation

Why variational inference?

zhidden variablegeneratesxobservation
Imagine you see a photo of a smiling person. You'd like to figure out what latent features (z) — smile, hair color, etc. — produced that image (x). Mathematically, we need:
p(z|x) = p(x|z) · p(z) / p(x)
The problem? Computing p(x) means summing over every possible combination of latent features — impossible in practice. Instead, we train a neural network (the encoder) to learn a simpler approximation q(z|x). Try fitting it below!
zp(z) = N(0,1) priorp(z|x) intractableq(z|x) your approximationKL gap
μ0.20
σ0.40
KL(q‖post)
0.116
KL(q‖prior)
0.516
Fit
good
The VAE loss function balances two goals:
1. Reconstruct well
The decoder should be able to rebuild the original input from the latent code z
2. Stay organized
The encoder's output q(z|x) should stay close to a simple bell curve N(0,1)
Loss = reconstruction error + KL(q(z|x) ‖ p(z))

Nice fit! Your q(z|x) closely matches the true posterior. In a real VAE, the encoder learns these μ and σ values automatically — you just did what training does!

Step 4 of 6: Reparameterization Trick

Making sampling differentiable

RandomDeterministicBeforedecoder modelz~ q(z|x)μσencoder model✗ can't backpropthrough samplingAfter: reparameterizeddecoder modelzz = μ + σ ⊙ ε∂z/∂μ∂z/∂σμσε~ N(0,1)encoder model✓ backprop worksgradients flow through μ, σ
ε0.00
μ
0.35
σ
0.60
ε
0.00
z = μ+σε
0.35

When ε ≈ 0, z equals μ exactly — the sample lands right at the mean. The randomness from ε is external to the network, so gradients still flow through μ and σ during backpropagation.

Step 5 of 6: Latent Space

Visualizing the effect of β

Imagine training a VAE on handwritten digits (0–9). The encoder compresses each 28×28 pixel image into just 2 latent dimensions (z₁, z₂). Each color below represents a different digit class. How does β shape this 2D space?
VAE loss: L(x, x̂) + β Σ KL(qⱼ(z|x) ‖ N(0,1))
↑ reconstruction loss↑ KL divergence (weighted by β)
z₁z₂0123456789Balanced (β = 1.0)
β1.00
β
1.00
Mode
balanced
Clusters
10

With β ≈ 1.0, both losses balance. Clusters are distinct enough to separate digits, but they spread out and overlap smoothly. No dead zones — the latent space transitions gradually from one digit to another, enabling generation of new, realistic samples.

Step 6 of 6: Generation

Smooth interpolation in latent space

Asmile, widet = 0.50Bglasses, narrow
0.0
0.2
0.3
0.5
0.7
0.8
1.0
Smile
0.1
Eye size
0.1
Face width
0.0
Glasses
-0.1
AB
t
0.50
smile
0.10
eyes
0.10
width
0.00

At t = 0.50, each latent attribute is a smooth blend of A and B. The smile fades, the eyes grow, glasses appear — all gradually. This is what a continuous latent space gives you: the ability to generate new, realistic data by sampling or interpolating anywhere.