Rendering Latent Space Maps…

Decoding 900 latent points for each model — takes about 10–15 seconds

Click to pin this point
Deep Learning Demo with Fashion-MNIST

Variational Autoencoder Latent Space Demo

An interactive visualization of how VAEs learn a structured, continuous latent space and why KL divergence term is important.

Start exploring ↓
Background

How a Variational Autoencoder Works

A standard autoencoder compresses an image into a small latent vector and then reconstructs it. A VAE adds a probabilistic twist: instead of encoding to a single fixed point, it encodes each input as a Gaussian distribution — defined by a mean (μ) and a variance (σ²) — and then samples a latent vector z from that distribution before decoding. This small change has profound consequences for the structure of the learned space.

Input
Fashion Image
28 × 28 px
Encoder
Compress
784 → 256 → 128
Latent Space
Sample z
z = μ + σ · ε   ε∼N(0,I)
Decoder
Reconstruct
2 → 128 → 256 → 784
Output
Reconstruction
28 × 28 px

The sampling step uses the reparameterization trick: instead of sampling directly (which blocks gradients), we compute z = μ + σ · ε where ε is random noise from N(0, I). This lets gradients flow through μ and σ, making the entire network trainable with standard backpropagation. The VAE is trained to minimize two terms simultaneously:

VAE Training Objective — the ELBO (Evidence Lower Bound)
ℒ = 𝔼[log p(x | z)]  −  KL( q(z | x) ‖ p(z) )
① Reconstruction Loss   𝔼[log p(x | z)]
How accurately the decoder re-creates the original image. Computed as binary cross-entropy over all 784 pixels. This is the main learning signal — without it, the model produces garbage.
② KL Divergence   KL( q(z | x) ‖ p(z) )
Measures how different the encoder's output distribution is from a standard normal N(0, I). Minimizing it forces all latent distributions to be compact and centered near the origin — eliminating gaps and enabling smooth interpolation.
📐   Reconstruction Loss
Pixel-by-pixel, how different is the decoded output from the input? This term drives the model to be faithful — a low reconstruction loss means the decoder can accurately reproduce the original clothing image. Without this term, there's no incentive to decode anything meaningful.
🔵   KL Divergence Regularizer
KL divergence penalizes the encoder for placing its output distributions far from N(0, I). This regularizes the latent space: it prevents clusters from collapsing to sharp points or drifting far apart. The result is a smooth, continuous space where every region decodes to something plausible — enabling meaningful interpolation and generation.

Interactive Demo

Explore the VAE Latent Space

This VAE was trained on Fashion-MNIST with a 2-dimensional latent space, so we can visualize it directly. Every point (z₁, z₂) in the grid below is a possible latent vector. The background image is generated by decoding a 30×30 grid of points covering z₁, z₂ ∈ [−3, 3]. The colored dots show where 1,000 real test images land after being encoded.

Interpolation A → B

Click two points on the map above to activate interpolation.
💡

Hover anywhere on the map to decode that latent point. Notice how similar items occupy nearby regions — boots cluster together, as do bags, T-shirts, and trousers.

Click once to pin point A (purple marker), then click again to pin point B (pink). The interpolation panel will activate so you can drag a slider to morph between the two items.


Core Concept

What Does KL Divergence Do?

The KL divergence term is what separates a VAE from a plain autoencoder. It constrains the encoder to map every input to a distribution close to a standard normal N(0, I). This seemingly simple constraint has a dramatic effect on the structure of the learned latent space.

❌   Without KL Divergence

  • The encoder places each class's representations wherever minimizes reconstruction loss — no other constraint.
  • Classes end up scattered arbitrarily, with large empty gaps between clusters.
  • Points in the gaps were never seen during training, so the decoder outputs noise or nonsense there.
  • Interpolating between two items crosses these dead zones — no smooth transition is possible.
  • The space is not continuous: you can't freely sample new points and expect meaningful outputs.

✓   With KL Divergence

  • All encoder distributions are pulled toward N(0, I) — compact, centered, and overlapping.
  • The region near the origin is densely filled with meaningful content — no dead zones.
  • Neighboring latent points decode to similar images — the space is smooth and continuous.
  • Interpolating between two items always produces plausible outputs at every step.
  • You can sample any point from N(0, I) and generate a novel, realistic clothing image.

The Key Trade-Off

KL divergence acts as a regularizer on the latent space. By pushing all distributions toward N(0, I), it forces the encoder to use overlapping, compact regions — the decoder must then learn smooth, consistent mappings across the entire space. The cost is slightly worse reconstruction accuracy (the encoder can't perfectly memorize each input's exact location), but the benefit is a continuous, navigable latent space where every point generates something meaningful. This trade-off is precisely what makes VAEs useful as generative models rather than just compression tools.

Side-by-Side Comparison

VAE vs. Plain Autoencoder

Both models use the identical encoder and decoder architectures, trained on the same Fashion-MNIST data for the same number of epochs. The only difference is whether the KL divergence term is included in the loss. Hover over both maps and compare what you see.

Variational Autoencoder

With KL
Hover to decode
z = (—, —)
Smooth and organized. The space is continuously filled — similar items cluster together and transitions between regions are gradual. Every point decodes to a recognizable clothing image. Notice how the scatter dots align with the content of the decoded background.

Plain Autoencoder

No KL
Hover to decode
z = (—, —)
Fragmented and gappy. Without KL, the encoder places clusters anywhere convenient, leaving large unoccupied regions. Hover over those empty-looking areas — the decoder produces noise because it was never trained on those latent coordinates.
🔍

Try this: Hover over the same (z₁, z₂) coordinate on both maps simultaneously. On the VAE side you'll see a recognizable clothing item; on the AE side the same coordinate often falls in a gap and produces a blurry, meaningless output — because the AE was never trained to decode that region.


Interactive Feature

Smooth Interpolation

A key test of any latent space is: can you smoothly interpolate between two points and get meaningful images at every step? Because the VAE's KL term enforces continuity, you can pick any two clothing items and walk a straight path between them — the decoder produces a plausible image throughout.

1
Go to the Latent Space section
Scroll up to the "Explore the VAE Latent Space" section and hover over the map to find two clothing items you want to morph between.
2
Click to pin A, then B
Click once to place pin A (shown in purple on the map). Hover to a new location and click again to place pin B (pink). A line connects them.
3
Drag the slider
The interpolation panel under the canvas activates. Drag the slider to walk from A (t=0) to B (t=1) and watch the image morph continuously.

The VAE decoder handles this gracefully because the KL term enforces a continuous prior. Every point between A and B lies in a region close to N(0, I), so the decoder encountered similar inputs during training. Try interpolating between very different categories — a sneaker and a bag — and watch the morphing happen step by step. A plain autoencoder cannot do this reliably because the path between two points crosses uninformative dead zones.

The interpolation controls are embedded directly in the Latent Space Explorer section above — scroll up, pin two points, and the slider activates automatically.