Variational Autoencoders
Prerequisite Knowledge
This lecture builds on the following concepts from earlier lectures. If any feel unfamiliar, review the linked notes before proceeding.
Previously Covered in This Subject
- The autoencoder architecture: encoder, decoder, code, and reconstruction loss ? covered in Lecture 3
- L1/L2, sparse, and denoising autoencoders ? covered in Lecture 4
- The Kullback-Leibler divergence penalty ? covered in Lecture 4
- Variational autoencoders: a penalty that shapes a distribution ? covered in Lecture 4
- Autoregressive generative models and likelihood thinking ? covered in Lecture 5
- Normalizing flow models ? covered in Lectures 7 and 8
9.1 Autoencoder Recap and the Generative Model Landscape
9.1.1 What a Vanilla Autoencoder Does
Here is the opening puzzle of this lecture: a network trained only to copy its input seems doomed to merely echo the past — so how could anything built that way invent new data? The answer starts with what the copying actually forces the network to learn.
An autoencoder is a network that squeezes data into a smaller code and then rebuilds the data from that code. Think of retelling a two-hour film in one minute: you cannot keep every scene, so you must decide which details carry the story. The autoencoder is forced into the same kind of decision, but it must learn the decision rule by itself. Say the original data lives in dimensions — counts the number of descriptive numbers per item, such as pixel intensities or measured features. The network transforms each item into a reduced representation of dimension , where is much smaller than . Training shapes this transformation so that, starting from the -dimensional code, you can recreate data close to the original in dimensions. The closeness is measured over the entire training set, not just one point — a model that copies one item perfectly while failing on the rest has not learned anything useful.
Two parts do the work:
- The encoder maps the input to a latent code. The word latent means hidden: the code appears nowhere in the raw training set. It is a learned representation, a transformation of the original data. If the encoder is a function with parameters , then for input the code is .
- The decoder passes that transformed data back out, aiming to reconstruct the input. Writing the decoder as with parameters , the reconstruction is .
We train the encoder-decoder combination by minimizing a loss function, typically the gap between reconstructed data and original data.
Real-valued data can take any continuous number from minus infinity to plus infinity; nothing restricts it to integers. Pixel intensities, sensor readings, and feature measurements are all like this. For such data the usual choice is the mean squared error between reconstruction and input, averaged over the training set:
Here is the number of training points, is an original data point, and is its reconstruction. The double bars measure the Euclidean norm: square each coordinate's difference, add them up, and take the square root — squaring first makes every mismatch count positively and punishes big errors harder than small ones. In plain words: square the difference between what we rebuild and what we had, then average. We lower this error by changing the parameters of the decoder and the encoder through standard backpropagation.
A tiny numerical pass through the loss. Let , , and suppose the data points and reconstructions are:
| 1 | |||
| 2 | |||
| 3 |
Then . Sense-check: the perfectly reconstructed third point contributes nothing, and the loss lands somewhere between the best case () and the worst single-point error — exactly what an average should do.
One assumption sits quietly inside this choice of loss: mean squared error fits real-valued targets. If the data were categorical — letters, labels, discrete tokens — squared distance would make little sense, and a cross-entropy-style loss would take over. That boundary matters later, when this lecture meets data that refuses to be continuous.
9.1.2 Regularized Variants Already Covered
Earlier material covered several variants built on this skeleton, each adding one extra pressure to training:
- L1/L2-regularized autoencoders add a penalty on the weights themselves, keeping them small so the learned map stays smooth rather than memorizing.
- The sparse autoencoder pushes most code units toward zero so only a few stay active. At any moment the code looks like a nearly empty vector with a handful of live entries — a constraint that often yields interpretable features.
- The denoising autoencoder learns to clean corrupted inputs back into clean ones: feed it a noisy version, ask for the original. Because the corruption changes every time, the network cannot copy mechanically; it must recover structure.
All of these still share one job: map input to a compact code and reconstruct the input from it. And all share one ceiling — none of them can invent new data. Every decoder output is a reply to some input that already exists. Nothing in their training asks them to fill in the space between known items. Closing that gap is the business of generative models.
9.1.3 Three Ways to Generate Data: Autoregressive, Flow, Variational
A variational autoencoder, shortened to VAE, differs from every autoencoder above in capability and features. A VAE can produce data semantically similar to its training set yet different from every training point. It has the ability to generate new data. In that sense it sits alongside two other generative families covered before: autoregressive models and flow models.
Each family makes a different trade-off along the same axes — how fast samples arrive, how honestly they report probabilities, how stable training is, and how large the latent space must be:
| Dimension | Autoregressive | Flow | VAE |
|---|---|---|---|
| Generation mode | One element at a time, each conditioned on what came before | All elements at once, in parallel | One shot, in parallel |
| Likelihood basis | Exact maximization | Exact maximization (change of variables) | Approximate maximization (lower bound) |
| Probability of new data | Exact | Exact | Approximate |
| Training stability | Stable | Stable | Stable |
| Speed of sampling | Slow — sequential chain | Fast | Fast |
| Sample quality | Highest of the three | Often trails autoregressive; progressive variants close much of the gap | Trails autoregressive in original form |
| Latent dimensionality | Not applicable (works directly on data) | Same as the data itself | Much smaller than the data |
Read the table column by column. An autoregressive model generates one element at a time, each new piece conditioned on what came before. Modern text systems work this way: Claude or ChatGPT emit output line by line, even word by word, never in one shot. Their strengths are real — likelihood maximization, exact probabilities for produced data, stable training. Their weakness is speed, because sequential generation is slow: element ten thousand cannot start before element nine thousand nine hundred ninety-nine finishes.
A flow model generates in parallel rather than sequentially, which makes it fast. Two characteristics matter here. First, the quality of flow-model output often trails what autoregressive models reach, though some progressive variants close much of that gap. Second — and this is a structural signature — the latent space of a flow model has the same dimensionality as the data itself. You sample a point from that data-sized latent space and apply an inverse transformation to get the generated item. For a million-pixel image, that means sampling a million-dimensional noise vector: no compression happened anywhere.
The VAE takes a third path. Generation happens at one shot, in parallel; nothing about it is sequential. It too rests on likelihood maximization. It can report the probability of new data, with some approximation. The cost sits in the mathematics. The derivation is crooked and complex, demanding heavy symbol pushing. And the outcome is not exact likelihood maximization but approximate likelihood maximization. In its original form the VAE produces samples below autoregressive quality, yet the samples arrive in parallel. One more family note: variants of hierarchical autoencoders have also been proposed that generate data in parallel while keeping strong visual clarity on images.
When to pick which: if you need the sharpest possible samples and can wait, go autoregressive; if you need fast sampling with exact likelihoods and do not mind a full-sized latent space, go flow-based; if you need fast generation plus a compact, structured code you can analyze and edit, the VAE earns its complexity.
Real-world: ChatGPT and Claude generate text sequentially, one token after another — the autoregressive pattern. Image-oriented generative development draws heavily on two classical techniques, the VAE and the GAN, and much of modern generative AI is rooted in that pairing.
The vanilla autoencoder learns to compress and rebuild but never to create. The three generative families split the same job three ways: autoregressive models buy quality with sequential slowness, flows buy parallel speed with a data-sized latent space, and the VAE buys both speed and a compact code at the price of approximate mathematics. The rest of this lecture pays that mathematical price openly — starting with why the plain autoencoder's latent space cannot support generation at all.
9.2 Loss of Semantics in Standard Latent Spaces
9.2.1 Why an Unstructured Latent Space Fails
Every autoencoding compresses dimensions into , so information loss is unavoidable — that much is arithmetic. The deeper problem is different. In all the standard autoencoders, the geometry of the latent space is unmanaged: training never tells the encoder where to place codes relative to each other.
A useful picture is a library shelved by accession number instead of by subject. Every book sits somewhere, but two neighbors on the shelf can be a cookbook and a calculus text, because nothing forced similar topics together. A reader who wanders one shelf over finds no promise of anything related. The standard autoencoder's latent space is exactly such a library — codes are placed wherever reconstruction pressure happens to drop them, and adjacency carries no meaning.
Take two points that sit close together in the latent space — close meaning a small L2 distance between them, that is, the straight-line length of the gap you compute by squaring each coordinate difference, adding them, and taking the square root. Once you decode those nearby codes, the outputs may not resemble each other at all. That failure is called loss of semantics: the neighborhood structure of the code space tells you nothing reliable about the data space. Two latent points centimeters apart can decode to a photograph of a dog and a photograph of an aeroplane.
Scope: The failure is about the space between stored codes, not about the stored codes themselves. Training does optimize reconstructions at the exact code positions it visits, so decoded training codes look right. What nobody optimized is how unknown points in between decode. Standard autoencoders leave those gaps unmanaged by construction.
A VAE attacks this directly. During training it does extra work that forces the latent space to become regular, structured, and well behaved. Regularity means precisely this: if two points are close in the latent space, their decoded images stay close in the -dimensional data space. Preserve that property and the code space carries meaning — wandering one shelf over always lands you on a related book.
9.2.2 Worked Illustration: Animals and Vehicles in One and Two Dimensions
Consider a small set of object classes: dog, bird, cat, car, and aeroplane. Each class is represented by some feature vector. Let the original dimension be — count the descriptive features of shape, parts, texture, and so on: nine of them, say number of legs, presence of wings, presence of wheels, fur versus metal, engine sound, and the rest.
Compressing nine features down to one. Run a standard autoencoder from to . Information crashes onto a single line. Each class collapses onto a single point on that one axis, and what survives organizes along a single semantic direction, roughly living versus inanimate:
d = 1 axis:
living end <--- car plane | bird cat dog ---> (positions illustrative)
The bird lands near the dog, because both are alive. The car and the aeroplane land together at the other end, because both are machines. Everything else about a dog — fur, legs, bark — is gone. One number simply cannot hold nine features' worth of distinction; it keeps only whichever direction best separates the data.
Raise the code to and more structure returns. Picture one horizontal axis for the ability to fly and one vertical axis for being alive:
vertical axis: alive (top) vs inanimate (bottom)
alive ↑
bird ● │ ● dog/cat
│
───────────┼─────────── → flies (horizontal axis)
│
plane ● │ ● car
↓ inanimate
On the fly dimension the aeroplane and the bird take the same high value, so they align horizontally. Vertically they split: one is not living, the other is living. They fall in different quadrants — first versus fourth. The car and the dog separate the same way: the dog holds the life value, the car does not, and neither flies. Every added dimension lets the code catch more information about the original data. That gradual recovery — one semantic direction at , two at , richer structure as grows toward — is what preserving semantics looks like.
9.2.3 Worked Illustration: Shapes and Colors Off the Training Distribution
Here is the sharpest way to see the failure.
Sampling between stored codes in a shape/color autoencoder. Imagine objects described by two feature groups: features of their shape and features of their color. These features pass through a standard autoencoder. After encoding, four objects occupy four latent points:
| Object | Latent point |
|---|---|
| Blue circle | one position |
| Green object | another |
| Triangle | another |
| Orange triangle | yet another |
Decode those training codes and you get back objects very close to the originals — on average, that reconstruction is what training optimizes.
Now pick a brand-new latent point that sits between the stored ones, a point corresponding to no training object. Decode it. Our intuition makes a specific prediction: if the sampled point falls between the triangle and the square, we want a blend — a shape between triangle and square, a color between their colors, something like a rounded orange polygon. Instead the decoder hands back a random pattern bearing no relation to anything in the training set, an outlier far from every training object.
The interpolation failed because nothing forced the space between training codes to decode sensibly. The decoder was only ever graded at the four stored points; everywhere else its behavior is uncontracted. That is loss of semantics in action — not a bug in someone's implementation, but a structural hole in the objective itself.
9.2.4 What Regularity and Structure Mean
A variational autoencoder enforces regularity and structure in the latent space so this cannot happen. Sample any point from the latent space a trained VAE builds. Decode it. You get something similar to the training set yet completely different from any single training item. The output is never random; it stays along the same line as the training data. Where the standard autoencoder returned junk for the between-points sample, the VAE returns a meaningful blend — a shape leaning triangle-ish, a color between the neighbors'.
Visualize the contrast as two scatter plots of latent codes. In the standard-autoencoder plot, codes cluster into scattered islands separated by wide empty stretches — decode from an empty stretch and anything can come out. In the VAE plot, the islands have swollen and merged into one smooth cloud centered at the origin, and every point inside it decodes to plausible data. The takeaway sentence for both plots: closeness in code space predicts closeness in data space, both for stored codes and for fresh samples.
Pitfalls:
- Treating "small reconstruction error" as evidence of a good latent space. Reconstruction quality says nothing about what happens between codes.
- Assuming nearby codes decode similarly just because the encoder is smooth-looking. Without explicit regularization, nothing enforces it.
- Judging regularity only at training codes. The whole point of generation is decoding points the training set never visited.
Real-world: this distinction decides whether a learned representation supports interpolation-based tools — image morphing, style blending, synthetic-data synthesis all decode off-distribution points, and all silently assume a regular latent space. Recommendation systems that blend user-preference vectors and image editors that slide "age" or "smile" dials are standing on exactly the property this section defined.
Loss of semantics is the failure where nearby latent codes decode to unrelated outputs, because plain autoencoders optimize reconstructions only at visited codes. The VAE's fix — pressuring the latent distribution toward a known prior — is introduced next.
9.3 Core Design of the Variational Autoencoder
9.3.1 Learn the Distribution, Not the Code
The last section ended with a demand: force the latent space to decode sensibly everywhere, not just at visited codes. The VAE meets that demand with one structural change so fundamental that everything else follows from it.
In a standard autoencoder you take an image , produce a latent representation , reconstruct , and drive toward by minimizing the squared gap. The VAE changes what the encoder emits. It does not output the latent representation itself. It outputs the parameters of a probability distribution over the latent space. Instead of producing , the network produces — the distribution of the latent given the input. Concretely, for each input the encoder names one Gaussian: its center and its spread.
Once training gives you this distribution, you can draw a sample from it. Pass that sampled value through the decoding process and you get the reconstruction. The training target stays familiar: the reconstructed input should sit very close to the original input. So the first distinction is exact — the variational encoder hands you distribution parameters rather than latent values. Where the plain encoder answered "the code is here," the variational encoder answers "the code lives somewhere in this neighborhood, centered here, with this much uncertainty."
Why a distribution instead of a point? A point gives the decoder exactly one place to be correct. A distribution spreads probability around the code, so training continuously grades a region of latent space — every sample drawn from the region must reconstruct well. That is the seed of regularity: neighborhoods get optimized, not just points.
9.3.2 The Gaussian Prior and the Two-Part Loss
The second design move preserves semantics. The VAE assumes a prior distribution for the latent variable , and that prior is the normal Gaussian distribution — the bell curve with mean zero and unit variance in every direction. A prior is the distribution we believe follows before seeing any data. During training, the model works so that the learned encoding distribution stays close to this normal Gaussian. Why does that help? Because a code space filled with well-placed Gaussians leaves no wild gaps: sample anywhere reasonable, decode, and land near meaningful data. Every neighborhood of the prior carries some encoder mass, so no decoded sample lands in uncontracted territory.
That sets the shape of the loss. The loss function is designed so that its value drops when two things happen together. One, the reconstruction error becomes small. Two, the latent distribution the encoder produces stays close to the normal Gaussian. Equivalently, the loss penalizes any deviation of the encoding distribution away from the normal Gaussian, alongside the usual reconstruction penalty. The regularization applied in a VAE is exactly a functional tied to the deviation of the latent distribution from that fixed prior.
Written pedagogically, the quantity to minimize combines both parts:
The first term sums squared reconstruction gaps over the training set — the same quantity as from Section 9.1 without the factor. The second term is the Kullback-Leibler divergence, introduced properly in Section 9.4. It measures the gap between the encoder distribution and the assumed prior , taken to be the zero-mean normal Gaussian. Minimizing the whole thing says two things at once: reconstructions sit close to originals, and the encoded latent distribution resembles the prior.
This written form is the teaching version. Actual VAE training replaces the mean-square term with a maximum-likelihood formulation — instead of minimizing squared error directly, you maximize the likelihood of the reconstruction. The two views agree in spirit: push reconstructions toward inputs while pulling the code distribution toward the Gaussian. Section 9.5 derives the likelihood form honestly, and the derivation shows why the squared-error view survives as an approximation.
9.3.3 Encoder Outputs and Their Shapes
Look inside the encoding process. Original data enters, and the encoder emits the parameters of the latent distribution, denoted and . Dimension bookkeeping matters. If the input dimension is capital , the latent dimension is small , far smaller. Concretely:
- is a -dimensional vector — the mean of the latent Gaussian, the center of the cloud of plausible codes for this input.
- is a covariance matrix for that Gaussian — entry records how latent dimensions and vary together.
- is treated as diagonal in nature; off-diagonal entries drop out, so each latent dimension keeps its own variance and none co-vary. A diagonal matrix means the Gaussian is axis-aligned: stretching along one latent direction never drags another along.
- The prior against which everything is compared is the standard normal, whose covariance is the identity: ones along the diagonal, zeros everywhere else.
Regularization pushes the learned values toward zero and the variances toward the identity pattern, making the whole latent cloud look like the standard Gaussian.
9.3.4 Sampling a Latent Value
How does a concrete latent value come into existence? Draw a random vector and combine it with the learned parameters:
Here is a random vector whose entries are independent draws, and collects the square roots of the diagonal variances of — each entry scales the noise on its own latent dimension. The in-class description put the components of between zero and one; the standard formulation draws , a standard normal noise vector, matching the reference text's reparameterization relation . Both tell the same story — add randomness scaled to the learned spread onto the learned center — and from here on denotes a standard-normal draw.
Every call to the random number generator returns a fresh , and multiplying it with the covariance-scale factor and adding the mean yields a new sample . Feed to the decoder and out comes , a data item numerically different from every training point yet semantically close to them. In strict matrix terms the same idea reads , with shapes matched so a covariance multiplies a -dimensional noise vector.
Sampling by hand with real numbers. Let the latent dimension be . Suppose the encoder, looking at some car photo, emitted and a diagonal , so .
Draw one standard-normal sample, say . Then:
so . Sense-check: the sample sits within about one standard deviation of the mean in both coordinates — close to the center but not exactly at it, which is what a Gaussian sample should look like. Draw again and you would get a different ; decode each and each becomes a distinct output.
9.3.5 Worked Example: Generating a New Car Design
Train a VAE on a large collection of car images. Training ends. Now call the random number generator; suppose it returns the vector we labeled above. Form the sample as scaled by and shifted by — exactly the arithmetic of the previous subsection, giving . Push this through the decoder. The output is an image of a car — maybe — that matches no single car in the training set. It is a new design of car: perhaps the wheelbase of one training car fused with the roofline of another, because those traits live along nearby latent directions. Repeat the call and you harvest more designs, each pass independent. That loop is how the VAE delivers its generative capability. It also explains why the VAE and the GAN became the two dominant classical engines of image generation. Fresh techniques keep arriving on top of that foundation.
Visualize what the trained latent space looks like as a heat map over the plane: brightness marks where the mixture of per-input Gaussians concentrates. After training, the bright region forms one smooth blob centered at the origin — the KL pressure has squeezed all per-input clouds into overlapping position. Any point you pick inside the blob lies within reach of some encoder distribution, so any decode stays meaningful.
Pitfalls:
- Reading as if were fixed. It must be redrawn for every sample; freezing it collapses the variety of generations.
- Forgetting the shapes: adding a matrix directly to a -vector is a type error — scale the noise by the variance terms (or multiply by the covariance matrix) before adding.
- Expecting the sampled output to copy a specific training image. By construction it matches none of them; similarity to the set, not equality with a member, is the contract.
Real-world: this sampling loop is production machinery. Car studios prototype concept designs by sweeping latent samples around a reference vehicle's ; game asset pipelines generate texture variations the same way; drug-discovery tools sample molecular latents to propose candidate structures no chemist has synthesized.
The VAE's two design moves: learn distribution parameters (, diagonal ) instead of codes, and pull every encoding distribution toward the fixed standard-Gaussian prior through a KL penalty. Sampling then runs with fresh noise per draw. What remains unspecified is the measuring stick inside that KL term — entropy and divergence supply it next.
9.4 Entropy and KL Divergence
9.4.1 Entropy Refresher
The VAE loss leans on one question, asked straight from the floor: how do you quantify the difference between two probability distributions? The VAE wants the latent distribution to mimic a zero-mean normal Gaussian. That assumption keeps the latent space regular and structured, so similar codes decode to similar data. To demand similarity, we must measure dissimilarity. The tool for that is the KL divergence, and the KL divergence grows out of entropy, introduced in the machine learning course.
Entropy measures indefiniteness — how unpredictable a random variable is. A good everyday anchor: guess-the-card. If I hold a deck where one specific card always appears (deterministic), you never have to guess. If any of fifty-two cards is equally likely, guessing is maximally hard. Entropy puts a number on that hardness.
For a discrete random variable with probability at each value :
The sum runs over every value the variable can take. Each term multiplies a probability by the logarithm of itself — so rare events contribute little weight, and impossible events contribute none at all, since their probability is zero. This quantity is the entropy: a measure of how unpredictable the variable is. Change the logarithm base and you change the unit: with log base 2, entropy is measured in bits, the same currency as computer storage.
9.4.2 Entropy at Its Extremes: Worked Computations
Deterministic system. Let the variable take exactly one value: probability 1 there, probability 0 everywhere else. Nothing is indefinite — you know the value all the time. At the certain value the term contributes ; everywhere else is 0. Either is 0 or is 0, so every term vanishes and the entropy equals 0. A deterministic system carries no disturbance and no surprise.
Completely random system. Entropy peaks when probability spreads uniformly over the outcomes. The classic case is a fair coin toss: heads with probability 0.5, tails with probability 0.5. Plug into the formula with log base 2:
All values equally likely, entropy equal to 1 bit — the maximum for this setting. As supporting background: spreading probability uniformly over equally likely outcomes gives bits, so a fair eight-sided die carries 3 bits; the binary coin toss is the special case . So the boundaries are settled: minimum value 0 for a deterministic system, maximum value 1 bit for a completely random binary system.
Q: What is the maximum value of the entropy? A: One. The minimum value is 0 for a deterministic system. The maximum is 1 for a completely random system — think of the fifty-fifty coin toss computed above.
9.4.3 Why the Minus Sign
Q: Can you say why this minus value is there? What is the significance of the minus sign in the entropy formula? What does it ensure? A: It is partly convention, but it guarantees something useful. The probability always lies between zero and one. The logarithm of any number between zero and one is a negative quantity, and multiplying by the positive keeps it negative. So the bare summation part of the formula would come out negative every time. Prefacing it with the minus sign flips the result. Entropy is then always a positive quantity: between zero and one in the binary case, zero for the deterministic system, one for the fully random one.
The takeaway from that exchange, restated once without the symbols: the flip exists because whenever — it converts an always-negative sum into an always-positive uncertainty score, so a bigger number really means more unpredictability.
9.4.4 From Entropy Difference to KL Divergence
Why climb from entropy to divergence? Because entropy measures the information content carried by one distribution, and comparing two entropies compares two information contents. If something is deterministic, its entropy is 0 and it carries no information; if something is completely random, it carries maximal information. Given two distributions and , each owns an entropy. The gap between their information contents can be written through expressions of the form , which mix the probabilities of one distribution with the logarithms of the other. From this core idea of information theory, the KL divergence emerges as the working variant.
The KL divergence — called KLD or KL divergence for short — of one distribution relative to another is assembled from exactly those pieces:
Read the middle expression first: it subtracts what already costs from what it would cost to describe outcomes drawn from while pretending they come from . Combining the two sums over a common denominator gives the rightmost ratio form. In words: weight the logarithm of the probability ratio by the probability under , then add up those weighted log-ratios over every value of . This matches the reference definition exactly — in the continuous case, with the sum replacing the integral for discrete variables.
The reverse direction swaps the roles:
Now the weighting distribution flips along with the ratio — a different weighted average, and so a different number.
One distribution pair, two different divergences. Let (the fair coin again) and let . Using log base 2:
Direction :
Direction :
Sense-check: both are positive, neither is zero since the distributions differ, and — the point of the exercise — the two directions give different numbers, , proving asymmetry with nothing but arithmetic.
Check the special cases straight from the formula. If for every value of we have , the ratio is 1, its logarithm is 0, and the KL divergence equals 0. If the distributions are completely disjoint — some value has but — the ratio blows up and the divergence takes a very large value, infinite in the limiting sense.
9.4.5 Properties: Non-Negative, Asymmetric, Not a Metric
Three properties decide how KL divergence may and may not be used.
First, it is always non-negative. The reference text proves it cleanly: since for every positive , substituting gives
Identical distributions achieve exactly 0; different distributions give strictly larger values. That monotone behavior is what a similarity score needs.
Second, it is not symmetric. Compare the two displayed equations above: interchanging and changes which distribution weights the logarithm, so the numerical result changes — the worked example just measured 0.74 versus 0.53 bits. Symbolically, .
Third, symmetry fails, so KL divergence violates the standard definition of distance in a metric space. In a metric space, the distance between and must equal the distance between and . It is a divergence, a directed measure of how one distribution deviates from another — not a distance in the geometric sense. Keep this straight whenever an exam question asks whether KL is a metric: it is not.
Scope: KL divergence answers "how badly does distribution cover distribution ?" — a directed question. It cannot serve wherever an undirected distance is required (clustering coordinates, nearest-neighbor search in distribution space). For symmetric alternatives, texts use the Jensen-Shannon divergence, the average of both KL directions toward the midpoint distribution.
9.4.6 The KL Term as the VAE Regularizer
Now place the tool inside the VAE. The quantity to control is the encoder's latent distribution, written . Here is the input, is the latent, and collects the parameters of the autoencoder's encoding side. The target is the normal Gaussian prior. The regularizing functional is:
Training chooses and learns the parameters so this divergence sinks toward zero. Driving it to zero keeps the latent space close to the normal Gaussian. Sample from that space and decode, and you get meaningful, semantically valid new data. That single term is the entire mechanism by which the VAE regularizes its code space.
Q: What is the way you compare two probability distributions? A: Use the KL divergence — and that answer, offered from the class itself, is exactly right. The Kullback-Leibler divergence quantifies how far one probability distribution sits from another, which is why it becomes the regularizing functional written above.
9.4.7 What the Regularizer Does to the Latent Space
Picture a two-dimensional latent space trained without the KL pressure. The training codes huddle in scattered islands, and much of the plane is empty. Sample a point in one of those empty stretches, far from every island, decode it, and junk comes out. Pick a point inside an island and decoding reproduces something close to genuine training data — but recreating training data is not the goal. The goal is data similar to the training set yet distinct from it.
With the KL regularization forcing the latent distribution toward the normal Gaussian, the islands swell and merge into smooth coverage. On the scatter-plot picture from Section 9.2: the empty stretches fill in until one connected cloud spans the region around the origin. Now interpolation works. Train a VAE on many frog images and many car images — sports cars, sedans, hatchbacks, frogs from varied environments. Sample a latent point between the frog region and the car region, then decode it. Nearer the frog side, you get a frog that looks like a car. Nearer the car side, you get a car that looks like a frog. No other framework in this course offers that interpolation capability. The same story plays out with simple shapes. Suppose one blob holds triangles of various sizes, and neighboring blobs hold circles and squares. Take a sample between the three and decode it. You get a triangle-like figure whose shape and color differ from all of them — yet it is a meaningful shape rather than a random one. Semantics survives the interpolation. Without regularity, two codes close together in latent space can decode to wildly different outputs; with regularity, decoded neighbors stay neighbors.
Real-world: language-model distillation pipelines literally minimize KL divergence between a big teacher model's output distribution and a smaller student's; policy-gradient methods in reinforcement learning constrain each update by its KL distance from the previous policy. The same directed measure you derived from a coin toss prices those systems' central trade-offs.
Entropy scores the unpredictability of one distribution ( deterministic, bit for a fair binary coin); KL divergence extends it into a directed, non-negative, asymmetric gap between two distributions — and that gap, measured from the encoder's Gaussian to , is the entire regularizing engine of the VAE.
9.5 The Training Mathematics: Intractability and the ELBO
9.5.1 Formalizing the Decoder
Set up the notation cleanly. Recall the pipeline. Original data flows through the encoder, which yields either latent codes or, in the VAE, distribution parameters and . A sample is drawn. The decoder converts into a reconstruction . Training drives toward in the average sense.
Probabilistically, the decoder defines a conditional distribution:
Here is the sampled latent value and — written with a star when we mean the true underlying parameters — collects the parameters of the decoder. The decoder learns the probability distribution of the reconstructed variable given the sampled latent. Concretely, for image data this conditional says: given this code, how likely is each possible output image? A common choice treats the output pixels as real values centered on the decoder's prediction, so high likelihood means the decoder confidently predicts what it renders.
Training then seeks the decoder parameters that maximize the likelihood of the training data under this model, . That sentence hides the entire difficulty of VAE training, and unpacking it fills the rest of this section.
9.5.2 Why Exact Likelihood Maximization Is Intractable
Write the marginal likelihood using the chain rule of probability — every data point can be produced from some latent value, so total likelihood sums (or integrates) over all ways production could have happened:
and in discrete form:
The first factor is no problem: the prior is the assumed normal Gaussian, fully known, evaluated at any by plugging into the bell-curve formula. The second factor is computable too — pass any single through the decoder and read off its output density. The killer is quantity. Evaluating this sum demands passing every possible latent value through the decoder to collect its contribution. Too many values exist. With even a modest 200-dimensional continuous latent space there are uncountably many candidates; no loop enumerates them all. Direct maximization becomes intractable — not slow, but impossible to finish.
Bayes' rule offers a rearrangement, and it fails the same way. The posterior density — given the data, the probability of the latent — is:
The posterior describes the density of a variable that depends on the original training data: which latent values could have been responsible for this image? The denominator contains again — the same intractable marginal, now sitting in the one slot Bayes' rule requires you to fill. Rewrite however you like; the posterior stays intractable. Both doors out of the room are locked by the same key.
9.5.3 The Encoder Approximation
The escape is a second network. In addition to the decoder model , define an encoding distribution that approximates the posterior:
This approximation network makes approximate maximization of the data likelihood possible, and the whole objective becomes optimizable with backpropagation. Summarizing the division of labor:
- Given , the encoder learns the mean vector of the latent distribution and its covariance matrix.
- The decoder — called the generator, because it generates the data — maps sampled latents to data.
- The encoder — called the inference network or recognition network, because it recognizes data vectors and infers their latents — maps data to distribution parameters.
- Both parameter sets, for decoding and for inference, are learned jointly.
Why an approximation can still train well: when matches the true posterior closely, quantities built from it behave almost like the exact ones. When it does not match yet, the objective itself contains a term measuring that mismatch (revealed below), so training automatically improves the approximation while improving the model. Approximation error is not ignored — it is priced into the loss.
9.5.4 Dimension Bookkeeping: A Concrete Example
Fix numbers so the shapes stop being abstract. Let the input be a 32 by 32 image (so ) and choose a latent dimension of . Then:
- is a 200-dimensional vector.
- is a 200 by 200 covariance matrix.
- The noise draw is a 200-dimensional vector, since is the 200 by 200 identity whose diagonal entries are 1 and off-diagonal entries 0.
- The sampled latent is . To be really correct about matrix-vector dimensions, the covariance multiplies the noise vector, producing another column, which adds elementwise to the mean.
Shape check on every product: , then . Nothing ever leaves column form.
The sampled passes through the decoder, which produces the distribution over decoder outputs; drawing from that distribution yields the actual reconstruction. Training adjusts and together through backpropagation while the loss shrinks.
9.5.5 Deriving the Evidence Lower Bound Step by Step
Training maximizes the log-likelihood, averaged over the training set. Take one training point and follow every algebraic step. Two warnings before the symbols fly: first, this derivation is heavy symbol pushing — the payoff is not a new quantity but the discovery that VAE training maximizes a bound, never the likelihood itself. Second, nothing below approximates anything; every line is exact until Step 5, where an inequality is introduced deliberately.
Step 1 — wrap the target in an expectation. The log-likelihood does not depend on the latent, so averaging it over any latent distribution changes nothing:
The expectation of a constant equals the constant — that is the entire content of this line. The latent values are drawn from the encoder distribution ; in practice each sampled arises by bringing in a random vector. Why bother writing a constant as an average? Because the next steps need the averaging machinery inside the brackets.
Step 2 — apply Bayes rule inside the logarithm. Expand the joint as conditional times prior, then divide by the posterior:
Nothing has changed yet — pure symbol pushing, exactly as announced. The fraction is just rewritten via Bayes' rule, since and .
Step 3 — multiply by one. Insert the factor , which equals 1, and regroup:
Multiplying by 1 cannot change anything mathematically, but it changes everything structurally: the encoder distribution now appears twice, once in each fraction, preparing the split.
Step 4 — split the logarithm into three named pieces. A logarithm of a product splits into a sum of logarithms: . Apply that, then read each piece against the KL definition from Section 9.4 — weighting log-ratios by probabilities under a distribution. The numerator group is the decoder transformation. The ratio forms one divergence (negated). The leftover ratio forms another:
Walk the sign bookkeeping once slowly. The split gives . Each expectation of a log-ratio under its own numerator's distribution is exactly a KL divergence, so the middle piece enters as and the last as .
The first term rewards reconstructions the decoder assigns high likelihood. The second is a KL divergence pulling the encoder distribution toward the prior. The third is a KL divergence between the encoder distribution and the true posterior — and evaluating it directly is exactly the intractable problem identified above.
Step 5 — drop the impossible term using its sign. The last KL divergence is greater than or equal to 0 always — proven in Section 9.4 from the inequality . So deleting it can only lower the right-hand side, leaving a lower bound on the log-likelihood:
This bound is the variational lower bound, better known as the ELBO — the evidence lower bound, sometimes heard aloud as the elbow function. Since the discarded KL term is always non-negative, maximizing the ELBO pushes up a guaranteed floor under the log-likelihood. Maximization acts on both pieces at once. Raise the reconstruction term — equivalently, shrink the reconstruction error between original and generated data. And shrink the KL divergence, which pins the learned encoder distribution close to the normal Gaussian. That double duty is precisely the informal loss of Section 9.3, now derived rather than asserted. Exact likelihood maximization is out of reach; maximizing the likelihood lower bound is what VAE training actually does.
One more insight hides in the discarded term: the gap equals exactly the intractable KL divergence between the approximation and the true posterior. Better encoders squeeze the gap shut; the bound gets tight precisely when becomes the real posterior. So tracking the ELBO tracks both the model and the quality of its own approximation.
Scope: The ELBO is a floor, not the target. Training raises the floor without ever measuring the ceiling (). If someone reports "the VAE achieved likelihood ," they almost always report the ELBO or an estimate of it. And the bound holds per data point: the full objective averages over all .
Why dropping a non-negative term lowers a bound — with numbers. Suppose for some : reconstruction term , prior-matching KL , and the intractable posterior KL happens to be . Then the exact identity reads . Dropping the last term claims only — true (), but loose by exactly 1.1, the size of what was thrown away. Sense-check: a claim of "" survives contact with the truth precisely because the discarded quantity was positive; had it been negative, the "bound" could have been false.
9.5.6 Why Sampling Is Needed for Backpropagation
Why insist on working with sampled latent values instead of the raw distribution parameters? Because optimization runs on gradients. Eventually you must differentiate the loss with respect to every parameter in and every parameter in , then adjust:
where is the learning rate — how big each correction step is. For the gradients to flow from the decoder back through to the encoder, a concrete value must traverse the whole path. The sampled latent provides that bridge: is nothing but plus scaled . Writing it that way relocates the randomness to the network's input edge — the noise enters as data would, and the path from and to the loss runs entirely through ordinary deterministic operations a derivative can climb. This relocation is known as the reparameterization trick. As backpropagation nudges the parameters toward the local minimum, the values and the values update along with everything else. Freeze the distribution parameters out of the forward path — say, by emitting a bare sampled number with no visible , ancestry — and the encoder receives no learning signal at all.
9.5.7 Mini-Batch Training and Generation After Training
Training follows the standard rhythm:
- Split the training set into mini-batches.
- Pass each batch through the encoder-decoder combination: encode to ; draw ; sample ; decode; score the reconstruction plus KL terms.
- Accumulate the loss over that batch and adjust and once.
- Repeat batch after batch until training stabilizes.
Each pass costs one encoder evaluation, one random draw, one decoder evaluation, and their backward sweeps per item — linear in batch size, with the KL term closed-form cheap because both distributions are Gaussians. The practical limit is the usual one: memory for activations during backprop.
When training is over, throw away the encoder. Generation needs none of it. Keep the decoder together with the learned machinery of mean vectors and covariances. Call a random number generator to produce , and build multiple values of from it. Pass them to the decoder and harvest new data. Each draw is independent, and all draws run in parallel. The asymmetry is worth pausing on: training needed two networks and data; deployment needs one network and dice.
Exam note: expect the ELBO derivation as a step-by-step question — expectation wrap, Bayes expansion, multiply-by-one insertion, three-term split, then dropping the non-negative posterior KL to leave the bound. Know why the dropped term may be discarded (it is always ), why sampling enables gradient flow to and , and the update rules , .
Real-world: anomaly detection systems score new inputs by their ELBO — a low bound means the model cannot reconstruct the item well, flagging it as unlike anything seen; semi-supervised pipelines reuse the inference network's codes as features for downstream classifiers, extracting double duty from the trained encoder before discarding it.
9.6 Generating Data with a Trained VAE
9.6.1 Sweeping Two Latent Dimensions: Worked Demo
Everything before this point built the machine; now watch it run.
A concrete run: sweeping two latent dimensions on CIFAR-10. Train the encoder-decoder combination on the CIFAR-10 dataset, displayed as negative images — background flipped to white, strokes rendered dark, like a photographic negative. After training, identify two latent dimensions and call them and .
Build the sample grid step by step:
- Fix and let take 20 evenly spaced values between zero and one.
- Fix and vary over 20 values.
- Combine the two sweeps into a 20 by 20 grid of paired values — 400 latent samples in total.
- Create these random signals in the encoding space and pass all of them through the decoder.
Out comes a grid of images — 400 decodes arranged so that horizontal position encodes and vertical position encodes .
Read the grid like a topographic map, where each step to a neighboring cell is one small latent step. At the center sits something resembling a real tree-like figure. Move along one axis and the image morphs smoothly toward a 9 on one side and an 8 on the other. Move along the other axis and it drifts toward 5, then 0, then something like 1. No cell contains visual garbage; no jump between neighbors is abrupt. Every cell is a fresh image, semantically akin to the training world yet matching no training item exactly.
Sense-check: 400 independent draws produced zero junk cells and smooth neighbor-to-neighbor transitions — exactly what a regular latent space predicts and an unmanaged one would violate. The smooth morphing across cells is the structured latent space made visible — Section 9.2's promise, delivered as pixels.
What the smoothness certifies: if even one latent direction crossed from meaningful content to noise, the grid would show a torn seam somewhere. Its absence across 400 samples is empirical evidence that the KL pressure really did remove the unmanaged gaps that plagued standard autoencoders.
9.6.2 Student Question: Using Generated Data for Machine Learning
Q: Can you think of one application where this generated data is going to be useful from a machine learning standpoint itself? Can this generated data be useful for machine learning work? A: Synthetic data generation — when we have less input data than we want and need more for training. Exactly right. In the deep neural networks course, data augmentation appeared as a mechanism against overfitting: scale images down or up, translate them, rotate them. On top of those geometric tricks, VAE-style generation augments the training set with genuinely new samples, and a supervised algorithm trained on the enlarged set performs better. The point reaches beyond marketing content, brochure images, or question answering. From a core machine learning standpoint, generated data strengthens supervised model development. And the entire pipeline is unsupervised. Starting from the original training data alone, the model learns the data distribution through the latent space. It samples from that latent space and decodes the samples into new data. No labels intervene anywhere.
The distinction deserves emphasis: classic augmentation rearranges existing examples — the same photo shifted two pixels left. VAE synthesis produces items that never existed, drawn from the learned distribution. Both fight overfitting; only the second expands what the model can plausibly see.
Scope: synthetic augmentation inherits whatever blind spots the training data had. If the VAE never saw rare classes, its new samples will not contain them either — generation multiplies coverage of the learned distribution; it does not discover outside it.
9.6.3 Choosing the Prior Distribution
The prior deserves one honest caveat. The normal Gaussian was assumed partly for convenience. With a Gaussian prior, computing the KL divergence stays simple, and so does updating the parameters of both encoder and decoder to reduce it — Gaussians come with closed-form distances between them. More complicated priors are legal — a mixture of Gaussians, for instance, several blobs instead of one. Practice has shown, though, that switching priors barely moves the quality of reconstruction or of new data. That observation is why the normal Gaussian remains the standard prior choice today: simplicity bought at zero measured cost is a good deal.
Real-world: attribute-controlled sampling turns face models into production tooling for recognition systems, and the same generated-data logic strengthens supervised training wherever usable data is scarce — medical imaging stands out, where collecting patient scans is slow and expensive but every extra plausible training case improves diagnostic models.
Generation after training is a two-ingredient recipe — random draws plus the decoder — and sweeping latent coordinates systematically turns those draws into a controllable map of the data distribution. The grid demo's smooth morphs are the visible proof of the structured space this lecture spent six sections building.
9.7 Applications, Strengths, and Limitations
9.7.1 Faces: Attribute Dimensions in the Latent Space
Train an input-decoder combination on mugshots of people. Once training finishes, pick out two latent dimensions and sweep samples across them. The decoder renders different versions of the same face. In one sweep a person who looked serious acquires a smile. That latent dimension is capturing smilingness — the training set contained other people smiling too, and the latent organization absorbed the concept.
Different regions of the latent space capture different high-level concepts, and which concepts appear depends on what the training images contain. Observed examples include male versus female faces, skin tone, smilingness, eyeglasses, beard, age, and head pose. Head pose means looking straight at the camera, turning toward a profile, or tilting the head back. Build a rich enough latent space from varied training images and you can sample along any of these abstract axes to create variations of a base image. Nobody labeled smile during training; the concept emerged because it helped reconstruction across many photos.
9.7.2 Face Recognition Robustness
Why does attribute variation matter operationally? Consider a recognition system for one individual. The database holds a single serious-looking photo taken around age 30. Twenty years later, investigators compare the person's current face against that stored face. Matching can fail outright — hairline moved, glasses added, expression hardened. It works only if the system was trained not just on the available facial data but on deliberate variations of it: aged renderings, altered poses, varied backgrounds.
A VAE manufactures exactly those variations. This is data augmentation done in a far smarter way than rescaling or rotating pixels. Bring in abstract latent dimensions — age, smilingness, skin tone, eyeglasses, beard — and synthesize changes along them. Feeding such augmented data to supervised training improves accuracy and prepares the recognizer for practical deployment.
The decades-later match, step by step. Take the single 30-year-old mugshot, encode it once to get its latent point . Now synthesize a small bank around it:
| Latent edit | Synthesized variant |
|---|---|
| same face, aged twenty years | |
| same face, turned toward profile | |
| same face, eyeglasses added | |
| same face, outdoors lighting |
Train or calibrate the recognizer on this bank instead of one photo. When today's face arrives, at least one synthesized variant sits near it in appearance space, so matching succeeds. Sense-check: each row changes exactly one attribute family while identity-carrying features stay anchored at — which is only possible because those axes exist separately in latent space.
9.7.3 Strengths and Weaknesses Summary
On the credit side, the VAE is a principled approach to generative modeling: a maximum-likelihood-based method. It permits inference of , so given data you can evaluate how likely it is under the model and judge whether generated output is plausible. It is good for feature representation — the feature size is far smaller than the original data, whereas a flow model's latent space matches the data dimension exactly.
On the debit side, training maximizes the lower bound of the likelihood, not the actual likelihood, because the actual likelihood is intractable. Samples from the original formulation come out blurrier than GAN output — GANs are covered next. They also trail sharp autoregressive generators such as PixelCNN or PixelSnail. Against those cons stands one more pro worth weighing: training is stable, with no jumping of the loss function up and down.
| Property | VAE | GAN |
|---|---|---|
| Training objective | Maximize ELBO (likelihood-based) | Adversarial min-max game |
| Reported probability for data | Yes, approximately via the bound | No natural density |
| Sample sharpness | Softer, blurrier | Sharper |
| Training stability | Stable | Notoriously touchy |
| Encoder / inference available | Yes | Not by default |
When to pick which: need calibrated probabilities or a reusable encoder — VAE territory; need maximally crisp samples and can absorb unstable training — GAN territory.
9.7.4 Latent Space Arithmetic
The structured latent space supports calculation, not just sampling — a practice known as latent space arithmetic. You can interpolate between a male voice and a female voice. Subtract the features of a piggy face from one image, then add the body of a pig to a cat image. The result lands on an intermediate concept between pig and cat. Add smiliness to a face. Add age to a person's portrait. Add eyeglasses. In the vector space of latents, concept directions behave like arithmetic operands, and editing data reduces to adding and subtracting along them.
Pitfalls:
- Expecting perfect orthogonality between concept directions. In plain VAEs the axes correlate — raising smile shifts apparent age too — which is exactly the flaw β-VAE targets next.
- Applying arithmetic far outside the trained region of latent space; edits work within the cloud the KL pressure organized, not anywhere in .
Real-world: photo-editing suites expose "aging" and "smile" sliders powered by latent-direction arithmetic; voice-cloning products interpolate speaker latents between reference voices to hit target pitch and timbre.
A trained VAE hands you editable concept dials for free: sweep them to synthesize attribute variants that harden recognition systems against real-world drift, combine them arithmetically to land between concepts, and weigh the whole framework as stable-but-blurry next to the GAN's sharp-but-touchy trade.
9.8 Beta-VAE: Disentangled Representations
9.8.1 Motivation: Entangled Attributes
Practitioners noticed a flaw in plain VAE latents. The abstract dimensions — age, smile, skin tone — are not completely orthogonal. Increase the smile and the person's apparent age shifts too. Age the face and the skin tone drifts with it. The attributes are entangled: correlated directions rather than independent controls.
Picture an old mixing desk whose faders are linked by gears: push the treble up and the bass creeps along with it. You asked for one change and got two. A well-built desk has independent faders — each slider moves exactly its own channel. Plain VAE latents are the geared desk; editing applications need the independent one. Changing age should leave skin tone untouched; changing skin tone should leave gender appearance untouched. Achieving independent latent modification requires disentangled dimensions, and a variant built for exactly that purpose is the beta VAE, written β-VAE.
9.8.2 The Beta-Weighted Loss
The modification to the loss function is tiny. Multiply the KL divergence term by a coefficient :
Every symbol is inherited from Section 9.5: the expectation averages decoder log-likelihood under encoder samples, the divergence measures distance to the standard-normal prior , and only the new dial is added. With this is the standard VAE objective. Raise and the optimizer, chasing the enlarged penalty, drives the KL divergence even smaller.
Take , for example. Minimizing the weighted objective then forces the divergence between the learned distribution and the normal Gaussian to become much smaller. The latent distribution gets pressed hard against the isotropic prior, in which are completely uncorrelated. The learned covariance flattens toward diagonal — and a purely diagonal covariance means every dimension is disentangled from every other. Nothing in this argument needs the exact value 100; it only needs large enough that structuring the latent space becomes worth more to the optimizer than pixel-perfect reconstruction.
Scope: the trade-off is real, not free. Heavy KL pressure squeezes the latent codes toward the prior so hard that reconstructions lose fine detail — raise and sharpness drops even as independence improves. Choosing means choosing where on that seesaw your application sits.
9.8.3 Capacity Constraint Interpretation and Results
The weighting is also known as the capacity constraint: larger beta spends more of the model's capacity on structuring the latent space and less on pixel-perfect reconstruction. The name reads directly off the formula — the KL term caps how much information each code may carry about its input, and sets how tightly the cap clamps.
The payoff shows in controlled edits:
- Skin color changes while expression, pose, and apparent age hold steady.
- On chairs, leg style, width, and viewing azimuth can each move independently; viewing azimuth is how the object sits oriented toward the viewer.
- New chair variants emerge feature by feature rather than all at once.
Editing proceeds latent-feature by latent-feature instead of collectively, which is the entire design goal of disentanglement. The geared mixing desk gets its sliders separated: one fader per attribute, no cross-talk.
β-VAE keeps the standard ELBO but multiplies the KL term by ; values above one buy disentangled, independently editable latent axes at some cost in reconstruction sharpness. When an application needs single-attribute control — face aging without skin-tone drift, chair-leg swaps without camera moves — the extra pressure pays for itself.
9.9 VQ-VAE: Vector-Quantized Variational Autoencoder
9.9.1 Motivation: Discrete Latent Spaces
The original VAE assumes a continuous latent distribution — the normal Gaussian is a creature of continuous random variables, able to take any value on the line or plane. Many practical domains refuse to be continuous. When the meaningful content is a finite menu of categories, formulating generation in discrete space fits better and can render new data more faithfully. The fix is a variant called VQ-VAE, where VQ stands for vector quantized.
9.9.2 Where Discrete Codes Fit: Images, Language, Audio
Take an image containing a background and an object. Describe it discretely: the shape of the object, its color, its orientation, the kind of background, the texture of that background, the lighting. None of these is a continuous dial. Color might admit perhaps 20 possible values and no more. Texture outdoors might span 5 or 10 types. A fixed menu of possibilities covers the scene.
The same reasoning extends beyond images — the argument holds for one-dimensional data too:
- Language: tokens are categorical by nature; the vocabulary is a finite list.
- Audio: the descriptors are discrete — which language is being spoken, male or female voice, the speaker's age bracket, the topic of discussion.
Finite, countable, categorical. For data best represented in discrete fashion, VQ-VAE is the formulated tool.
9.9.3 Codebook Quantization Mechanism
Replace the continuous latent space with a discrete embedding space holding discrete latent variables, written through . Each is itself a real-valued vector — say 200-dimensional — but only these fixed vectors may serve as codes; infinitely many values are not permitted.
The mechanism runs in three moves:
- Encode input into a continuous latent — a 200-dimensional vector in our running example.
- Compare against all codebook vectors and record the index minimizing the distance:
The notation means: among all indices from 1 to , return the one whose codebook vector sits closest to the encoder output under the Euclidean norm.
- During decoding, pass the selected fixed embedding — not the raw — through the decoder, then compute reconstruction losses as usual.
So a given latent value maps to a sequence of indices. What the encoder produces is thereby restricted to a small set of possibilities called the codes, and the collection of embeddings is the codebook.
A quantization trace with real numbers. Shrink the running example to , , with codebook entries , , . Suppose the encoder outputs . Compute squared distances:
Smallest wins: index , so travels onward to the decoder and itself never does. Sense-check: sits closest to the origin entry both by eye and by arithmetic — quantization snaps the continuous output onto its nearest legal code. In an actual lecture run with the full codebook, the computed vector sat closest to the 53rd embedding, so exactly embedding number 53 traveled onward to the decoder.
9.9.4 The Three-Part Loss and Stop-Gradient
VQ-VAE training maximizes the log-likelihood of the generated data given the quantized latent . The subscript records that the value passed downstream is the selected codebook entry, not the encoder's raw output. On top of the likelihood term sit two more loss components, and they pull in opposite directions:
- Codebook alignment: move each embedding closer to the encoder outputs . Otherwise encoder outputs would be jerked around by arbitrary random code vectors — the codebook starts random, and left alone it would steer generation badly.
- Commitment: move the encoder outputs closer to their assigned embeddings. The process is two-way — codebooks approach encodings, and encodings commit to codebooks.
Assembled as one objective to minimize:
This matches the canonical published form of the VQ-VAE objective exactly: the alignment term carries the stop-gradient on the encoder output so it updates only the codebook, and the commitment term carries it on the embedding so it updates only the encoder. Here is the stop-gradient operator — during the forward pass it passes its argument through unchanged, but during backpropagation it blocks gradients from flowing through, letting exactly one side of each squared term receive learning signal. In the alignment term the gradient reaches but not ; in the commitment term it reaches but not . The full treatment of training VQ-VAE, including this operator, opens the next session.
Pitfall: reading the two squared terms as redundant. They are not — removing the alignment term strands a randomly initialized codebook that nothing ever corrects; removing the commitment term lets encoder outputs wander arbitrarily far from every code, making each quantization snap violent and unstable.
9.9.5 Resources and Next Steps
Two recommended VQ-VAE blogs were flagged as particularly good reading, with links traveling with the shared materials — going through them is strongly advised before continuing. The next session spends 15 to 20 minutes finishing VQ-VAE training, including the stop-gradient mechanics introduced above. It then introduces VQ-VAE-2, which stacks multiple layers of encoders and multiple layers of decoders; that hierarchy renders data at quality as high as competing methods or higher. After that begins the GAN — the generative adversarial network — scheduled as a multi-session deep treatment.
Exam note: read the two recommended VQ-VAE blogs before the next session. Know the three-step mechanism (encode continuously, snap to nearest codebook index, decode the snapped code), the argmin selection rule, and the three-part loss's division of labor — reconstruction through the quantized code, alignment updating the codebook, commitment updating the encoder — with the stop-gradient deciding who learns in each term.
Exam Guidance Summary
- Exam relevance flag: the VAE is the autoencoder type explicitly designated for examinations, spanning the mid-semester and end-term scope. Treat this topic as core testable material, not enrichment.
- Expect conceptual questions on the design distinctions. Those cover why the latent distribution is learned instead of latent codes (distribution parameters grade a neighborhood of codes rather than a single point), why the Gaussian prior regularizes the space (it merges isolated code islands into smooth coverage), and what loss of semantics means (nearby codes decoding to unrelated outputs). Use the animal/vehicle and shape/color illustrations as reference examples.
- Expect mathematical questions on the entropy formula and its extremes: 0 for deterministic systems, 1 bit for the uniform coin toss. Also expect the significance of the minus sign ( makes each term negative; flipping keeps entropy positive), the KL divergence definition , and its properties: always non-negative, asymmetric — so not a metric distance. Then comes the step-by-step ELBO derivation, including why the final KL term can be dropped: it is always non-negative, so removing it leaves a valid lower bound.
- Know the sampling mechanics cold: with correct matrix dimensions — a covariance multiplying a noise vector adding to a mean. Know why sampling enables gradient flow to the encoder (the reparameterization relocates randomness to the input edge), and know the update rules , .
- Study advice: work through the offline reference list shared for this topic. Read the two recommended VQ-VAE blogs before the next session, since VQ-VAE training continues next time and the stop-gradient discussion builds on them.
- Sequencing signal: the next topic, GANs, receives a five-to-six-hour allocation. Budget preparation effort accordingly, and expect the remaining VQ-VAE and VQ-VAE-2 material to be examined only after it is completed.
Exam note: highest-weight targets in order: (1) the five-step ELBO derivation with the reason each step is legal, (2) sampling mechanics with shapes, (3) entropy/KL properties including asymmetry, (4) the conceptual design distinctions backed by the two illustrations.
Key Industry Applications
- Conversational AI systems: ChatGPT and Claude exemplify autoregressive, token-by-token generation — the slow-but-stable paradigm the VAE's parallel one-shot generation contrasts with. Every chat reply you wait for is the sequential-generation cost paid in production.
- Synthetic training data: VAE-generated samples augment scarce datasets for supervised learning, extending classic augmentation (scaling, translation, rotation) with new synthesized instances; the whole pipeline is unsupervised. Medical imaging and industrial defect inspection lean on this where collecting real examples is slow or costly.
- Face recognition robustness: attribute-controlled synthesis — age progression, pose, smile, eyeglasses, beard, skin tone, background — hardens recognizers against real-world variation. One example: matching a decades-old mugshot to a current face.
- Content creation and marketing: generated imagery supports brochures, creative assets, and question-answering media pipelines.
- Image generation stack: VAEs and GANs form the classical backbone of image generative AI. Autoregressive image models such as PixelCNN and PixelSnail set the sharpness benchmark that early VAEs chased. VQ-VAE targets discrete, categorical structure in images, language, and audio — the same discrete-token logic that modern codec-style generators exploit. VQ-VAE-2's hierarchical encoders and decoders push render quality higher.
The VAE's industry footprint concentrates wherever one of its three signature properties pays rent directly: compact latents for compression and feature reuse, a structured space for attribute-controlled editing and augmentation, and a tractable likelihood proxy for scoring inputs.
UDL Lecture 9 notes · Variational Autoencoders
Sections Breakdown
Recaps encoder-decoder training and its regularized variants, then compares autoregressive, flow, and variational approaches to generating data.
Shows why unmanaged latent spaces decode nearby codes to unrelated outputs, using animal-versus-vehicle and shape-and-color illustrations.
Explains learning distribution parameters instead of codes, the Gaussian prior, the two-part loss, and sampling latents.
Builds entropy from coin-toss extremes up to the KL divergence formula, its non-negativity and asymmetry, and its role as the VAE regularizer.
Derives the evidence lower bound step by step, shows why exact likelihood is intractable, and how reparameterized sampling enables backpropagation.
Walks a grid sweep over two latent dimensions, synthetic-data augmentation, and why the Gaussian prior remains the standard choice.
Covers attribute dimensions in face latents, recognition robustness, the VAE-versus-GAN comparison, and latent space arithmetic.
Introduces the beta-weighted objective that buys independent, editable attribute axes at some cost in reconstruction sharpness.
Explains discrete codebook quantization, nearest-neighbor index selection, and the three-part loss with stop-gradients.
Exam Revision Notes
Below is the distilled, exam-ready core. Every entry comes from the full explanation above. Use this section for rapid review; return to the main notes when a point needs more context.
Autoencoder Recap and the Generative Model Landscape
Must-know: A vanilla autoencoder compresses D dimensions to d and minimizes averaged squared reconstruction error; it cannot generate new data. The VAE joins autoregressive and flow models as a generative family with parallel one-shot generation and approximate likelihood maximization.
?? Top pitfall: Assuming all autoencoder variants (sparse, denoising, L1/L2) can generate data — none of them can; generation is unique to the VAE step.
Self-check: Which generative family has a latent space with the same dimensionality as the data itself? (Flow models.)
Connects to: 9.2, 9.3
Loss of Semantics in Standard Latent Spaces
Must-know: Loss of semantics: two latent points with small L2 distance can decode to outputs far apart in data space, because standard autoencoders optimize reconstruction only at visited codes. Regularity means nearby codes decode to nearby data, including for fresh samples between training codes.
?? Top pitfall: Judging a latent space by reconstruction quality at training codes alone — the failure appears at unvisited points between codes, exactly where generation samples.
Self-check: In the d=2 animal/vehicle layout, which two classes align horizontally and why? (Aeroplane and bird — both score high on the fly axis; they split vertically on the alive axis.)
Connects to: 9.1, 9.3
Core Design of the Variational Autoencoder
Must-know: VAE encoder emits distribution parameters, not codes: mu_X (d-vector) and Sigma_X (diagonal d x d). Loss = reconstruction sum + KL(q_phi(z|x) || p(z)) toward the standard normal prior; sampling is z = mu + eps*sigma with eps ~ N(0, I). The MSE+KL form is the teaching version; real training maximizes likelihood.
?? Top pitfall: Freezing the noise vector across samples, or adding the d x d covariance to a d-vector without scaling — both break generation or shapes.
Self-check: Why does learning a distribution over codes (rather than one code per input) seed latent regularity? (Training grades a region — every sample from the neighborhood must reconstruct well.)
Connects to: 9.2, 9.4, 9.5
Entropy and KL Divergence
Must-know: Entropy H = -sum p log p: 0 for deterministic systems, 1 bit for the uniform binary case; the minus sign flips an always-negative sum so entropy is positive. KL divergence D_KL(P||Q) = sum P log(P/Q): always >= 0, asymmetric (D_KL(Q||P) differs), hence not a metric distance. The VAE regularizer is D_KL(q_phi(z|x) || N(0,I)) driven toward zero.
?? Top pitfall: Treating KL divergence as a symmetric distance — swapping the arguments changes which distribution weights the log-ratio and gives a different number (worked example: 0.74 vs 0.53 bits).
Self-check: Why does entropy never come out negative? (log p <= 0 for 0 < p <= 1, so each term p*log p <= 0; the leading minus sign makes the total non-negative.)
Connects to: 9.3, 9.5
The Training Mathematics: Intractability and the ELBO
Must-know: The ELBO derivation: wrap log p(x) in an expectation over q, apply Bayes rule, multiply by q/q = 1, split into reconstruction term minus prior KL plus posterior KL; drop the last term because it is always >= 0, leaving log p(x) >= ELBO(x_i). Training maximizes this bound, not the likelihood itself.
?? Top pitfall: Forgetting why sampling is needed: without a concrete z = mu + Sigma*eps traversing the network, gradients cannot flow from the decoder back into the encoder parameters.
Self-check: Why can the third KL term be dropped from the exact identity? (It is a KL divergence, hence always non-negative; removing it can only lower the right side, yielding a valid lower bound.)
Connects to: 9.3, 9.4, 9.6
Generating Data with a Trained VAE
Must-know: Generation needs only the decoder plus random draws; a 20x20 sweep over two latent dimensions (400 samples) shows smooth morphing between semantically related outputs, evidencing the structured latent space. Generated data augments scarce training sets for supervised tasks; the whole pipeline is unsupervised.
?? Top pitfall: Assuming VAE synthesis discovers content outside its training distribution — it multiplies coverage of what it learned; rare unseen classes stay absent from generated samples.
Self-check: Why does the smooth morphing across the grid cells certify latent regularity? (Any tear into noise would expose an unmanaged gap; its absence across 400 samples shows every neighborhood decodes meaningfully.)
Connects to: 9.5, 9.7
Applications, Strengths, and Limitations
Must-know: VAE pros: principled maximum-likelihood method, permits q(z|x) inference, compact features (smaller latent than data dimension, unlike flows), stable training. Cons: maximizes only the lower bound, samples blurrier than GAN/PixelCNN/PixelSnail output. Latent directions capture attributes enabling arithmetic edits like adding smile or age.
?? Top pitfall: Assuming concept axes are independent in plain VAEs — smile, age, and skin tone arrive entangled, motivating beta-VAE next.
Self-check: Why does one stored mugshot fail recognition decades later, and how does a VAE fix it? (Appearance drifts; the VAE synthesizes aged/posed/glassed variants around the stored latent so training covers the drift.)
Connects to: 9.6, 9.8
Beta-VAE: Disentangled Representations
Must-know: Beta-VAE loss: ELBO with KL term weighted by beta. Beta = 1 recovers standard VAE; large beta (e.g., 100) forces the latent distribution toward the isotropic prior, flattening the covariance toward diagonal — the capacity constraint that yields disentangled attributes.
?? Top pitfall: Forgetting the trade-off: raising beta improves disentanglement but degrades reconstruction sharpness because codes are squeezed toward the prior.
Self-check: Why does a diagonal covariance mean disentanglement? (Diagonal entries mean each latent dimension varies independently — no co-variation between attribute axes.)
Connects to: 9.5, 9.7, 9.9
VQ-VAE: Vector-Quantized Variational Autoencoder
Must-know: Quantization selects i = argmin_j ||z_e(x) - e_j|| and passes only e_i to the decoder (lecture run ended at index 53). Loss = -log p(x|z_q) + ||sg[z_e] - e||^2 + beta_commit ||z_e - sg[e]||^2: alignment updates the codebook, commitment updates the encoder; sg blocks gradients through its argument.
?? Top pitfall: Treating the two squared loss terms as redundant — alignment fixes a randomly initialized codebook, commitment keeps encoder outputs from wandering away from codes.
Self-check: In the three-part VQ-VAE loss, which term updates the codebook vectors and why does sg matter there? (The alignment term; sg on z_e stops the gradient reaching the encoder so only e moves.)
Connects to: 9.8, 9.5
Exam Guidance Summary
Must-know: VAE is core examinable material: conceptual design distinctions, entropy extremes (0 and 1 bit), KL properties (non-negative, asymmetric, not a metric), full ELBO derivation with the drop-the-KL justification, and z = mu + Sigma*eps with matrix dimensions.
?? Top pitfall: Underestimating the ELBO derivation weight — it is explicitly flagged as expected step-by-step exam content.
Self-check: Which autoencoder type is designated for examinations? (The VAE, spanning mid-semester and end-term scope.)
Connects to: 9.3, 9.4, 9.5, 9.9
Key Industry Applications
Must-know: Industry applications map to VAE properties: compact latents (compression/features), structured latent space (attribute editing, augmentation), approximate likelihood (scoring inputs).
Self-check: Which two classical techniques form the backbone of image generative AI? (VAE and GAN.)
Connects to: 9.1, 9.6, 9.7, 9.9
Was this lecture useful?
BitsNotes AI Assistant
Subject Notes AssistantConfigure AI Chat
Choose how to access the chatbotSigned in as
Powered by BitsNotes — 20 messages per day. No API key needed. Want unlimited access? Use "Bring Your Own Key" mode.
Sign in to use AI Chat
Get 20 free AI messages per day to ask questions about your lecture notes. Sign in with Google or GitHub — it takes 5 seconds.
Sign In to BitsNotesSwitch to "Bring Your Own Key" tab above for unlimited access with any OpenAI-compatible provider.