Back to Autoencoders Hub

Deconstructing Autoencoders from Scratch

Part 3: What the Bottleneck Learns

Introduction

Part 1 covered the math; Part 2 built four autoencoder variants in PyTorch. Now we train all four on a 5,000-image MNIST subset, measure reconstruction loss, visualize the 2D latent space, and test denoising. The point is not SOTA numbers---it is understanding what the bottleneck actually learns.

Experimental Setup

Everything runs on a standard CPU in under 2 minutes:

Training Results

All five configurations converged smoothly over 30 epochs:

Variant Latent Dim Final Loss
Vanilla 2 0.0401
Vanilla 32 0.0125
Denoising 32 0.0191
Sparse (MSE+L1) 32 0.0130
Convolutional 32 0.0083

Analysis

  1. Bottleneck width matters: Vanilla $d=2$ (0.0401) vs. $d=32$ (0.0125)---a 16$\times$ wider bottleneck cuts reconstruction error by 3.2$\times$. MNIST digits clearly have more than 2 intrinsic dimensions, but 32 captures most of the structure.
  2. Denoising costs extra: 0.0191 vs. vanilla's 0.0125. Training on corrupted inputs is a harder task, but it produces more robust representations.
  3. Sparsity is nearly free: 0.0130 vs. 0.0125---the L1 penalty ($\lambda = 10^{-3}$) barely hurts reconstruction while enforcing a structured latent code.
  4. Convolutions win: 0.0083, 34% lower than vanilla $d=32$. Preserving spatial structure via Conv2d/ConvTranspose2d is a large advantage on image data.

2D Latent Space Visualization

We encode all 10,000 MNIST test images with the vanilla $d=2$ autoencoder and plot them as a scatter, colored by digit class.

What the Scatter Plot Shows

Denoising Quality

Given heavily corrupted inputs (Gaussian noise, $\sigma = 0.3$), the denoising autoencoder recovers clean, recognizable digits.

Why Denoising Works

The bottleneck acts as a noise filter. Noise is high-frequency, random, and uncorrelated---it cannot be efficiently packed into 32 dimensions. Digit structure is low-frequency, patterned, and compressible. Forcing noisy inputs through the bottleneck strips out whatever is not compressible.

Vincent et al. (2008) formalized this: the denoising autoencoder learns to project corrupted inputs back onto the data manifold. The output is not a denoised version of the specific noisy input---it is the nearest point on the learned manifold.

Reconstruction Comparison

Comparing reconstructions across all four variants reveals the qualitative differences in what each architecture captures.

Observations

Where These Ideas Show Up Now

  1. Variational Autoencoders (VAEs): Replace the deterministic bottleneck with a probabilistic one---the encoder outputs mean and variance, the latent code is sampled from a Gaussian. This enables generating new samples by sampling the latent space.
  2. Diffusion Models: The denoising principle---learn to reverse corruption---is the foundation of DDPMs. Stable Diffusion applies it iteratively across multiple noise levels.
  3. Sparse Autoencoders for Interpretability: Anthropic and others use sparse autoencoders to decompose LLM internal representations into interpretable features. Same L1-penalized bottleneck, applied to understanding what Transformer neurons encode.
  4. Latent Diffusion: Stable Diffusion compresses images into a latent space via a convolutional autoencoder (VQ-VAE), then runs diffusion in that compressed space. The autoencoder's job is exactly what we studied here.

Conclusion

Four autoencoder variants, trained from scratch on 5,000 MNIST images, confirm the core principle: forcing data through a bottleneck reveals its structure.

The convolutional variant hit the lowest reconstruction loss (0.0083) by preserving spatial structure. The 2D latent space showed unsupervised digit clustering. The denoising variant stripped heavy Gaussian noise from corrupted inputs. The sparse variant maintained reconstruction quality (0.0130) while enforcing structured activations.

The information bottleneck, the manifold hypothesis, and the reconstruction objective are not historical curiosities---they are the foundation of every modern generative model. Full code and training logs are on GitHub.