Autoencoders

Force a signal through a narrow gap and out again. What survives is what the network decided mattered.

Overview

The apparently useless task

An autoencoder has two halves. The encoder maps an input to a smaller form. The decoder maps it back. The loss is the difference between the output and the input.

Copy the input to the output. Stated like that it sounds like a null task — the identity function scores just right.

Except it can't, because the form in the middle is smaller than the input. The network has to throw info away, and the training goal forces it to throw away the least costly info. What lives the bottleneck is what the data was mostly made of.

Autoencoders

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

An autoencoder learns to copy its input. The copying is not the point — the bottleneck is.
With fewer latent dimensions than inputs, an exact copy is impossible, so the network must decide what to keep.
A linear autoencoder with squared error provably finds the same subspace as PCA.
Add noise to the input and ask for the clean output: the bottleneck cannot afford to store noise, so it removes it.

Autoencoders

A network trained to copy its input, and why that odd task is handy.

Watch the squeeze

The grey line is the input. The orange line is what comes back out.

At a latent size of 1 the rebuild keeps only the largest structure and misses all else. Raise it and the copy tightens. Past a certain point extra dims buy almost nothing, because the left detail is noise rather than signal.

That flattening is the handy reading: it says roughly how many numbers the data really needs, which is its true dimensionality.

The relationship to PCA

A linear autoencoder trained with squared error finds the same subspace as PCA. Not a thing similar — provably the same span, though the single directions need not be the principal components in order.

So a linear autoencoder is PCA with extra steps. What makes them worth having is non-linearity: with non-linear activations the encoder can learn a curved manifold, which no linear map can follow. An image dataset lives on a wildly curved surface in pixel space, and that is the case where an autoencoder earns its keep.

Denoising

Raise the noise control. Now the input is corrupted and the target is still the clean signal.

The rebuild error against the *noisy* input goes up, and against the *clean* signal it goes down. The readout gives both.

The reason is the bottleneck again. Noise is, by build, the part of the signal with no structure to compress. So A narrow code can't afford to store it. Forcing the network to rebuild through the bottleneck forces it to discard just what noise is.

Denoising autoencoders make this the training method: corrupt the input on purpose, ask for the clean version. It also blocks the collapse answer where a wide code just learns the identity. This Is why the trick appears even when denoising isn't the goal.

What they are used for

Dimensionality reduction where the structure is non-linear.

Outlier spotting. Train on normal data. Any of it that rebuilds badly is unlike what was seen. It works because the model has only learned to compress one kind of thing.

Pretraining. Once the standard way to reset deep networks before better init and norming made it unneeded.

Generation — but not right. A plain autoencoder's latent space has gaps, so a random code often decodes to nothing real. Fixing that is what the VAE is for.

The elbow that reveals intrinsic dimension

The handy thing an autoencoder measures is *how many numbers the data really needs*, and it shows up as an elbow in rebuild error against latent size. On data that truly lives in d dims with a little noise on top, widening the bottleneck past d buys almost nothing:

latent sizereconstruction error
1large — only the biggest structure survives
up to the true dfalling steeply, each dimension earns its place
beyond dflat — the remaining detail is noise

The point where the curve flattens is the data's true dimensionality, and it is the same number PCA would find — a linear autoencoder with squared-error loss provably recovers the PCA subspace. The reason to reach for an autoencoder instead is the non-linear case: real data (an image dataset, say) lies on a curved manifold in a high-dimensional space, and a straight map can't follow the curve where a non-linear encoder can. That is also why the rebuild error reads as pixel match rather than visual quality, and why autoencoder outputs look softened: the bottleneck keeps what is common to the data and spends nothing on the exact sharp detail that sets apart one example from its neighbours.

The denoising variant turns the same bottleneck into a training method: corrupt the input on purpose and ask for the clean version back. Because noise is by build the part of a signal with no structure to compress, a narrow code can't afford to store it. So Rebuilding through the bottleneck forces the network to discard just what noise is. That also drops the collapse escape where a too-wide code simply learns the identity. This Is why the trick shows up even when denoising isn't the goal. And it's the same instinct behind using an autoencoder for outlier spotting. Here A model trained only on normal data rebuilds any of it unseen badly, and the rebuild error turns the outlier score.

None of the latent dims mean any of it on their own, either: they aren't sorted or disentangled unless a thing in the training made them so, which is a large part of what separates a plain autoencoder from the variational and beta-weighted variants built to push just that structure.

Where it goes wrong with Autoencoders

A bottleneck that is too wide. With enough room the network learns the identity and nothing is found. Constrain it, or use denoising, or add a sparsity fine.

Expecting a usable generative model. Sample from a plain autoencoder's latent space and you'll mostly get nonsense.

Reading the rebuild error as a quality score. It measures pixel match, not visual likeness. This Is why autoencoder outputs look blurred.

given the latent dims mean any of it on their own. They aren't sorted or disentangled unless a thing in the training made them so.

Questions people ask about Autoencoders

What is an autoencoder? A network trained to reproduce its input after squeezing it through a narrow bottleneck. The bottleneck forces it to learn a compact code.

How is an autoencoder related to PCA? A linear autoencoder with squared error learns the same subspace as PCA. Non-linear activations let it capture curved structure that PCA cannot.

Why not just make the bottleneck as big as the input? Then the network can learn the identity function and copy the input without learning anything useful. A small bottleneck, noise or a sparsity penalty prevents that.

What is a denoising autoencoder? One trained to recover the clean input from a corrupted copy. This pushes it to learn the structure of the data rather than memorise it.

What are autoencoders used for? Dimensionality reduction, anomaly detection through high reconstruction error, pretraining and compression. They are not good generators by themselves.

How do I choose the bottleneck size? Plot reconstruction error against code size and look for the elbow. Beyond the elbow, extra dimensions add little.

Train one, then look at what the bottleneck kept

An autoencoder learns to copy its own input through a narrow layer. Training one on data with a known hidden structure shows what a bottleneck forces it to discover -- and where PCA does the job better.

example_01.pyNumPy
Output

The sharp drop from one unit to two is the model discovering the true dimensionality. Now look at what the code layer actually contains.

example_02.pyNumPy
Output

The code recovered the true factors, but the ordering is arbitrary. Now compare against PCA, which solves the same reconstruction problem in closed form.

example_03.pyNumPy
Output

On flat data PCA wins because it finds the exact solution. Give the autoencoder something curved and the picture changes.

example_04.pyNumPy
Output

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why is training a network to copy its input not a null task?

  2. What does a linear autoencoder with squared error find?

  3. Why does a bottleneck remove noise?

Cheat sheet

Autoencoders

Train an autoencoder to compress and reconstruct its input, examine the bottleneck, and see why a network that copies is still worth building.

DEEP LEARNING · vizlearn.in/deep_learning/autoencoders.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.