What a Latent Space Is — and Settling Who Owns the Term
The ground-level definition of a latent space is plain: a coordinate system with far fewer dimensions than the raw data, where every sample maps to a vector z, and the axes capture the data's true factors of variation. The supporting idea is the manifold hypothesis: high-dimensional data (a 512×512×3 image is ~786k dimensions) does not fill its ambient space — it concentrates near a low-dimensional manifold. The things a face photo can vary in are few: pose, lighting, expression, age. A latent space is a coordinate chart for that manifold.
In LLM contexts, however, the term is a loan word, and a loosely borrowed one — worth flagging up front to avoid a level confusion. People say "a Transformer's hidden states are its latent space." Hidden states are indeed intermediate representations, but they differ from a VAE latent in three mechanistic ways: (1) hidden states are deterministic intermediate activations — same input, same vector, no probabilistic structure; (2) there is no prior over them — nothing requires hidden states to follow any distribution, so you cannot "sample from hidden-state space" and decode a valid sentence; (3) they exist to serve the next layer's computation, not sampling and generation. A VAE latent z, by contrast, is a genuine random variable: it has an explicit prior p(z) = N(0, I), a posterior q(z|x), and the whole space is deliberately sculpted by the training objective so that any point decodes into a plausible sample. This article is about how that sculpting is done.
The Plain Autoencoder's Defect: It Compresses, It Cannot Generate
Start with the control group. An autoencoder (AE) is: an encoder squeezes x into a low-dimensional vector z, a decoder reconstructs x̂ from z, and the training objective is a single term — minimize reconstruction error (e.g. ‖x − x̂‖²).
That objective determines the artifact: the AE only cares whether the training samples' code points can be decoded back; it imposes no requirement whatsoever on the overall shape of z-space. The trained latent space looks like this:
A plain AE's latent space (2-D slice of z-space)
● ● ● = code point of a training sample
● ● (decoder is only defined near these)
● ✗ ●
● ✗ ● ✗ = holes: no training signal ever
● ● passed here; the decoder's output
● ● at these points is garbage
The code points are scattered islands with large holes between them: no gradient ever flowed through those regions, so the decoder's behavior there is entirely undefined. Feed the decoder the midpoint between two code points and you usually get shattered noise, not "a blend of the two." So an AE is a competent compressor but not a generator: you don't know where to sample — there is no prior telling you where valid z's live, and landing near a code point is pure luck. The defect is rooted not in the architecture but in the objective: reconstruction loss rewards memorizing visited points, never rewards paving the space flat and full.
The VAE's Move: the Encoder Outputs Distribution Parameters, Not a Point
The first cut a VAE (Variational Autoencoder) [1] makes goes straight at that root: the encoder no longer outputs one vector z, but two vectors μ and σ — the parameters of a diagonal Gaussian N(μ, diag(σ²)). On every forward pass, z is sampled from that distribution before being handed to the decoder.
AE: x ──encoder──► z (a point) ──decoder──► x̂
VAE: x ──encoder──► (μ, σ) ← parameters of a distribution
│
z ~ N(μ, σ²) ← sampling
│
decoder ──► x̂
The geometric consequence: each training sample now occupies not a point in latent space but a cloud centered at μ with radius σ. The same x sees a slightly different z on every training step, forcing the decoder to decode x correctly across the whole cloud — the holes between points get filled by clouds, neighboring samples' clouds overlap, and decoding inside an overlap naturally yields a smooth blend of the two. Continuity is not a wish; it is extorted by sampling noise.
But clouds alone are not enough — they could drift anywhere, leaving the space globally sparse and broken. Hence the VAE's training objective (the ELBO, Evidence Lower BOund) has two terms:
log p(x) ≥ E_q [ log p(x|z) ] − KL( q(z|x) ‖ p(z) )
└────────┬────────┘ └────────┬────────┘
reconstruction: sampled regularizer: pull each sample's
z must decode back to x posterior cloud toward N(0, I)
The ELBO is a computable lower bound on log p(x) (the model's log-likelihood of the data, itself intractable); the word "variational" names the technique of approximating the true posterior with a tunable distribution q(z|x) [2]. Tag the levels explicitly: the mechanism layer is the encoder/sampling/decoder data path; the objective layer is the tug-of-war between the ELBO's two terms; the artifact layer is the thing we actually wanted — a continuous, smooth latent space packed around the origin, where any point decodes into a plausible sample. Generation then becomes trivial: sample z from p(z) = N(0, I), run the decoder, done. What the AE couldn't do is supplied exactly by the KL term — deep dive shortly.
The Reparameterization Trick: Moving Randomness Out of the Computation Graph
First, a problem that looks like a technicality but decides whether a VAE can train at all: "sampling" is not differentiable.
Backpropagation requires every node in the graph to be differentiable with respect to its inputs. But at the node z ~ N(μ, σ²), z is drawn at random — ∂z/∂μ is simply undefined, the gradient dies there, and the encoder (the weights producing μ and σ) never receives the signal "here is how to adjust the distribution parameters to reconstruct better."
The pre-existing general remedy was the score-function estimator (the REINFORCE family): don't differentiate through sampling; instead weight the loss by the gradient of log q. It is mathematically unbiased but has enormous variance — the signal drowns in noise and convergence is impractically slow. That is the control group.
The reparameterization trick [1] is an elegant transfer of responsibility:
z = μ + σ ⊙ ε , ε ~ N(0, I) (⊙ = element-wise product)
Read it term by term: ε is drawn from a standard normal and has nothing to do with model parameters; σ⊙ε scales the noise to the target standard deviation; +μ shifts it to the target center. The distribution of z is identical to sampling N(μ, σ²) directly — but the computation graph has changed:
Before reparameterization: After reparameterization:
μ ──┐ μ ────────┐
├──►(sample)──► z (+)──► z
σ ──┘ ▲ σ ──(×)───┘
│ ▲
gradient dies here ε ~ N(0,I)
▲
randomness outsourced to an
input that needs no gradient
Sampling still happens — but at ε, a leaf node with no trainable parameters upstream. With respect to μ and σ, z is now a deterministic, differentiable function: ∂z/∂μ = 1 and ∂z/∂σ = ε, and gradients flow unobstructed back into the encoder. Moreover, because the gradient is computed exactly for each concrete draw of ε, its variance is far below the score-function estimator's — this is the core contribution of [1] (the SGVB estimator), the step that turned the VAE from a mathematical construction into a trainable model.
One common misconception worth breaking: reparameterization does not "make sampling differentiable" — sampling never is — it rearranges the graph so that no sampling node sits on any path that needs gradients.
KL Divergence: Definition, Term-by-Term Reading, Asymmetry
KL divergence (Kullback–Leibler divergence) measures "how much information is lost when using distribution p to approximate distribution q":
KL(q ‖ p) = E_{z~q} [ log q(z) − log p(z) ] = ∫ q(z) · log( q(z)/p(z) ) dz
Term by term: log(q/p) is the pointwise "surprise gap" — at a point that is ordinary under q but rare under p, this term is a large positive number; the outer expectation weights by q, meaning the bill is only tallied where q has mass. The information-theoretic reading: if you design a compression code for p but the data actually comes from q, you pay an extra KL(q‖p) nats per sample on average. Two properties are the foundation for everything that follows:
Non-negative, and zero iff the distributions are equal (by Jensen's inequality). So KL can be used like a distance — but it is not a distance, because:
It is asymmetric: KL(q‖p) ≠ KL(p‖q), and the two directions behave in opposite ways. Suppose p is bimodal and you must approximate it with a single-mode Gaussian q:
minimize KL(q‖p) "reverse KL": minimize KL(p‖q) "forward KL":
p: ▲▲ ▲▲ p: ▲▲ ▲▲
q: ██ q: ██████
(squeeze into one mode) (flatten across both modes)
mode-seeking: wherever q has mass-covering: wherever p has
mass, p must not be zero, or mass, q must not be zero
log(q/p) explodes
→ rather miss a mode than → rather cover the valley than
straddle a low-density valley miss any mode
The VAE's regularizer is KL(q(z|x) ‖ p(z)) — q first: the reverse direction. The posterior cloud is only required to "stay within the prior's coverage," not to cover the entire prior. That direction is chosen not by taste but by computability: the expectation is weighted by q, and q is our own encoder — easy to sample from; the reverse weighting (by the true data posterior) would be intractable. When both q and p are diagonal Gaussians this KL has a closed form, per latent dimension:
KL = ½ · Σᵢ ( μᵢ² + σᵢ² − log σᵢ² − 1 )
Read each term as a geometric instruction: μ² pulls the cloud's center toward the origin (all samples' clouds huddle near the prior — the space doesn't scatter); σ² − log σ² − 1 attains its minimum 0 at σ = 1 — if σ tries to grow, the σ² term punishes it; if σ tries to shrink, −log σ² punishes it divergingly. That second pair of terms is the protagonist of the next section.
KL's Role in the VAE: the Anti-Degeneracy Anchor Against σ→0
Now a thought experiment: remove the KL term, keep only reconstruction loss — how does the model optimize?
Degeneration is inevitable. For reconstruction, sampling noise is pure interference — the farther z strays from μ, the worse the reconstruction. So the optimizer's best strategy is σ → 0: the clouds shrink to points, sampling exists in name only, the VAE quietly degenerates back into a plain AE, and the latent space shatters back into islands. Meanwhile μ has no reason to stay near the origin: samples' codes flee apart from each other (the farther apart, the less confusion, the easier reconstruction). You trained a VAE's architecture and got an AE's artifact.
The KL term is the anchor that holds against this degeneration, and the closed form above already spells out how: −log σ² diverges as σ→0 — the harder σ shrinks the harder it is punished, forcing every cloud to keep a minimum "fatness"; the μ² term drags the fleeing cloud centers back to the origin, forcing clouds to overlap. Reconstruction loss wants to tear the space apart; KL wants to knead it into a standard Gaussian ball. ELBO training is the sustained tug-of-war between these two forces, and the continuous, smooth, sampleable latent space we want is precisely the equilibrium of that tug-of-war — the credit belongs to neither term alone.
The tug-of-war can also tip the other way, giving VAE practice its most famous failure mode: posterior collapse. When the decoder is powerful enough on its own (typically an autoregressive LSTM/Transformer decoder that can model sentences unaided), it can reconstruct well without ever looking at z — and then, for the optimizer, driving KL straight to zero (setting q(z|x) = p(z), making z pure noise unrelated to x) is a free loss reduction. Bowman et al. [4] documented this systematically for text VAEs: under most hyperparameter settings, models consistently flatten q(z|x) into the prior, the KL term hits zero, and z is ignored entirely. Their two fixes are both mechanistic: KL annealing — start the KL weight at 0 and ramp it up during training, letting the model first learn to pack information into z before the anchor tightens; word dropout — randomly replace the decoder's input tokens with UNK, deliberately kicking away the decoder's autoregressive crutch so it must lean on z.
Turn the same knob the other way and you get β-VAE [3]: multiply the KL term by β > 1, strengthening the anchor. The cost is blurrier reconstruction (the information channel is throttled); the payoff is that latent dimensions are pushed toward the prior's independent structure, making disentangled representations easier to learn — one dimension for pose, one for lighting. VQ-VAE [5] instead sidesteps the tug-of-war altogether: it replaces the latent with a discrete codebook (encoder outputs are quantized to the nearest code), there is no KL term of the same form, the decoder cannot bypass the quantization bottleneck even if it wants to ignore the latent — so posterior collapse is structurally absent. That is a "change the mechanism" fix rather than a "tune the objective" fix, and the contrast makes vivid exactly what load the KL was carrying in the original design.
"KL as Anchor" Is a Design Pattern — It Reappears Verbatim in RLHF
Abstract the structure of the previous section: a primary objective (reconstruction) + a KL regularizer (pulling toward a reference distribution), with β setting the anchor's tension. This pattern applies far beyond VAEs — wherever "letting the primary objective optimize unconstrained digs out a degenerate solution," you need a KL anchor. LLM alignment is a textbook reprise.
In RLHF's RL stage, the policy model optimizes the score given by a reward model. But the reward model is only a proxy for human preference, and it has holes; optimized against without constraint, the policy finds regions that "score high but output garbage" — sycophantic boilerplate, bloated verbosity, exploiting the RM's scoring quirks. That is reward hacking. Structurally it is the same event as σ→0 in the VAE: the optimizer overfits the letter of the objective and crawls into a corner the designer never wanted. InstructGPT's [7] remedy is identical in shape — a per-token KL penalty in the objective:
objective = E[ r(x,y) ] − β · KL( π_RL ‖ π_SFT )
└────┬────┘ └────────┬─────────┘
primary: please the RM anchor: don't stray far from SFT
In the paper's own words, the term is added to "mitigate over-optimization of the reward model." The role played by π_SFT (the supervised fine-tuned model) is exactly the role played by the prior N(0,I) in the VAE: a known-reasonable reference distribution, with KL confining optimization to its trust radius. Note this KL is also in the reverse direction (π_RL first) — the policy only needs to stay inside SFT's behavioral coverage, not to cover all of SFT's behaviors; mode-seeking is precisely the desired property here.
DPO [8] pushes the relationship to full transparency: its starting point is the KL-constrained objective max E[r] − β·KL(π‖π_ref), which admits a closed-form optimal policy; DPO inverts it, bypassing both the reward model and RL entirely, reducing alignment to a classification loss. And that β does not disappear from the loss — it is the direct embodiment of KL constraint strength: large β, tight anchor, the model stays conservatively close to the reference; small β, loose anchor, larger deviations are licensed in exchange for preference gains (the paper notes the model degenerates without this weighting). Put the three β's side by side and they are one knob wearing three names:
System Primary objective KL anchor's reference Meaning of β
──────────────────────────────────────────────────────────────────────
VAE recon log p(x|z) prior N(0, I) compression/regularization
weight [1][3]
RLHF reward r(x,y) π_SFT anti reward-hacking [7]
DPO human preference π_ref licensed deviation from
the reference [8]
Practical Applications: the Roles VAEs Play in Real Systems
(1) Latent Diffusion / Stable Diffusion: the VAE is the compression substrate. LDM's [6] key decision was not to run diffusion in pixel space but to first train an autoencoder compressing 512×512×3 images into 64×64×4 latents; the diffusion model operates entirely in that small space, and the decoder restores resolution once at the end. It works because of the manifold hypothesis: perceptually important information is low-dimensional to begin with, and the 48× that gets squeezed out is mostly perceptual redundancy. This is "latent space as compute accelerator" — the VAE you see inside Stable Diffusion is the VAE this article is about.
(2) Anomaly detection: the VAE learns the distribution of "normal." Train a VAE on normal-only data and it carves the manifold of "normal" into its latent space; at inference, reconstruction error + the KL term together form an anomaly score — an off-manifold sample is both hard to reconstruct and has a posterior that strays from the prior. In a manufacturing setting: train on AOI images from an SMT line, and normal solder joints reconstruct cleanly while bridging or cold joints reconstruct blurry with spiking error; or train on normal sensor time series and an abnormal vibration signature sends the score jumping. The mechanistic reason a VAE beats a plain AE here: the AE's objective is reconstruction only and it often over-generalizes — reconstructing even unseen anomalies well, ruining the score; the VAE's probabilistic structure penalizes off-manifold inputs on principle, and the score carries an approximate-likelihood interpretation.
(3) Interpolation and data augmentation. A continuous latent space makes linear interpolation between z₁ and z₂ decode into semantically smooth transitions — garbage in an AE, a feature in a VAE. For scarce classes (rare defect morphologies, say), sample within a neighborhood of their codes and decode to synthesize training data. The conditional variant, CVAE, wires labels into encoder and decoder so you can request "generate a sample of defect class 3."
(4) Tuning memo. VAE tuning is almost entirely about managing the tug-of-war: blurry reconstruction → β too large or latent dimension too small; posterior collapse (KL≈0) → KL annealing, weaken the decoder, or free bits (set a per-dimension KL floor below which no penalty applies, preserving a minimum information flow); grow the latent dimension from small while monitoring per-dimension KL — a dimension flat at 0 long-term is a dead dimension, a signal of excess capacity.
Closing: One Logical Chain
The AE's objective rewards only reconstruction → its latent space shatters into islands with no prior to sample from → it compresses but cannot generate → the VAE has the encoder output μ/σ and lets sampling noise extort local continuity → but "sampling" is non-differentiable and training would break → reparameterization rewrites z as μ + σ⊙ε, outsourcing randomness to the parameter-free ε and restoring gradient flow → clouds alone still degenerate: σ→0 shrinks back to an AE → in the KL's closed form, −log σ² diverges to hold off the collapse and μ² gathers clouds toward the origin; the equilibrium of that tug-of-war is the continuous, sampleable space → anchor too loose degenerates, too tight collapses the posterior; β, annealing, and free bits all tune the same tug-of-war → and the same "primary objective + β·KL anchor" pattern reappears verbatim in RLHF: reward hacking is the isomorph of σ→0, π_SFT is the isomorph of the prior, and DPO's β is the anchor-tension knob. Understand the VAE's KL and you get the alignment β for free.
Natural next stops, whichever you want to dig into: diffusion models formalized as hierarchical VAEs (the ELBO derivation runs nearly parallel); IWAE (tightening the ELBO with multiple importance-weighted samples); normalizing flows (exact likelihood instead of a lower bound); and the other face of KL — why forward KL corresponds to maximum-likelihood training.
References
[1] Kingma, D. P., Welling, M. (2013). Auto-Encoding Variational Bayes. — https://arxiv.org/abs/1312.6114 [2] Kingma, D. P., Welling, M. (2019). An Introduction to Variational Autoencoders. — https://arxiv.org/abs/1906.02691 [3] Higgins, I. et al. (2017). β-VAE: Learning Basic Visual Concepts with a Constrained Variational Framework. ICLR 2017. — https://openreview.net/forum?id=Sy2fzU9gl [4] Bowman, S. R. et al. (2016). Generating Sentences from a Continuous Space. — https://arxiv.org/abs/1511.06349 [5] van den Oord, A., Vinyals, O., Kavukcuoglu, K. (2017). Neural Discrete Representation Learning (VQ-VAE). — https://arxiv.org/abs/1711.00937 [6] Rombach, R. et al. (2022). High-Resolution Image Synthesis with Latent Diffusion Models. — https://arxiv.org/abs/2112.10752 [7] Ouyang, L. et al. (2022). Training Language Models to Follow Instructions with Human Feedback (InstructGPT). — https://arxiv.org/abs/2203.02155 [8] Rafailov, R. et al. (2023). Direct Preference Optimization: Your Language Model is Secretly a Reward Model. — https://arxiv.org/abs/2305.18290