4.5.2Generative Models

Autoencoders fundamentals

2,572 words12 min readdifficulty · medium
Figure — Autoencoders fundamentals

What is an Autoencoder?

WHY three components? The encoder discovers what's important, the bottleneck forces compression (preventing the network from just memorizing), and the decoder tests if the compressed representation is sufficient.

WHAT makes it "auto"? The network teaches itself – the target output is the input. No labeled data required.

Architecture Deep-Dive

Basic Structure

The simplest autoencoder is symmetric:

Encoder: xRnW1,b1h1σh2W2,b2zRm\mathbf{x} \in \mathbb{R}^n \xrightarrow{W_1, b_1} \mathbf{h}_1 \xrightarrow{\sigma} \mathbf{h}_2 \xrightarrow{W_2, b_2} \mathbf{z} \in \mathbb{R}^m

Decoder (mirror): zW3,b3h3σh4W4,b4x^Rn\mathbf{z} \xrightarrow{W_3, b_3} \mathbf{h}_3 \xrightarrow{\sigma} \mathbf{h}_4 \xrightarrow{W_4, b_4} \hat{\mathbf{x}} \in \mathbb{R}^n

where m<nm< n (bottleneck), σ\sigma is activation (ReLU, tanh), WiW_i are weight matrices.

WHY symmetric? Not required, but common. It mirrors the compression/decompression symmetry and reduces hyperparameters.

Deriving the Loss Function

Goal: Make reconstruction x^\hat{\mathbf{x}} close to original x\mathbf{x}.

For continuous data (images, sensor readings), assume Gaussian noise: p(xx^)=N(xx^,σ2I)p(\mathbf{x}|\hat{\mathbf{x}}) = \mathcal{N}(\mathbf{x}|\hat{\mathbf{x}}, \sigma^2 I)

Maximum likelihood estimation gives: maxp(xx^)=maxexp(12σ2xx^2)\max p(\mathbf{x}|\hat{\mathbf{x}}) = \max \exp\left(-\frac{1}{2\sigma^2}\|\mathbf{x} - \hat{\mathbf{x}}\|^2\right)

Taking negative log (for minimization):

For binary data (black/white images, binary vectors), use binary cross-entropy:

WHY different losses? The loss matches the data type and assumed noise model. MSE for continuous, BCE for binary.

Training Process

Forward Pass

  1. Encode: z=fϕ(x)=σ(W2σ(W1x+b1)+b2)\mathbf{z} = f_\phi(\mathbf{x}) = \sigma(W_2 \sigma(W_1 \mathbf{x} + b_1) + b_2)
  2. Decode: x^=gθ(z)=σ(W4σ(W3z+b3)+b4)\hat{\mathbf{x}} = g_\theta(\mathbf{z}) = \sigma(W_4 \sigma(W_3 \mathbf{z} + b_3) + b_4)
  3. Compute loss: L=xx^2\mathcal{L} = \|\mathbf{x} - \hat{\mathbf{x}}\|^2

Backward Pass (Gradient Derivation)

Chain rule through decoder → bottleneck → encoder:

LW1=Lx^x^zzW1\frac{\partial \mathcal{L}}{\partial W_1} = \frac{\partial \mathcal{L}}{\partial \hat{\mathbf{x}}} \frac{\partial \hat{\mathbf{x}}}{\partial \mathbf{z}} \frac{\partial \mathbf{z}}{\partial W_1}

WHY this matters? The gradient flows through the bottleneck. If z\mathbf{z} is too small, gradients vanish. If too large, the network just memorizes.

HOW to prevent memorization?

  1. Make dim(z)\dim(\mathbf{z}) small enough to force compression
  2. Add regularization (later: VAE adds KL divergence, sparse autoencoders add sparsity penalty)

The Bottleneck: Why Compression Matters

The latent dimension dzd_z determines behavior:

dzd_z Behavior Problem
dzdxd_z \geq d_x Identity mapping No compression, memorization
dzdx/2d_z \approx d_x / 2 Moderate compression Balanced
dzdxd_z \ll d_x Strong compression May lose important details

Derivation of capacity:

Information content: I(x)dxlog2(L)I(\mathbf{x}) \leq d_x \log_2(L) bits (for LL quantization levels per dimension).

Bottleneck capacity: I(z)dzlog2(Lz)I(\mathbf{z}) \leq d_z \log_2(L_z) bits.

For perfect reconstruction: dzlog2(Lz)I(x)d_z \log_2(L_z) \geq I(\mathbf{x}) (Shannon's source coding theorem).

WHY can we compress? Real data has intrinsic dimensionality dintrinsicdxd_{\text{intrinsic}} \ll d_x. Images of faces live on a low-dimensional manifold (face space), not uniformly filling R784\mathbb{R}^{784}.

Properties of Latent Space

Interpolation

Can we smoothly morph between two images?

Given xA,xB\mathbf{x}_A, \mathbf{x}_B, compute latent codes: zA=fϕ(xA),zB=fϕ(xB)\mathbf{z}_A = f_\phi(\mathbf{x}_A), \quad \mathbf{z}_B = f_\phi(\mathbf{x}_B)

Interpolate: zt=(1t)zA+tzB,t[0,1]\mathbf{z}_t = (1-t)\mathbf{z}_A + t\mathbf{z}_B, \quad t \in [0,1]

Decode: xt=gθ(zt)\mathbf{x}_t = g_\theta(\mathbf{z}_t)

WHAT do we see? Smooth transitions (e.g., "3" gradually morphs into "8").

WHY? The encoder maps similar inputs to nearby latent points. Linear interpolation in latent space → semantic interpolation in data space.

Limitation: Basic autoencoders have gaps in latent space. Not all z\mathbf{z} decode to realistic images. (VAEs fix this by forcing continuous distributions.)

Dimensionality Reduction Comparison

Method Type Latent Space Nonlinear?
PCA Linear Orthogonal
Autoencoder (linear) Linear Equivalent to PCA
Autoencoder (nonlinear) Nonlinear Learned manifold
t-SNE Nonlinear Visualization only

Proof that linear AE = PCA:

Minimize L=xWTx2\mathcal{L} = \|\mathbf{x} - W^T\mathbf{x}\|^2 where WRn×kW \in \mathbb{R}^{n \times k} (encoder), WTW^T (decoder).

Taking gradient: LW=0\frac{\partial \mathcal{L}}{\partial W} = 0 gives eigenvectors of covariance matrix E[xxT]\mathbb{E}[\mathbf{x}\mathbf{x}^T].

This is PCA. WHY use autoencoders then? Nonlinear activations let them learn curved manifolds, not just hyperplanes.

Recall Explain to a 12-Year-Old

Imagine you're sending photos to a friend, but the internet is slow. You can't send the full 1MB photo. So you write down: "It's a cat, orange, lying on a couch, sunlight from left." That's maybe 50bytes! Your friend reads this description and draws a picture. It won't be perfect, but it'll capture the main idea.

That's an autoencoder! The encoder (you) looks at the photo and writes down the key facts. The decoder (friend) reads those facts and recreates the photo. The latent code is the short description.

Training is like practicing: You send the photo, friend draws it, you compare. If the cat looks like a dog, you adjust your description ("add: pointy ears, whiskers"). After many tries, the descriptions get better, and the drawings look more like the originals.

The magic: the description is tiny compared to the photo, but contains everything important!

Use Cases

  1. Dimensionality reduction: Visualize high-D data (alternative to PCA, t-SNE)
  2. Anomaly detection: High reconstruction error → outlier (e.g., fraud detection)
  3. Denoising: Train on noisy inputs, clean targets (denoising autoencoders)
  4. Pretraining: Encoder as initialization for supervised task (transfer learning)
  5. Data compression: Losy compression (though not competitive with JPEG/H.264)

WHY anomaly detection? Network learns "normal" data distribution. Unusual inputs (anomalies) reconstruct poorly because they don't fit learned patterns.

Connections


#flashcards/ai-ml

What are the three components of an autoencoder? :: Encoder (maps input to latent code), Latent space (compressed representation), Decoder (reconstructs input from latent code)

What is the training objective of an autoencoder?
Minimize reconstruction loss between input x and reconstructed output x̂, typically using MSE or BCE depending on data type
Why is the bottleneck dimension important in autoencoders?
It must be smaller than input dimension to force compression and prevent memorization. If too large, network learns identity mapping; if too small, loses important information.
What loss function should be used for binary data in autoencoders?
Binary Cross-Entropy (BCE), derived from Bernoulli likelihood: -Σ[x_i log(x̂_i) + (1-x_i)log(1-x̂_i)]
What loss function should be used for continuous data in autoencoders?
Mean Squared Error (MSE): (1/n)Σ(x_i - x̂_i)², derived from Gaussian likelihood assumption
What is the relationship between linear autoencoders and PCA?
Linear autoencoders are mathematically equivalent to PCA—they learn eigenvectors of the data covariance matrix that capture maximum variance
How does interpolation work in autoencoder latent space?
Encode two inputs to z_A and z_B, interpolate linearly z_t = (1-t)z_A + t z_B, then decode z_t to get smooth transitions between original inputs
Why do autoencoders work for data compression?
Real data has intrinsic dimensionality much lower than ambient dimensionality due to redundancy and structure. Autoencoders discover this low-dimensional manifold.
What is a common mistake when setting bottleneck size?
Making it too large (d_z ≥ d_x) allows identity mapping without learning useful compressed representations. Fix: set d_z significantly smaller than d_x
How are autoencoders used for anomaly detection?
Network learns to reconstruct "normal" data well. Anomalies have high reconstruction error because they don't fit the learned distribution, making them detectable.
What activation function is typically used in the decoder output layer for image reconstruction?
Sigmoid for binary/normalized images (outputs in [0,1]), or linear/tanh depending on data range. Must match the expected output distribution.
What prevents an autoencoder from learning the identity function?
The dimensionality bottleneck constraint (d_z < d_x) and/or regularization techniques (sparsity penalties, noise injection, KL divergence in VAEs)

Concept Map

contains

contains

maps x to

bottleneck dim less than input

reconstructed by

outputs

forces discovery of

exploits

compared to input via

continuous data

binary data

derived from

Autoencoder

Encoder f_phi

Decoder g_theta

Latent space z

Information Bottleneck

Reconstruction x-hat

Underlying structure

Data redundancy

Reconstruction loss

MSE loss

BCE loss

Gaussian MLE

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Autoencoder ek neural network hai jo data ko compress karke chota representation banata hai (encoding), phir wapas original form mein reconstruct karta hai (decoding). Sochiye jaise ap ek bada suitcase pack kar rahe ho – apko decide karna padta hai ki kya zaroori hai, kya redundant hai, aur destination pe jake sab kaise wapas unpack karenge. Real-world data mein bohot redundancy hoti hai – images mein spatial patterns, text mein semantic structure. Autoencoder ek information bottleneck create karta hai, jisse network ko underlying structure discover karna force hota hai.

Teen components hote hain: Encoder input ko latent space mein map karta hai (compressed form), Latent space woh small representation hai (jaise 784 pixels ko 32 numbers mein compress karna), aur Decoder us compressed code se original input ko reconstruct karta hai. Training objective simple hai: reconstruction loss minimize karo – matlab input aur reconstructed output ka difference kam karo, using MSE (continuous data ke liye) ya BCE (binary data ke liye).

Autoencoder ka magic bottleneck dimension mein hai. Agar latent space bohot bada hai toh network sirf memorize kar lega (identity mapping), kuch seekhega nahi. Agar bohot chota hai toh important details kho jayengi. Sahi size se network ko data ka intrinsic structure seekhna padta hai – jaise MNIST digits mein 784 pixels hain but actually wo 10-20 dimensions ki manifold pe live karte hain (digit identity, rotation, thickness). Latent space mein similar inputs (jaise different "3"s) pas-paas cluster hote hain, aur interpolation se smooth transitions milte hain (jaise "3" gradually "8" mein morph ho). Ye unsupervised learning hai – koi labels nahi chahiye, network khud seekhta hai ki data mein kya important hai!

Go deeper — visual, from zero

Test yourself — Generative Models

Connections