NeuroNomixer
  • Home
  • Blog
  • Visual Guides
  • Authors
  • Contact
Sign InSign Up
HomeBlogAuthorsContactPrivacy Policy

© 2026 NeuroNomixer — Built with Next.js & Tailwind CSS

Visual Guides/VAEs and Latent Space
Deep Learning

VAEs and Latent Space

Make the bottleneck probabilistic and the compressed space becomes a smooth, sampleable map. Train a real variational autoencoder in your browser, then click around its latent space, morph one image into another, and measure what the beta knob buys and costs.

Train 3 VAEs
Decode 3 latent points (0/3)
Sweep the A to B interpolation
Compare 2 beta values (0/2)

Sign in to save progress

From autoencoder to VAE: one probabilistic twist

A plain autoencoder squeezes each image through a narrow bottleneck and learns to reconstruct it. That gives you compression, but the space between encoded points is lawless: decode a spot no training image landed on and you get garbage. A variational autoencoder (Kingma and Welling, 2013, arXiv:1312.6114) fixes this by encoding every image as a distribution, not a point, and paying a penalty whenever those distributions stray from a standard normal prior.

Encoder emits mu and sigma

The encoder here maps each 8x8 image (64 pixels) through 16 tanh units to a mean and a log-variance for each of the 2 latent dimensions: 1108 parameters that describe a little Gaussian per image, not a single point.

The reparameterization trick

You cannot backpropagate through a random draw, so the VAE samples z = mu + sigma * eps with eps from N(0, I). The randomness moves into eps, and gradients flow cleanly through mu and sigma. This one trick is what makes VAE training work.

Decoder maps z back to pixels

The decoder (1136 parameters) turns any 2-D latent point into 64 pixel intensities. Because the KL penalty keeps the posteriors packed around the origin, points between and around the training images decode to sensible pictures too. That is what makes the space a map.

loss = BCE(x, decode(z)) + beta * KL( N(mu, sigma^2) || N(0, I) )
z    = mu + sigma * eps,   eps ~ N(0, I)        (reparameterization)

Minimizing this loss maximizes the evidence lower bound (ELBO). The beta weight on the KL term is the beta-VAE generalization (Higgins et al., 2017); beta = 1 recovers the original objective.

The dataset: 180 tiny images you can inspect

Three soft shape families (blob, cross, ring), 60 images each, drawn at deterministic LCG-sampled positions on an 8x8 grid. Small on purpose: it keeps in-browser training honest and fast, and a 2-D latent space is enough to organize it. Showing 24 of 180.

blob
blob
blob
blob
blob
blob
blob
blob
cross
cross
cross
cross
cross
cross
cross
cross
ring
ring
ring
ring
ring
ring
ring
ring

Step 1: train three real VAEs, one per beta

Hit the button and watch both loss terms fight it out live: reconstruction wants every image pinned precisely, the KL term wants every posterior to look like the prior. Beta sets the exchange rate between them.

What runs here: a real VAE (64 → 16 → 2 latent dims → 16 → 64, 2244 parameters) trained with Adam (learning rate 0.01, batch size 30) on the 180 images above, minimizing the negative ELBO: BCE reconstruction plus beta times the KL divergence, with z drawn through the reparameterization trick. Because retraining at every slider position would be slow, the guide trains one model per beta value (0.05, 1, 4) and the beta slider snaps between them. Weight init is Xavier-style uniform (Glorot and Bengio, 2010); all seeds are fixed, so every run reproduces these exact curves.

Step 2: walk the latent space

Every marker is one dataset image placed at its encoder mean. Click or drag anywhere, including the empty gaps, and the decoder renders that exact point. Then drop beta to 0.05 and watch the map sprawl beyond the prior, or raise it to 4 and watch the space collapse toward one average image. The metrics update from the selected model.

beta = 1snaps between the 3 trained models
0.0514

Reconstruction (BCE, nats/image)

?

lower = sharper reconstructions

Reconstruction (MSE per pixel)

?

same story in pixel units

KL to prior (nats/image)

?

lower = latent hugs N(0, I), smoother map

-3-3-2-2-1-100112233z1z2Train the models to unlock the latent map
blobcrossringdecode probe

Decoded from z = (?, ?)

Train first

Every decoded image is a real forward pass through the trained decoder at the exact z you picked. Nothing is precomputed or looked up.

Evaluation detail: the metric tiles and table use the deterministic posterior mean (z = mu) instead of a random sample, so the numbers are stable and reproducible. Sampling noise would jitter them slightly.

Step 3: interpolate between two encoded images

Pick two dataset images. Both are encoded to their posterior means, drawn as A and B on the map above, and the slider walks the straight line between them, decoding every step through the real decoder. This is the classic VAE party trick, and it only works because the KL term made the space between them meaningful.

Endpoint A

Endpoint B

t = 0.50z(t) = (1 - t) · mu_A + t · mu_B, decoded live
AB

Decoded at t = 0.50

Train first

The full path, seven fixed steps

Every frame is the real decoder evaluated on a straight line between the two encoder means. In a well-trained VAE the images morph smoothly because the KL term forces nearby latent points to decode to similar images. Try endpoints from different shape classes and watch one shape dissolve into the other.

Sampling new data

Because training pulls every posterior toward N(0, I), decoding a fresh draw from the prior yields a new image that never existed in the dataset. Generation is just decoding random points, and it works exactly as well as the latent space is organized.

Posterior collapse

Push beta high enough and the cheapest solution is mu = 0, sigma = 1 everywhere: the KL term hits zero and the decoder ignores z, outputting one average image. You can watch this happen here at beta = 4, where KL nearly vanishes and clicking the map barely changes the output.

The beta dial in practice

beta-VAE (Higgins et al., 2017) raises beta above 1 to trade reconstruction sharpness for a more disentangled, better-organized latent space. Low beta buys crisp reconstructions but lets the space sprawl into holes the prior never samples.

← All GuidesNext Guide →