Autoencoder — Theory
1. WHY — Learning Compressed Representations
High-dimensional data often lives near a lower-dimensional manifold. An autoencoder learns this manifold by training a neural network to reconstruct its input through a bottleneck.
Unlike PCA, which is limited to linear subspaces, a nonlinear autoencoder can capture curved manifolds. The bottleneck forces the network to discover the most informative features rather than memorizing an identity map.
Applications: dimensionality reduction, feature learning, denoising, anomaly detection, and (with the variational extension) generative modeling.
2. WHAT — Notation and Architecture
2.1 Notation table
| Symbol | Type | Meaning |
|---|---|---|
| \(x \in \mathbb{R}^d\) | vector | input data point |
| \(z \in \mathbb{R}^k\) | vector | latent (code) representation, \(k < d\) |
| \(\hat{x} \in \mathbb{R}^d\) | vector | reconstruction |
| \(f_\theta: \mathbb{R}^d \to \mathbb{R}^k\) | function | encoder with parameters \(\theta\) |
| \(g_\phi: \mathbb{R}^k \to \mathbb{R}^d\) | function | decoder with parameters \(\phi\) |
| \(n\) | scalar | number of training samples |
| \(\lambda\) | scalar | regularization coefficient |
2.2 Basic autoencoder
The encoder maps input to code, the decoder maps code back to input space:
The objective minimizes reconstruction error over the dataset:
For binary/probability outputs, binary cross-entropy may replace MSE:
BCE treats each output coordinate as an independent Bernoulli variable, so the decoder's last layer must produce values in \((0,1)\) — in practice a sigmoid.
2.3 Architecture choices
| Variant | Constraint | Effect |
|---|---|---|
| Undercomplete | \(k < d\) | Bottleneck forces compression |
| Overcomplete | \(k \geq d\) | Needs additional regularization |
| Tied weights | \(W_{\text{dec}} = W_{\text{enc}}^T\) | Reduces parameters, regularizes |
3. HOW — Linear Autoencoder and PCA Connection
3.1 Linear autoencoder
Consider a single-layer encoder and decoder with no activation:
where \(W_e \in \mathbb{R}^{k \times d}\), \(W_d \in \mathbb{R}^{d \times k}\).
The reconstruction objective becomes:
The composite map \(W_d W_e\) has rank at most \(k\), so this is a rank-constrained least-squares problem — the form that makes the PCA connection below possible.
3.2 Equivalence with PCA subspace
Theorem (Baldi & Hornik, 1989). For mean-centered data, the optimal linear autoencoder with \(k\)-dimensional bottleneck spans the same subspace as the \(\text{top-}k\) principal components.
Derivation sketch. With centered data (\(\bar{x} = 0\)), set \(b_e = 0\), \(b_d = 0\). The objective is:
Let \(M = W_d W_e\). This is a \(\text{rank-}k\) approximation problem. By the Eckart–Young theorem, the optimal \(\text{rank-}k\) approximation of \(X\) in Frobenius norm uses the \(\text{top-}k\) singular vectors.
Therefore \(M\) projects onto the subspace spanned by the \(\text{top-}k\) right singular vectors of \(X\), which equals the \(\text{top-}k\) eigenvectors of the covariance matrix \(\frac{1}{n}X^T X\).
Result: A linear autoencoder with \(k\) latent dimensions recovers the PCA subspace. However, the individual encoder/decoder weight matrices are not unique — any invertible \(k \times k\) rotation gives the same reconstruction.
4. Denoising Autoencoder (DAE)
4.1 Idea
Instead of reconstructing a clean input from itself, corrupt the input and reconstruct the original:
The corruption enters only the encoder's input while the target stays clean, so copying the input is no longer optimal.
4.2 Why it works
By forcing the network to denoise, the encoder must learn features that are robust to noise rather than memorizing individual samples. The DAE implicitly learns a vector field pointing toward the data manifold (Vincent et al., 2010). This acts as a regularizer even when \(k \geq d\).
5. Sparse Autoencoder
For an overcomplete autoencoder (\(k \geq d\)), add a sparsity penalty on the hidden activations:
where \(h_j = \frac{1}{n}\sum_{i=1}^{n} z_j^{(i)}\) is the average activation of unit \(j\) across the dataset. The L1 penalty encourages most hidden units to be inactive for any given input, resulting in a sparse distributed code.
Alternatively, a KL-divergence penalty targets a desired activation probability \(\rho\) (typically \(\rho \approx 0.05\)):
This penalty vanishes exactly when each unit's mean activation \(\hat{\rho}_j\) equals the target \(\rho\), and grows without bound as a unit drifts toward always-off or always-on.
6. Variational Autoencoder (VAE)
6.1 Generative model
A VAE is a latent-variable generative model. The generative process is:
The goal is to maximize the marginal log-likelihood:
This integral is intractable because it requires integrating over all possible latent codes.
6.2 Variational inference and the ELBO
Introduce an approximate posterior \(q_\theta(z \mid x)\) (the encoder) and derive a tractable lower bound.
ELBO derivation. Start from the log-likelihood and apply Jensen's inequality:
Multiply and divide by \(q_\theta(z \mid x)\):
By Jensen's inequality (\(\log\) is concave):
Split the log:
Result: The Evidence Lower Bound (ELBO) is:
The gap between \(\log p_\phi(x)\) and the ELBO equals a non-negative KL divergence:
Maximizing the ELBO therefore simultaneously improves the generative model and tightens the approximation.
6.3 Gaussian encoder
Choose the encoder to output a diagonal Gaussian:
where \(\mu_\theta(x)\) and \(\log\sigma_\theta^2(x)\) are outputs of the encoder network. We parameterize \(\log\sigma^2\) instead of \(\sigma\) for numerical stability.
6.4 Closed-form KL divergence
For \(q = \mathcal{N}(\mu, \text{diag}(\sigma^2))\) and \(p = \mathcal{N}(0, I)\), both \(k\)-dimensional:
Derivation. Using the general formula for KL between two Gaussians:
Substitute \(\mu_0 = 0\), \(\Sigma_0 = I\), \(\mu_1 = \mu\), \(\Sigma_1 = \text{diag}(\sigma^2)\):
- \(\text{tr}(I^{-1}\thinspace\text{diag}(\sigma^2)) = \sum_j \sigma_j^2\)
- \(\mu^T I^{-1} \mu = \sum_j \mu_j^2\)
- \(\log\frac{|I|}{|\text{diag}(\sigma^2)|} = -\sum_j \log\sigma_j^2\)
Result: the closed-form KL divergence is
Each latent dimension contributes independently, so this term is evaluated in closed form from the encoder outputs alone — no sampling of \(z\) is required.
6.5 Reparameterization trick
To backpropagate through the sampling \(z \sim q_\theta(z \mid x)\), express the random variable as a deterministic function of the parameters plus external noise:
This moves the stochasticity to \(\varepsilon\), which does not depend on \(\theta\), allowing gradients to flow through \(\mu\) and \(\sigma\) via standard backpropagation.
Without this trick, computing \(\nabla_\theta \mathbb E_{q_\theta}[\cdot]\) would require high-variance score function estimators (REINFORCE).
6.6 Full VAE loss
For a single sample, using one Monte Carlo sample of \(\varepsilon\):
Over the dataset:
Encoder and decoder are trained jointly on this single average, in which the reconstruction term rewards faithful decoding and the KL term pulls every posterior toward the prior.
7. Failure Cases
-
Identity map (no bottleneck). If \(k \geq d\) with no regularization, the autoencoder can learn \(f = g^{-1}\): perfect reconstruction but no useful features. Always constrain: bottleneck (\(k < d\)), sparsity, noise, or KL.
-
Blurry VAE reconstructions. MSE reconstruction loss averages over modes, producing blurry outputs. This is fundamental: the Gaussian decoder \(p_\phi(x|z) = \mathcal{N}(\mu_\phi(z), \sigma^2 I)\) minimizes MSE, which penalizes sharpness. Alternatives: learned variance, adversarial loss.
-
KL vanishing / posterior collapse. In VAE training, a powerful decoder can reconstruct from \(p(z)\) alone, ignoring the encoder. The KL term drops to zero (\(q \approx p\)) and the latent code carries no information.
-
Mitigations: KL annealing (warmup \(\beta\) from 0 to 1), free bits, or weaker decoders.
-
Uninterpretable latent space. A deterministic autoencoder's latent space may have gaps and irregular structure. The latent code is optimized for reconstruction, not for downstream tasks or smooth interpolation.
-
Bottleneck too small. If \(k\) is much smaller than the intrinsic dimensionality of the data, reconstruction quality degrades sharply and important structure is lost.
-
Bottleneck too large. If \(k\) is close to \(d\), the model memorizes rather than compresses. Validation reconstruction error plateaus but generalization to new data does not improve.
8. Connections
- PCA — linear autoencoder recovers PCA subspace (§3.2)
- Neural Networks — encoder/decoder are MLPs trained with backpropagation
- Dimensionality Reduction — autoencoder as nonlinear alternative
- Probabilistic View — VAE as variational inference
- Information Theory — KL divergence, ELBO
9. References
- Kingma, D. P., & Welling, M. (2013). Auto-encoding variational Bayes. arXiv preprint arXiv:1312.6114.
- Baldi, P., & Hornik, K. (1989). Neural networks and principal component analysis: Learning from examples without local minima. Neural Networks, 2(1), 53–58.
- Vincent, P., Larochelle, H., Lajoie, I., Bengio, Y., & Manzagol, P. A. (2010). Stacked denoising autoencoders: Learning useful representations in a deep network with a local denoising criterion. Journal of Machine Learning Research, 11, 3371–3408.
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press. Chapter 14: Autoencoders & Chapter 20: Generative Models.