4.5.2 · D4Generative Models

Exercises — Autoencoders fundamentals

2,444 words11 min readBack to topic

Before we start, one reminder of the three objects, since every problem leans on them:

Figure — Autoencoders fundamentals

Level 1 — Recognition

Exercise 1.1

A network maps to (same size, no shrinking), then back to . Is this a useful autoencoder? Answer yes/no and name the failure in one phrase.

Recall Solution

No. With there is no bottleneck, so the network can learn the identity mapping (, ). It copies its input perfectly while learning no structure. The latent code is meaningless. The whole point is — the shrinking is what forces learning.

Exercise 1.2

Match each data type to the loss it should use: (a) grayscale photos with pixel values in treated as continuous intensities; (b) strictly black-or-white pixels (each is exactly 0 or 1).

Recall Solution
  • (a) continuous → MSE (Mean Squared Error), because we assume Gaussian noise around the reconstruction.
  • (b) binary → BCE (Binary Cross-Entropy), because each pixel is a coin-flip (Bernoulli), 0 or 1.

The rule: the loss must match the assumed noise model of the data, not personal preference.

Exercise 1.3

An autoencoder trains with no labels — the target it tries to match is the input itself. Which learning paradigm is this?

Recall Solution

Unsupervised learning — see Unsupervised Learning. The "auto" in autoencoder means it makes its own targets: the label is the input.


Level 2 — Application

Exercise 2.1

A single pixel has true value . The decoder outputs . Compute the per-pixel BCE: Then recompute for . Which prediction is punished harder, and by roughly how many times?

Recall Solution

With , the second term vanishes, leaving .

  • : (using natural log ).
  • : . Ratio . The confident-but-wrong guess ( when truth is ) is punished about 22× harder. That steepness is exactly why BCE suits binary data.

Exercise 2.2

Two pixels: truth , reconstruction . Compute the MSE

Recall Solution

Errors: and . Squares: and . Sum . Divide by :

Exercise 2.3

Compression ratio. An MNIST autoencoder maps down to . Compute the compression factor , rounded to one decimal.

Recall Solution

So the latent code is a ~24.5× shrink. The parent note called it "~25× compression" — same thing rounded.


Level 3 — Analysis

Exercise 3.1

A linear autoencoder (no activation functions) is trained on data lying exactly on the line in , with a 1-dimensional bottleneck. Encoder , decoder . What ratio must the network learn, and why?

Recall Solution

Every valid data point has the form for some scalar — one degree of freedom. The single number must stand in for . On decode, and , so the decoder output always sits on the line . To land on the true data line we need Geometric picture: the decoder can only produce points along one direction it picks. Training forces that direction to align with the data line — this is exactly PCA finding the first principal axis. See figure below.

Figure — Autoencoders fundamentals

Exercise 3.2

A team observes that increasing from to keeps lowering the reconstruction error smoothly toward zero, and they conclude "bigger latent is strictly better." What is the flaw in this reasoning, and what one diagnostic plot exposes it?

Recall Solution

Lower reconstruction error is not the goal — useful, compressed structure is. As , error falls to zero because the net approaches the identity map, storing the input verbatim instead of learning structure. Error-goes-down is consistent with the worst outcome (memorisation). Diagnostic: plot reconstruction error vs and find the elbow — the point after which extra dimensions barely help. Choose at the elbow, well below . The elbow marks the data's intrinsic dimensionality.

Exercise 3.3

Using the capacity bound from the parent note, : suppose an image truly needs bits of information to specify, and each latent unit can carry distinct levels. What is the smallest integer that can still perfectly encode it?

Recall Solution

Each unit carries bits. We need Smallest integer: . Fewer than 20 units cannot hold 40 bits at 2 bits each — reconstruction must lose information (Shannon's source-coding floor).


Level 4 — Synthesis

Exercise 4.1

Design (on paper) a symmetric autoencoder for binary images. Specify: input dim, one hidden layer each side, latent dim = 16, activations, and output activation. Justify the output activation and the loss.

Recall Solution
  • Input: flattened to .
  • Encoder: (ReLU) (linear).
  • Decoder: (ReLU) (sigmoid).
  • Loss: BCE. Why sigmoid on output? BCE needs each output in to read as a probability "this pixel is on"; sigmoid squashes any real number into . Why BCE not MSE? Pixels are binary (Bernoulli), so BCE is the matched likelihood — it punishes confident wrong pixels far more (recall Exercise 2.1). This mirrors the MNIST design in the parent note; see Encoder-Decoder Architectures.

Exercise 4.2

You have two trained latent codes and for two different digits. Write the formula for the midpoint interpolation and describe what decoding it should produce in a well-trained autoencoder.

Recall Solution

Linear interpolation with mixing weight : The midpoint is : . Decoding should give a plausible in-between image — a smooth morph — if the latent space is smooth. Plain autoencoders often produce blurry or nonsensical midpoints because they don't enforce smoothness; Variational Autoencoders (VAE) add that constraint.

Exercise 4.3

Convert the plain autoencoder above into a sparse autoencoder conceptually. What single new term is added to the loss and what does it encourage?

Recall Solution

Add a sparsity penalty to the loss: where penalises the average activation of hidden units (e.g. an L1 term or a KL term pushing mean activation toward a small target ), and controls its strength. This forces few units active at once, so each unit learns a distinct feature — the Sparse Autoencoders idea. Note it lets you keep a wide hidden layer while still avoiding the identity trap, because sparsity (not size) does the constraining.


Level 5 — Mastery

Exercise 5.1

Prove that for continuous data with Gaussian noise , maximising the likelihood is equivalent to minimising squared error, and show the noise variance does not change the optimum.

Recall Solution

The Gaussian density (dropping the normalising constant that doesn't depend on ) is Step 1 (why log): maximising a product/exponential is awkward; the logarithm is monotone increasing, so . Take : Step 2 (why negate): we minimise by convention, so flip sign. Since is a constant it drops out of : Step 3 (why vanishes): is a positive constant multiplier. Scaling an objective by a positive constant never moves its minimiser. Hence

Exercise 5.2

For BCE with a single pixel and true value , show that the loss is minimised exactly at . (Treat as a fixed number in and set the derivative to zero.)

Recall Solution

Per-pixel loss: . Differentiate with respect to (using and chain rule on ): Set to zero: The second derivative is positive (both terms are positive), so this is a minimum. Thus BCE is honestly minimised only when the guess equals the truth.

Exercise 5.3

Generalise the capacity argument: a dataset has intrinsic dimensionality . Argue why setting guarantees some reconstruction error no matter how long you train, while permits (does not guarantee) zero error.

Recall Solution

By definition, the data lives on a manifold needing independent coordinates to specify a point. The decoder is a function ; its image (the set of reconstructable points) has dimension at most .

  • If : the decoder's reachable set is a lower-dimensional sheet than the data manifold, so it cannot cover every data point. Some points are unreachable ⇒ nonzero error for those, guaranteed, however long you train. This is the Shannon floor .
  • If : the decoder's image can be made large enough to contain the whole manifold, so zero error is possible — but only if training actually finds such weights. Enough room ≠ guaranteed success (optimisation may still fail). Hence "permits", not "guarantees".