Autoencoder
Squeeze data through a narrow bottleneck and see what survives
Paint a symbol on the left, watch the latent code in the middle and the reconstruction on the right. Turn the bottleneck slider and see how the picture crumbles.
How to play: paint into the left grid (click/drag; right-click or Ctrl erases) or load a training symbol with the symbol buttons. The encoder presses it into the latent code (the bars in the middle), the decoder rebuilds the reconstruction on the right. Drag the bottleneck slider k down from 8 to 1: for many symbols the reconstruction crumbles and the error bar rises. Or drag the marker on the 2D map — that bypasses the encoder and morphs directly between the symbol clusters. In the squeeze prediction you guess beforehand whether a symbol survives the hardest squeeze (k = 1) still recognizable. Tip: before you start, read the explanation below the playfield — it walks you through every part of the machine, step by step.
The autoencoder in nine steps
You need no prior knowledge. We'll build the autoencoder up from scratch: first the everyday problem it solves, then each part on its own, and only at the very end the technical term. Read the steps in order — each one uses only what was already explained before.
- 1. The problem: too much information, too little roomImagine describing a picture to a friend over the phone so she can redraw it. You can't read out every single dot — that takes forever. You have to boil the picture down to a few telling keywords: "round symbol, open at the top". Exactly this problem — squeezing a lot of information into a few meaningful values and unpacking it again later — is something machines solve constantly: when storing photos, recognizing patterns, sorting through huge amounts of data.
- 2. What is an autoencoder? The summary machineAn autoencoder is a machine that learns to do exactly that automatically: it takes an input, squeezes it into a terse shorthand description, and rebuilds the input from it. The special part: nobody tells it which keywords are good — it figures out for itself what matters by practising thousands of times to repeat its own input as faithfully as possible. In this demo the input is a small 8×8 image (64 dots) that you'll paint yourself in a moment.
- 3. The encoder: the squeezingThe first part of the machine is called the encoder. Its job: turn the 64 dots into a few numbers — the shorthand description. In the playfield that's the left half: you paint on the left, the encoder compresses the image step by step (64 → 32 → 8 numbers). You'll see those few numbers as bars in the middle in a moment.
- 4. The bottleneck: the narrowest point (the squeezing)Between encoder and decoder sits the narrowest point — the bottleneck. All information has to pass through here. With the slider k you set how far the bottleneck is opened: k = 8 means room for eight numbers, k = 1 only a single one. The narrower (smaller k), the less gets through — that is the "squeezing". Remember this slider: it's the single most important knob in the whole demo.
- 5. The decoder: the rebuildingThe second part of the machine is called the decoder. It receives only the few numbers from the bottleneck and rebuilds a full 64-dot image from them — the reconstruction (on the right of the playfield). Important: the decoder never sees your original. Anything that didn't fit through the bottleneck it has to guess or leave out.
- 6. The reconstruction error: how much was lost?How good was the reconstruction? That is measured by the reconstruction error: the difference between your original (left) and the rebuilt image (right), averaged over all 64 dots. Little difference = small error = the machine kept the essentials. Big difference = big error. You see it on the right as the error bar: the fuller, the more was lost.
- 7. Why the narrow bottleneck is the trickNow the actual idea. If the bottleneck were as wide as the input, the machine could simply pass everything through — and learn nothing. Only because the bottleneck is much narrower is the machine forced to drop the unimportant parts and keep just the essentials. Try it: drag k from 8 to 1. At large k the symbol stays recognizable; at k = 1 many symbols fall apart, and the error bar rises. The narrowing presses out knowledge — and that is exactly the point.
- 8. The latent space: where similar things lie togetherThe shorthand descriptions of all images together form a map — the latent space. You see it as the 2D map in the middle: each point is one possible shorthand. The remarkable part: similar symbols lie close together and form groups (clusters), different ones lie far apart. That's why you can drag the marker between two groups and watch one symbol morph smoothly into another — the machine learned a meaningful order, entirely on its own.
- 9. And now the bridge to real AIWhat you try here in miniature with 64 dots is one of the most important tools of modern AI — only that real models process millions of values. What exactly this has to do with artificial intelligence is in the next section, right below the demo.
The five stations in the playfield
The playfield shows an image's journey from left to right through the machine. For each station: what you see, what to try, and what to watch for.
- Input (8×8 — you paint)
- What you see
- Far left, the input grid: 8×8 = 64 cells that you fill yourself. It is your original image.
- What to try
- Paint a symbol (click and drag) or load a ready-made training image with the symbol buttons.
- What to watch for
- Only this image enters the machine. Everything that follows is computed from it alone.
- Encoder
- What you see
- The first arrow stands for the encoder — the part that squeezes the image together.
- What to try
- You don't have to do anything here; the encoder computes automatically whenever the input image changes.
- What to watch for
- In latent mode (when you drag the map) the encoder is bypassed — the arrow goes pale then.
- Latent code
- What you see
- The middle: a few bars (the shorthand) plus a 2D map with all the learned symbol groups.
- What to try
- Drag the marker on the map between two groups — and watch on the right how one symbol turns into another.
- What to watch for
- Grey bars are switched off by the bottleneck. At small k only the leading, most important bars stay active.
- Decoder
- What you see
- The second arrow stands for the decoder — the part that rebuilds an image from the shorthand.
- What to try
- Here too the machine computes by itself; your changes above (k or marker) take effect immediately.
- What to watch for
- The decoder only knows the few numbers from the bottleneck — never your original.
- Reconstruction
- What you see
- Far right, the reconstruction (the rebuilt image) plus the error bar below it.
- What to try
- Compare it with your original on the left. Then drag k smaller and watch how it changes.
- What to watch for
- The narrower the bottleneck, the more the reconstruction deviates and the fuller the error bar gets.
Does this remind you of the PCA demo? It also reduces dimensions. The difference: PCA can only do straight (linear) projections. An autoencoder uses nonlinear layers (tanh) and can therefore learn curved relationships — you can see it because on the way between two clusters the decoder produces real in-between shapes instead of just cross-fading the two images.
Definition
What is an autoencoder?
Analogy
Imagine you have to send a picture through a very narrow mail slot. It won't fit as a whole. So you describe it to a friend with just a few keywords — "round symbol, open at the top" — and she redraws it on the other side from memory. The fewer keywords you're allowed, the rougher her drawing gets.
That is exactly what an autoencoder does. One part of the network (the encoder) squeezes the input into a tiny description — the latent code. The other part (the decoder) rebuilds the input from it. In between sits the bottleneck: the narrow point all information must pass through. The narrower the bottleneck, the more detail is lost.
Definition
An autoencoder is a neural network that learns to reconstruct its own input. It consists of an encoder that maps the input x to a low-dimensional code z, and a decoder that produces a reconstruction x' from z. Training minimizes the reconstruction error, here the mean squared error (MSE) between x and x'.
The bottleneck forces the network to keep only the essentials — a compressed representation. Unlike linear PCA, an autoencoder uses nonlinear activations (here tanh) and can capture curved relationships. In this demo the latent dimensions are ordered (nested/ordered bottleneck): the first dimensions carry the most important information, so the error rises monotonically on average as more dimensions are cut off.
An autoencoder learns entirely without labels — it only gets inputs and tries to output them as faithfully as possible. The trick lies in the middle: the bottleneck is much smaller than the input, so the network can't simply pass everything through. It has to find a clever shorthand. Here's how it works:
- Encode: the encoder gradually compresses the 64 pixels of the input grid until only a few numbers remain — the latent code. That is the shorthand description of the image.
- Bottleneck: this is the narrowest point. With the slider k you decide how many latent dimensions may be used. Fewer dimensions = less room for information = a coarser description.
- Decode: the decoder rebuilds a 64-pixel image from the latent code. It only has the shorthand — anything that didn't fit it must guess or leave out.
- Ordered code: in this demo the latent dimensions are sorted by importance. The first dimension carries the most, the last the least. That's why the reconstruction gets steadily worse the more dimensions you cut — reliably, not randomly.
Important: the latent space is not an arbitrary cloud of numbers. Similar inputs land close together — identical symbols form clusters. That's exactly what you see on the 2D map, and exactly why you can morph between symbols by dragging the marker in between.
This little toy is no special case — it shows a basic principle that sits in many places inside modern artificial intelligence. Four applications follow directly from what you just tried, and a fifth bridges over to image generation.
Representation learning & embeddings
The few-numbers-long shorthand in the bottleneck is what AI calls a representation or an embedding. Instead of working with raw pixels, the machine learns to describe each object by a few meaningful values — and to do so such that similar things lie close together. Such embeddings are the base currency of modern AI: words, images, users, molecules are translated into these compact numeric profiles that a model can then compute with.
Dimensionality reduction
Turning 64 dots into a few numbers is dimensionality reduction. Real data often has thousands of features, most of them redundant or irrelevant. An autoencoder presses them down to the few that truly count. That saves memory and compute time and makes hidden structure visible — similar to PCA, but with curved instead of only straight relationships.
Anomaly detection
An autoencoder only learns to reconstruct well what it sees often. Feed it something unusual later that it never practised, and the reconstruction error suddenly spikes — a clear alarm signal. That's how AI spots credit-card fraud, manufacturing defects, or oddities in sensor data: not because someone told it what fraud looks like, but because the unknown simply doesn't fit through the bottleneck.
Denoising
Train an autoencoder specifically to reconstruct clean images from noisy inputs and it becomes a denoiser. Because the bottleneck only lets the essentials through, the actual image survives and the random noise drops away. Such denoising autoencoders sit in photo apps, in image enhancement, and in cleaning up sensor and audio data.
The bridge to generative AI
Now the most exciting step. You saw that in the latent space you can aim for points between symbols and produce new in-between shapes. What if you deliberately picked new, meaningful points and decoded brand-new images from them? That is exactly what a variational autoencoder (VAE) does: it organizes the latent space so you can safely sample new points in it and turn them into real images — the first step from "rebuild" to "generate".
From here a direct line runs to today's image generation: the VAE made the latent space generation-ready; later diffusion models, like the ones behind Stable Diffusion and similar image generators, often work in exactly such a learned latent space. The basic scaffold — press an input into a compact space, then rebuild images from it — is something you already held in your hands in this demo.
VAE: Variational Autoencoders
The development of probabilistic generative models through latent space modeling. On December 20, 2013, Diederik Kingma and Max Welling published the paper 'Auto-Encoding Variational Bayes'. VAEs connect encoder and decoder networks through a probabilistic latent space — typically a multivariate Gaussian distribution. Unlike deterministic autoencoders, the encoder encodes data as distributions rather than single points, enabling continuous interpolation and data generation. The Reparameterization Trick makes randomness differentiable as a model input, enabling standard gradient optimization. In their experiments, VAEs generated handwritten digits (MNIST) and small face images (Frey Faces) — still blurry, but groundbreaking as a proof of concept for variational inference. This work laid the foundation for modern generative AI and shaped later probabilistic approaches through to diffusion models.
In short: an autoencoder is not a niche trick but a way of thinking that runs right across AI — from compressing, through spotting oddities, to generating new content.
- 1. EncodeThe encoder is a sequence of layers that maps the input x (64 pixels here) to ever smaller vectors: 64 → 32 → 8. Each layer multiplies by a weight matrix, adds a bias, and applies the nonlinear tanh function.
- 2. BottleneckThe smallest layer is the latent code z. With k the trailing dimensions are set to 0. Because the code is ordered, only the most important information remains with a small k.
- 3. DecodeThe decoder mirrors the encoder: 8 → 32 → 64. From the latent code z it rebuilds the reconstruction x' — again with weights, bias and tanh.
- 4. Loss functionTraining minimizes the reconstruction error, the mean squared error L = mean over (x − x')². The network adjusts its weights via backpropagation so that this error becomes small — without any labels.
- 5. Ordered code (nested dropout)To sort the dimensions by importance, training picks a random cut k at each step and switches off all dimensions behind it. This forces the network to pack the most important information into the leading dimensions — and the error falls monotonically on average as k grows.
Check
Test your knowledge
What is the purpose of the bottleneck in an autoencoder?
Select one answer
1. What is the purpose of the bottleneck in an autoencoder?
2. What happens to the reconstruction when you shrink the bottleneck k?
3. What distinguishes an autoencoder from linear PCA?
4. Why do similar inputs lie close together in the latent space?
Compress first, then reconstruct
An autoencoder is a neural network that is supposed to output its own input again. The encoder squeezes the input through a narrow bottleneck into a few numbers, and the decoder builds the input back up from that. It is trained without labels: the target is simply the input itself. That's why the autoencoder is a method of unsupervised representation learning.
The bottleneck forces the learning
In the demo, the input has 64 pixels (an 8x8 glyph), but the bottleneck has only 8 numbers. The network therefore cannot simply pass the input through 1:1 — it must distill the essential information to get by with so few numbers. This exact compact sequence of numbers in the bottleneck is the latent code (the learned representation).
Ordered code: the k control
This network was trained with nested dropout, so that the 8 code dimensions are ordered by importance: the first carries the most, the last the least. Setting all dimensions from k onward to 0 leaves a valid, shorter code — and the reconstruction error drops monotonically the more dimensions you keep. This is not an exact optimum but a learned approximation: at a small k, the reconstruction stays visibly blurry.
Properties
- Unsupervised: the input is its own target, no labels needed
- Lossy: the bottleneck forces a simplification, the reconstruction is rarely perfect
- Nonlinear: due to the tanh layers, the code is not a flat plane like in PCA
- Representation: the latent code is a compact, reusable feature representation
Variants
The denoising autoencoder receives a noisy input but is supposed to reconstruct the clean version — this way it learns robust features. The variational autoencoder (VAE) produces, instead of fixed numbers, a probability distribution in the bottleneck; new, plausible examples can be drawn from it. This turns the autoencoder into a generative model and builds the bridge to modern image AI.
Play with the demo! Slide the k control down from 8 to 1 and watch the reconstruction fall apart and the error bar rise. Then drag the marker across the latent map and see how one glyph smoothly transitions into the next — a nonlinear interpolation in the learned code space.
1986-1988: Auto-associative networks
The idea of letting a network reconstruct its own input arose with the spread of backpropagation (Rumelhart, Hinton and Williams, 1986). Bourlard and Kamp showed in 1988 that such a network with linear activation is closely related to principal component analysis (PCA) — the autoencoder is thus the nonlinear generalization of this classical dimensionality reduction.
2006: Deep autoencoders
Hinton and Salakhutdinov showed in Science ("Reducing the Dimensionality of Data with Neural Networks") how deep autoencoders can be learned through layer-wise pretraining. They clearly beat classical methods at dimensionality reduction and gave deep learning an important boost.
2008: Denoising autoencoder
Vincent, Larochelle, Bengio and Manzagol introduced the denoising autoencoder: it receives a deliberately noisy input and is supposed to reconstruct the clean original. This forces the network to learn robust, meaning-bearing features instead of mere copies.
2013: Variational autoencoder
Kingma and Welling introduced the variational autoencoder (VAE). It learns a distribution in the latent space from which new examples can be generated. This turned the autoencoder into a genuine generative model — one of the roots of today's image generation.
2014: Ordered codes (nested dropout)
Rippel, Gelbart and Adams showed with nested dropout how to order the dimensions of the latent code by importance. Exactly this trick is what powers this demo: it allows the bottleneck to be narrowed continuously via the k control while keeping a meaningful shorter code.
1
# Autoencoder — unsupervised representation learning
2
# Demo network: 64 -> 32 -> 8 -> 32 -> 64 (activation: tanh)
3
4
function train(data):
5
# start encoder + decoder with random weights
6
encoder = network(64 -> 32 -> 8)
7
decoder = network(8 -> 32 -> 64)
8
9
repeat for many epochs:
10
for each input x in data:
11
# x is an 8x8 glyph = 64 pixels
12
# 1. encoder compresses the input
13
z = encoder(x) # z = latent code (8 numbers)
14
# 2. nested dropout orders the code dimensions
15
k = random from {1, 2, 4, 8}
16
z[k..] = 0 # keep only the first k dimensions
17
# 3. decoder reconstructs the input
18
x_recon = decoder(z) # back to 64 pixels
19
# 4. measure reconstruction error (MSE)
20
loss = mean((x - x_recon)^2)
21
# 5. adjust weights via backpropagation
22
gradients = differentiate(loss)
23
encoder, decoder = update(gradients)
24
25
return encoder, decoder
26
27
# --- after training: this is what the demo shows ---
28
function reconstruct(x, k):
29
z = encoder(x)
30
z[k..] = 0 # narrow the bottleneck to k dimensions
31
return decoder(z)
32
33
# recognizer: nearest prototype to the reconstruction
34
function recognize(x_recon):
35
best = none
36
for each prototype p:
37
distance = mean((x_recon - p)^2)
38
if distance is smallest: best = p
39
return class(best)
40
41
# latent atlas: set the code directly and decode
42
function morph(z1, z2):
43
z = [z1, z2, 0, 0, 0, 0, 0, 0]
44
return decoder(z) # nonlinear interpolation
🗜️ Encoder: compressing
The encoder squeezes the 64 pixels of the input down step by step (64 -> 32 -> 8). At the end, only 8 numbers remain — the latent code. The network must decide along the way which information really matters.
# 1. encoder compresses the input
z = encoder(x) # z = latent code (8 numbers)
🖌️ Input
An 8x8 glyph with 64 pixels — in the demo you draw it yourself or pick a template.
🗜️ Encoder
Compresses the 64 pixels into a few numbers via 64 -> 32 -> 8.
🧬 Bottleneck
The latent code with only 8 ordered numbers; the k control keeps the first k.
🎨 Decoder
Builds an 8x8 image back up from the code via 8 -> 32 -> 64.
🖼️ Reconstruction
The output plus error bar (MSE): how close does the reconstruction come to the input?