# Autoencoders and Variational Autoencoders

> Compress data into a latent space and reconstruct it; add a probabilistic twist and you get a model that can sample new examples. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/autoencoders-vaes
Last updated: 2026-11-28

Compress data into a latent space and reconstruct it; add a probabilistic twist and you get a model that can sample new examples.

Advanced · about 8 hours · difficulty 4/5

Learn first: [Convolutional Neural Networks](https://www.internhack.in/roadmaps/tracks/machine-learning/cnns), [Probability for ML](https://www.internhack.in/roadmaps/tracks/machine-learning/probability)

An **autoencoder** has an encoder that squeezes the input into a small latent vector and a decoder that rebuilds the input from it. Because the bottleneck is narrow, the network must learn the most important structure of the data. That makes autoencoders useful well beyond generation.

### Plain autoencoders

- **Dimensionality reduction**: a nonlinear cousin of PCA.
- **Denoising autoencoders** learn to remove noise by reconstructing clean inputs from corrupted ones.
- **Anomaly detection**: inputs unlike the training data reconstruct poorly, so reconstruction error flags them.

### Variational autoencoders (VAEs)

A plain autoencoder's latent space has gaps, so random points decode to garbage. A **VAE** makes the encoder output a distribution (a mean and variance) and pushes it towards a standard normal. The loss has two parts:

- a **reconstruction term** (how well the output matches the input), and
- a **KL divergence term** (how close the latent distribution is to the prior).

The **reparameterization trick** (`z = mu + sigma * eps`) keeps sampling differentiable so the model trains with ordinary backpropagation. After training you can sample `z` from a normal distribution and decode brand new examples, or interpolate smoothly between two inputs.

### Why it still matters

VAEs produce blurrier samples than GANs or diffusion, but the idea of a learned latent space is everywhere: **latent diffusion** models such as Stable Diffusion run their denoising inside a VAE's latent space.

**Good enough to move on** when you can train a VAE on a small image dataset, sample new images, and explain both terms of its loss.

## Key concepts

- **Encoder, bottleneck, decoder**
- **Denoising autoencoders**
- **Reconstruction-based anomaly detection**
- **VAE latent distributions**
- **KL divergence term**
- **Reparameterization trick**
- **Latent space interpolation**

## Mini project

Train a convolutional VAE on Fashion-MNIST, plot a 2D latent space coloured by class, generate a grid of new samples, and use reconstruction error to detect images from a different dataset.

## Check yourself

What would happen to a VAE's samples if you removed the KL term from the loss, and why is the reparameterization trick needed?

## Free resources

- [Dive into Deep Learning](https://d2l.ai/) (d2l.ai)
- [Deep Learning (Goodfellow, Bengio, Courville)](https://www.deeplearningbook.org/) (MIT Press)
- [PyTorch tutorials](https://pytorch.org/tutorials/) (PyTorch)

Previous: [Large Language Models and Efficient Fine-Tuning](https://www.internhack.in/roadmaps/tracks/machine-learning/llm-fine-tuning) · Next: [Generative Adversarial Networks](https://www.internhack.in/roadmaps/tracks/machine-learning/gans)

## More in Generative Models

- [Autoencoders and Variational Autoencoders](https://www.internhack.in/roadmaps/tracks/machine-learning/autoencoders-vaes)
- [Generative Adversarial Networks](https://www.internhack.in/roadmaps/tracks/machine-learning/gans)
- [Diffusion Models](https://www.internhack.in/roadmaps/tracks/machine-learning/diffusion-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
