4.5.2 · D3Generative Models

Worked examples — Autoencoders fundamentals

3,746 words17 min readBack to topic

This is the practice companion to Autoencoders fundamentals. The parent note built the machinery — encoder, bottleneck, decoder, MSE vs BCE loss. Here we do the opposite of hand-waving: we enumerate every kind of situation an autoencoder problem can put in front of you, then solve one concrete example for each. If a scenario is not below, tell us — the goal is total coverage.

Every symbol we use is first re-anchored so you can read this cold.

Recall Quick symbol refresher (click to expand)
  • ::: the input — a list of numbers (a vector). For an image it is the pixel values laid in a row.
  • ::: the reconstruction — what the decoder spits out, our attempt to rebuild .
  • ::: the latent code — the small squeezed representation in the middle (the bottleneck).
  • ::: how many numbers are in (input dimension). ::: how many are in .
  • ::: the encoder function ( is just the name-tag for its weights) — it is the machine, . ::: the decoder, .
  • ::: "squared length" — square each entry of and add them up.

The scenario matrix

Think of a topic as a machine that can be fed many kinds of situation. Below is the full menu, followed by a schematic that groups the nine cases so you can see how they relate. Every worked example is tagged with the cell it covers, so you can check nothing is missing.

# Scenario class What is special about it Example
A Perfect compressible data — points sit on a line/plane intrinsic dimension < input dimension, exact reconstruction possible Ex 1
B Bottleneck too wide () — degenerate network cheats with the identity map Ex 2
C Continuous data, Gaussian noise — pick the loss MSE is the right answer, compute it by hand Ex 3
D Binary data — pick the loss BCE is right; MSE gives a wrong-feeling penalty Ex 4
E Boundary / degenerate loss input or BCE blows up to (the log-of-zero trap) Ex 5
F Gradient flow through the bottleneck backprop by the chain rule on a tiny net Ex 6
G Real-world word problem — sensor compression translate a story into and a compression ratio Ex 7
H Capacity / information limit — how small can go? Shannon source-coding bound, a counting argument Ex 8
I Exam twist — data NOT on a line, undercomplete forced error irreducible reconstruction error, the "elbow" Ex 9

The schematic below shows the same nine cases grouped into three families — geometry of the data, choosing the loss, and the mechanics of learning — so the matrix is not just a flat list.

Figure — Autoencoders fundamentals

We will hit A through I. Cases A, B, I are geometric, so they get figures.


Case A — data that compresses perfectly

Look at Figure s01 below: all data sits on one ray through the origin (lavender line, coral dots). The mint arrow shows the single number — how far along the line — that fully describes each point. That is why one latent dimension suffices. Here are the two entries of the encoder and the two entries of the decoder .

Figure — Autoencoders fundamentals
Figure s01 — the three data points on ; the mint arrow is the latent coordinate = signed distance along the line.

  1. Pick the encoder as "distance along the line". Choose , so . Why this step? We only need to be some faithful label of position. The simplest label is the first coordinate — since is forced to , knowing knows everything.
  2. Pick the decoder to rebuild both coordinates from . We need and , so set . Why this step? The decoder must invert step 1 and re-inject the line's constraint . That constraint is literally the 2:1 ratio.
  3. Read off the pattern. Decoder ratio = the line's slope. The autoencoder discovered the line. Why this step? This is the parent note's claim "linear AE = PCA": the learned direction is the principal axis of the data.

Verify. Point : ; ; . Reconstruction ✓. Loss . Zero error because the data's intrinsic dimension (1) equals (1).


Case B — the bottleneck that cheats

  1. Set (identity matrix) and . Then and . Why this step? With as many latent slots as inputs, the net can literally copy the input through. No squeezing happens. ( = the matrix with 1's on the diagonal and 0's elsewhere; multiplying by it changes nothing.)
  2. Compute the loss. for every . Why this step? Perfect score — and that is exactly the trap. Zero loss but the latent code learned no structure; it's just the input again.
  3. Contrast with the picture. In Figure s02, the wide bottleneck (right) passes the cloud through unchanged; the narrow bottleneck (left) is forced to flatten the cloud onto a line, discovering its main axis. Why this step? This is the parent's "Common Mistake: bottleneck too wide". Compression requires .

Figure — Autoencoders fundamentals
Figure s02 — left: narrow projects the cloud onto its principal axis (coral = reconstruction, mint = the axis). Right: wide leaves the cloud untouched — zero loss, zero learning.

Verify. For : , , loss . The identity map achieves zero loss ⇒ the metric alone cannot tell you the model is useless. You must constrain .


Case C — continuous data → MSE

Recall MSE: average of the squared per-entry errors. Let stand for the number of entries in the vector — for a single input vector this is exactly (the input dimension from the refresher). We keep the letter only to match the parent note's formula: In this example .

  1. List the per-entry errors. ; ; ; . Why this step? MSE never cares about sign — the square erases it — so we can proceed to squaring.
  2. Square each. . Why this step? Squaring is why large errors () hurt far more than small ones (). This is the Gaussian-noise assumption from the parent note showing up.
  3. Average over . . Why this step? Dividing by makes the loss comparable across vectors of different lengths.

Verify. Sum , divide by 4 . The single error contributed of the loss — confirming the forecast that big errors dominate. Units: same as input-squared (a "power-like" quantity), dimensionally consistent with a squared distance.


Case D — binary data → BCE

For a single binary pixel, BCE reduces (since kills the second term) to

  1. Confident-correct, . . Why this step? of something near 1 is near 0 → tiny penalty. Being right and sure is cheap, as it should be.
  2. Confident-wrong, . . Why this step? As while , . BCE screams when you are confidently wrong.
  3. Compare with what MSE would say. MSE here: vs — ratio . BCE ratio: . Both punish the wrong guess, but BCE's punishment grows unboundedly toward the boundary, matching the Bernoulli likelihood. Why this step? The parent note's rule: match the loss to the data's noise model. For 0/1 data the likelihood is Bernoulli ⇒ BCE.

Verify. , , ratio . The wrong guess costs about 22× more — so "roughly 20×" was the right forecast.


Case E — the log-of-zero trap

  1. Plug into . . Why this step? The logarithm has a vertical asymptote at 0 — the "confidently wrong" penalty is unbounded, not just large. This is the limiting behaviour the matrix demanded we cover.
  2. The mirror case: and . Then too. Why this step? Symmetry — a confident-wrong output at either extreme blows up.
  3. The engineering fix: clamp. Real code clamps into with e.g. , so stays finite. Equivalently, use a sigmoid output which never exactly reaches 0 or 1. Why this step? Without clamping, one saturated pixel produces NaN gradients and destroys training. This is why the parent's MNIST decoder ends in a sigmoid.

Verify. — the clamped worst-case penalty, large but finite. The un-clamped value is , confirming the "literal infinity" forecast.


Case F — gradient through the bottleneck

We chain the loss backward: loss ← . Here is the single encoder weight (entry of ) and the single decoder weight (entry of ).

  1. . Differentiate . Why this step? This is the "how wrong are we" signal that gets pushed backward.
  2. and . Straight from , . Why this step? Each factor is one layer's local derivative; the chain rule multiplies them.
  3. Multiply, then evaluate. . At first compute and , so , giving . Why this step? The chain rule multiplies the three local derivatives into the full gradient; evaluating at the given point shows the encoder weight multiplies the signal reaching . If (or ) shrinks toward 0, the encoder's gradient vanishes — this is the parent's warning "gradients flow through the bottleneck". Here the gradient is 0 because the fit is already perfect.

Verify. . Zero gradient at a perfect reconstruction ⇒ we are already at a loss minimum, matching the forecast. Sanity: nudge (imperfect), then , gradient , so learning would resume; the negative sign says "increase ", which raises toward ✓.


Case G — real-world word problem

  1. Compute . . So . Why this step? We must flatten the window into one vector before it enters the encoder — dimensions multiply.
  2. Compression ratio . A 25× squeeze. Why this step? This ratio tells you how aggressive the bottleneck is; the parent used a similar 25× for MNIST ().
  3. Choose the loss. Sensor readings are continuous real numbers with roughly Gaussian noise ⇒ MSE. Why this step? BCE only makes sense for values in modelled as Bernoulli. Heart rate is not a coin flip.

Verify. ✓; exactly ✓. The forecast "25" matches. Units check: is a pure count, ratio is dimensionless — correct for a compression factor.


Case H — the information / capacity limit

The bound (parent note): for lossless coding you need where = intensity levels per input pixel, = distinguishable values per latent slot, and = the information content in bits.

  1. Input information. pixels, levels ⇒ bits each ⇒ bits. Why this step? This counts the worst-case bits needed if pixels were totally independent (the upper bound on content).
  2. Latent capacity per slot. bits per latent number. Why this step? Each latent slot can carry only its bit-budget; we tally the total budget next.
  3. Solve for . Need . Why this step? Below 32 slots there aren't enough bits to distinguish every possible patch — reconstruction must lose information. Note: real image patches have low intrinsic dimension, so in practice far fewer slots work; this is the worst-case ceiling.

Verify. bits; . Minimum — so "more than 20" was right. Sanity: if latent slots held only bits each (), you'd need slots and no compression is guaranteed.


Case I — the exam twist: forced (irreducible) error

The 1D linear AE reconstructs a point as its projection onto : , where is the dot product (multiply matching entries, add them). Here plays the role of both the encoder direction (it computes the latent number ) and the decoder direction (it maps back with ).

Figure — Autoencoders fundamentals
Figure s03 — the two points (coral) and their forced reconstructions on the mint line (lavender). The dashed drops are the irreducible errors — the parts perpendicular to that one latent number can never store.

  1. Project . . So . Why this step? With one latent number, every reconstruction is forced to land on the line — see the dashed drops in Figure s03. can't be reached exactly.
  2. Error for . , squared length . Why this step? This leftover is the irreducible error — the part of perpendicular to , which a 1D code can never store.
  3. By symmetry, gives the same . Total loss . Why this step? This is the exam lesson: when intrinsic dimension (here 2) exceeds (here 1), loss has a floor > 0. Plotting loss vs shows an "elbow" — beyond it, adding latent slots stops helping.

Verify. , error; total . Nonzero, confirming the forecast that unequal-direction points cannot be compressed losslessly to 1D. (Best possible = first PCA axis still leaves a positive floor here.)


Recall Scenario self-test (cover, then reveal)

Which loss for continuous sensor data, and why? ::: MSE — Gaussian noise model; large errors squared → dominate. Why does give zero loss but zero learning? ::: The network learns the identity map; no compression, latent code meaningless. What happens to BCE as while ? ::: It diverges to ; implementations clamp to . A 1D linear AE on points not sharing a direction gives what loss? ::: Nonzero — an irreducible floor from the perpendicular component. In backprop, why don't we special-case negative inputs ? ::: The gradient is a signed product; sign flips cancel so descent still moves toward in every quadrant.

Where to go next