Maps to: UC Berkeley CS294-158 Deep Unsupervised Learning (Spring 2024), Lecture 3 · Thu 2/1/2024 · Likelihood Models: Flow Models, second part (slides 42–66): change of many variables, composition, affine and elementwise flows, NICE / RealNVP coupling layers, the RealNVP architecture and its partitions, the choice of coupling transform (affine, mixture-of-logistics CDF, piecewise polynomials), “architecture matters” (Flow++), Glow (invertible 1×1 convolutions, interpolation, attribute control), continuous-time flows (FFJORD), flows on discrete data and dequantization, future directions. Also the variational-dequantization slides of Lecture 4 (slides 102–104). Basis: The slides give the constructions and show pictures of trained flows. These notes derive why coupling layers are invertible with triangular Jacobians, why uniform dequantization is a bound and how variational noise tightens it, and why continuous flows need only a trace. The experiments put every family on data with exact answers: 2-D densities with exact log-densities, Lecture 2’s discrete worlds with exact entropies, and binary ShapeWorld with its 17.80-bit floor — where flows can finally be compared, bit for bit, with the autoregressive models of Lectures 3–4. Code:
code/lecture06/(5 scripts +coupling.py; PyTorch (CPU); 10 s – 100 s each, except the image flows of script 04: ~15 minutes on the first run, then cached) Reading time: 4 hours including running the code
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- State the change-of-variables formula in many dimensions, explain as a volume ratio, and explain why a general determinant () is unaffordable inside a training loop.
- Explain why affine flows are Gaussians and elementwise flows model only independent coordinates, and what composing them with linear mixing buys.
- Build an additive (NICE) and an affine (RealNVP) coupling layer, prove that it is invertible with a triangular Jacobian, and explain why its networks need no restrictions at all.
- Explain the RealNVP image architecture (checkerboard and channel masks, squeezing, multi-scale), why a bad partition hurts, and what Glow adds (ActNorm, invertible 1×1 convolutions) and Flow++ adds (mixture-CDF couplings, attention, variational dequantization).
- Derive the instantaneous change of variables of continuous-time flows and use Hutchinson’s trace estimator.
- Explain why a continuous density on discrete data can reach infinite likelihood, prove that uniform dequantization gives a lower bound on the discrete log-likelihood, and explain how variational dequantization tightens it.
- Read a flow’s bits per image as an upper bound and compare it honestly with exact autoregressive bits.
0.2 Prerequisites and scope
| Assumed | Introduced here | Developed later |
|---|---|---|
| Lecture 5 (change of variables in 1-D/2-D, AF / IAF, triangular Jacobians) | coupling layers, squeeze, ActNorm, 1×1 convolutions, multi-scale flows | VAEs with flow posteriors / IAF-VAE (Lecture 8) |
| Lecture 2 (discrete worlds with exact entropies, the density-at-center trap) | dequantization (uniform, variational), the bound it gives | diffusion models as continuous-time models (Lectures 11–12) |
| Lectures 3–4 (exact bits on ShapeWorld: 22.3 / 18.8) | continuous normalizing flows, Hutchinson’s estimator | flows as compressors, bits-back with dequantization (Lecture 21) |
0.3 Map
§1 change of many variables: volumes, determinants and their cost ← code 01
§2 affine and elementwise flows ← code 01
§3 coupling layers: NICE, RealNVP, Flow++-style transforms; depth ← code 02
§4 flows for images: partitions, squeeze, Glow; flows on ShapeWorld ← code 04
§5 continuous-time flows ← code 05
§6 discrete data: degeneracy, uniform and variational dequantization ← code 03, 04
§7 where flows stand
§8 summary §9 pitfalls §10 exercises §11 further reading §12 next lecture
0.4 Notation
| Symbol | Meaning |
|---|---|
| , | data and latent, same dimension; invertible and differentiable |
| the Jacobian; “logdet” | |
| a coupling mask: 1 = kept (“conditioning”) variables, 0 = transformed | |
| log-scale and shift networks of a coupling layer | |
| a dequantized data point: integer plus noise | |
| the dequantization noise distribution (uniform, or learned) | |
| bits per image | of a probability of a binary ShapeWorld image (an upper bound for flows) |
0.5 How the experiments are run
2-D experiments reuse Lecture 5’s exact densities (two moons: truth 1.0245 nats on the test points; face 1.5640). The dequantization experiment uses homework 1 Q1’s two discrete worlds from Lecture 2 (exact entropies 3.454 and 5.293 bits). The image experiment uses Lecture 1’s 10,000 / 2,000 binary ShapeWorld images: every flow models after RealNVP’s logit preprocessing, and reports — an upper bound on its discrete code length, comparable with the floor (17.80 bits), Lecture 3’s gated PixelCNN (22.28) and Lecture 4’s Transformer (18.79). The four image flows train for 3,000 steps of 64 images (~3–4 minutes each on a CPU) and are cached.
1. Change of many variables
1.1 Volumes
In 1-D, a flow stretches an interval into an interval and probability is conserved. In dimensions, the sampling map turns a small cube into a small parallelepiped — to first order a linear map, given by the Jacobian. A linear map multiplies volumes by the absolute value of its determinant, so
The slides’ intuition: is likely if it maps to a “large” region in space — a small neighbourhood of that the flow blows up into a big region of likely ‘s. Script 01 checks both halves on a random 3-D flow and a hand-written 2-D “banana” flow (, ):
| cube side | vol() / vol() | ||
|---|---|---|---|
| (+1.54, −0.29, −2.18) | 0.3 | 2.0477 | 2.1322 |
| 0.1 | 2.0953 | 2.1322 | |
| 0.01 | 2.1340 | 2.1322 | |
| (+0.57, −1.08, −1.40) | 0.3 / 0.1 / 0.01 | 2.4787 / 2.5254 / 2.5512 | 2.5603 |
As the cube shrinks, the volume ratio converges to . And for the banana, a histogram of samples (boxes of ) matches at six random points within 2–5% (sampling noise), while alone is off by up to 60%.
1.2 The new key requirement
Training by maximum likelihood,
needs the log-determinant for every training example at every step, and its gradient. A general determinant costs (an LU decomposition). Script 01, Part 2:
| general matrix: slogdet | triangular: | |
|---|---|---|
| 128 | ~0.1 ms | ~0.03 ms |
| 512 | ~0.9 ms | ~0.01 ms |
| 2048 | ~8.6 ms | ~0.03 ms |
For a RGB image () a full determinant is ~ operations per example — and that is before computing the Jacobian itself ( backward passes). Hence the slides’ new key requirement: the Jacobian determinant must be easy to calculate and differentiate. Every practical flow is built from layers whose Jacobian is diagonal or triangular (determinant = product of the diagonal), or whose determinant has another cheap structure (LU-factored convolutions, §4.3).
1.3 Composition
Flows compose, , and by the chain rule : log-determinants add. Composition is “an easy way to increase expressiveness” (the slides) and the reason every architecture below is a stack of simple layers.
2. Affine and elementwise flows
2.1 Affine flows are Gaussians
The affine flow , i.e. sampling with , is another name for the multivariate Gaussian . Its Jacobian is , so the log-likelihood involves — expensive for large unless is kept triangular (a Cholesky factor). It is the baseline every flow must beat:
| data | full-covariance Gaussian | truth |
|---|---|---|
| two moons (nats per point) | 1.9083 | 1.0245 |
| ShapeWorld, dequantized (bits per image, upper bound) | 205.7 | 17.80 |
On ShapeWorld (script 01, Part 4), a uniform density on — knowing nothing — scores exactly 288.0 bits; a diagonal Gaussian is worse than that (294.9 bits: one smooth bump per pixel fits the two-box shape of a dequantized binary pixel, mass on or , worse than a flat density does), and the full covariance reaches 205.7 — roughly Lecture 1’s independent Bernoulli pixels (212.1). Covariance captures “neighbouring pixels are both ink”, not shapes.
2.2 Elementwise flows
An elementwise flow transforms each coordinate by its own 1-D flow, : lots of freedom (any monotone map, Lecture 5), a diagonal Jacobian (cheap), but no interaction between coordinates — alone it can only model . Interleaving elementwise flows with invertible linear maps lets each new elementwise layer act along new directions (an idea known as iterative Gaussianization). Script 01, Part 3 on two moons:
| flow | test NLL | − truth |
|---|---|---|
| Gaussian (affine flow) | 1.9083 | +0.884 |
| elementwise only | 1.7849 | +0.760 |
| elementwise → linear → elementwise | 1.4939 | +0.469 |
| (elementwise → linear) × 4 → elementwise | 1.3981 | +0.374 |
| (Lecture 5) AR flow, | 1.043–1.047 | +0.02 |
Composition helps, but slowly: an elementwise map cannot make the transform of depend on the value of , which is exactly what bending two moons requires. Coupling layers provide that dependence.
3. Coupling layers
3.1 The construction
NICE (Dinh et al., 2014) and RealNVP (Dinh et al., 2017) split the variables in half, , keep one half, and transform the other with an elementwise affine map whose parameters are computed from the kept half:
Invertible, because the kept half passes unchanged and can recompute and :
Tractable Jacobian, because it is triangular:
The lower-left block — however complicated — never enters the determinant. Hence the slides’ takeaway: coupling layers allow unrestricted neural nets to be used in flows, while preserving invertibility and tractability. Think of them as data-parameterized elementwise flows. Both directions cost one pass: no sequential steps, unlike Lecture 5’s AF/IAF. One layer leaves half the variables untouched, so layers alternate which half is kept (in 2-D: keep , then keep , …). The code implements the split with a binary mask : (coupling.Coupling).
The additive coupling of NICE sets : volume-preserving, . NICE therefore ends with a diagonal scaling layer, the only place where volume can change.
3.2 The choice of coupling transform
“A Bayes net defines coupling dependency, but what invertible transformation to use is a design question” (the slides). Options:
- affine (NICE, RealNVP, IAF-VAE, …) — closed-form inverse, the most common;
- more complex, nonlinear monotone maps — “better performance”: CDFs / inverse CDFs of mixtures of Gaussians or logistics (Flow++), piecewise linear / quadratic functions (neural importance sampling), rational-quadratic splines (neural spline flows).
MixtureCDFCoupling2D implements a Flow++-style transform for 2-D: with an 8-component logistic-mixture CDF. Its inverse needs bisection (Lecture 5).
3.3 Experiments: depth is the price
Script 02 trains each flow for 6,000 Adam steps on the 2-D densities:
Two moons (truth 1.0245):
| flow | params | test NLL | − truth | samples in 99% region |
|---|---|---|---|---|
| NICE: additive × 8 + scaling | 36,900 | 1.1102 | +0.086 | 95.1% |
| RealNVP: affine × 8 | 36,896 | 1.0622 | +0.038 | 98.9% |
| Glow-style: (ActNorm, learned mixing, affine) × 8 | 37,008 | 1.0645 | +0.040 | 98.7% |
| Flow++-style: mixture-CDF × 4 | 23,912 | 1.0866 | +0.062 | 98.3% |
| (Lecture 5) AR flow, | 8,248 | 1.0427–1.0469 | +0.02 | ~98% |
Face (truth 1.5640):
| flow | params | test NLL | − truth | samples in 99% region |
|---|---|---|---|---|
| RealNVP: affine × 4 | 18,448 | 2.0667 | +0.503 | 81.8% |
| RealNVP: affine × 8 | 36,896 | 1.7896 | +0.226 | 92.2% |
| RealNVP: affine × 16 | 73,792 | 1.6933 | +0.129 | 95.2% |
| Flow++-style: mixture-CDF × 8 | 47,824 | 1.7259 | +0.162 | 94.7% |
| (Lecture 5) AR flow, | 8,248 | 1.5876–1.5906 | +0.02 | ~98% |

Exact densities and coupling flows (02_coupling_layers_2d.py). Note the thin tears and folds: a smooth bijection from a Gaussian must stretch and cut its way to holes and rings.
- Additive couplings are weakest (their own Jacobian is 1; all stretching comes from the final scaling).
- Depth is the price of one-pass inversion. On the face, affine couplings improve from 4 → 8 → 16 layers (+0.50 → +0.23 → +0.13 nats) and are still far behind Lecture 5’s two-step AR flow. Half the variables pass each layer unchanged, and an affine map only shifts and stretches the other half; carving a ring with two eyes out of a Gaussian takes many such cuts.
- A more flexible transform helps per layer: 8 mixture-CDF couplings beat 8 affine ones (+0.16 vs +0.23).
- In 2-D, an AR flow is a coupling flow — two layers (an elementwise CDF of ; a CDF of given ) with the most flexible 1-D maps possible. Coupling layers do not win in 2-D; they win in many dimensions, where an AR flow needs sequential steps for one direction and a coupling flow a fixed number of layers for both.
Speed both ways (Part 3, 5,000 points of the face; timings vary between runs): an 8-layer RealNVP evaluates densities in ~3.5 ms and inverts in ~3 ms (closed form; round trip exact to ); the 8-layer mixture-CDF flow needs ~10 ms forward but ~480 ms to invert (bisection). Flexibility of the transform and speed of the inverse pull in opposite directions.
4. Flows for images
4.1 The RealNVP architecture and its partitions
For a image the slides list RealNVP’s multi-scale design: three checkerboard couplings, a channel squeeze, three channel couplings; split off half the variables as fine-grained latents (); repeat on the rest (: ); finish with latents for the highest-level details (: ).
- A checkerboard mask keeps the pixels where row + column is even, so every transformed pixel is predicted from its four direct neighbours.
- A squeeze reshapes each block into 4 channels: . It is a permutation (log-det 0), and after it a channel mask splits each block — pixels again conditioned on neighbours, now at a coarser scale.
- Factoring out half the variables after each scale saves compute, and gives a hierarchy of latents.
Why partitions matter: the slides contrast a good partition (checkerboard × 4; squeeze; channel × 3; unsqueeze; checkerboard × 3) with a bad one (mask top half; bottom half; left half; right half, × 2) and show the bad one’s samples are worse. A transformed pixel can only be explained by the kept ones; with half-image masks, most pixels never see their own neighbours.
4.2 Glow and Flow++
Glow (Kingma & Dhariwal, 2018) replaces RealNVP’s fixed alternation by a learned step: ActNorm (a per-channel affine normalization initialized from the first batch so that activations have zero mean and unit variance, then trained like any parameter — unlike batch norm it is a fixed invertible map per example), an invertible 1×1 convolution (a learned channel mixing, a generalization of “reverse the channels”), then an affine coupling. With the LU parameterization , : instead of (times positions). Glow trained at large scale and showed smooth interpolations between faces in latent space and attribute control (add the latent direction “smiling” to a face).
Flow++ (Ho et al., 2019) showed that “NN architecture also matters”: mixture-of-logistics CDF couplings + self-attention in the conditioner networks + variational dequantization (§6). The slides’ conclusion: the Bayes net (coupling dependency), the transformation class and the network architecture all play a role in a flow’s performance. (Reported results, CIFAR-10 test bits per dimension: RealNVP 3.49, Glow 3.35, Flow++ 3.08 — versus PixelCNN++ 2.92 for an autoregressive model.)
4.3 Flows on ShapeWorld
Script 04 trains four flows on binary ShapeWorld, all with the same budget: logit preprocessing, one squeeze to , then 10 affine couplings with identical conditioner CNNs (32 channels, 3 layers); they differ only in the partition or the step:
| model | params | test bits per image (2,000 images) |
|---|---|---|
| RealNVP partitions (checkerboard + channel, alternating) | 130,320 | 132.9 |
| bad partition (top / bottom / left / right halves) | 130,320 | 154.5 |
| Glow steps (ActNorm + 1×1 conv + coupling) | 130,760 | 123.2 |
| Glow steps + variational dequantization | 130,760 | 94.7 |
| (script 01) full-covariance Gaussian | – | 205.7 |
| (Lecture 3) gated PixelCNN, exact | – | 22.28 |
| (Lecture 4) Transformer on 2×2 patches, exact | – | 18.79 |
| exact entropy | – | 17.80 |
(training curves, test bits at steps 600 / 1200 / 1800 / 2400 / 3000: RealNVP 152 / 142 / 137 / 134 / 133; bad partition 169 / 161 / 158 / 155 / 155; Glow 146 / 135 / 128 / 125 / 124; Glow + VD 114 / 104 / 100 / 97 / 96.)
The numbers deliver the slides’ caveat — “performance not as good as other models” — in its starkest form:
- Flows are 5× further from the floor than our worst autoregressive model. A smooth bijection must turn the 921,600 sharp-edged unit cubes of dequantized ShapeWorld (one per possible image, each with a flat density inside) into one Gaussian blob. Binary data are the hardest case for flows; on 8-bit photos, where neighbouring intensities vary smoothly, the reported gap is 0.15–0.6 bits per dimension (the CIFAR-10 numbers of §4.2).
- The partition matters: the bad partition costs 22 bits, with the same layers and parameters.
- Learned mixing helps: Glow’s steps beat fixed alternation by 10 bits.
- Variational dequantization is the largest single gain: 28 bits with the same flow (§6).
(Plan vs reality: a first prototype kept the first couplings at full resolution; it reached ~125 bits after 1,500 steps but cost 0.15 s per step. All four flows now work at after one squeeze — fair, and ~3× cheaper.)
Samples, temperature, interpolation. No sample of any of the four flows — at temperature , or (Glow’s trick of sampling ) — is a valid ShapeWorld image; the nearest valid image is 24–53 pixel flips away (Lecture 3’s gated PixelCNN: 38% valid). Lowering the temperature does not help here: near , the Glow flow decodes to ink-filled blobs (89% ink at , against 26% in real images). Interpolating linearly between the latents of two test images (Glow’s interpolation slides) reproduces both end points exactly but passes through broken, overlapping shapes: 2 of 8 frames valid — the two ends.

Samples at temperature 0.7 of the four flows (04_flows_on_shapeworld.py).
5. Continuous-time flows
5.1 From many small steps to an ODE
Take a residual flow layer with small . Its Jacobian is , and . Chaining infinitely many infinitesimal steps gives an ODE and the instantaneous change of variables (Chen et al., 2018):
Three consequences, which are the slides’ “allows for unrestricted architectures; invertibility and fast log-probability computation guaranteed”:
- Any network works: an ODE with a Lipschitz velocity field is invertible by running it backwards.
- Only the trace is needed, not the determinant.
- FFJORD (Grathwohl et al., 2018) estimates the trace without forming the Jacobian: for any probe with , and is one vector-Jacobian product — one backward pass, whatever .
5.2 Experiments
Script 05, Part 1 (a random MLP , 2,000 probes):
| exact trace | Rademacher probe: mean ± std | Gaussian probe: mean ± std | |
|---|---|---|---|
| 2 | −0.0807 | −0.0810 ± 0.015 | −0.0784 ± 0.294 |
| 16 | +0.1809 | +0.1852 ± 0.901 | +0.1695 ± 1.016 |
| 128 | +0.0356 | +0.0462 ± 2.964 | +0.0923 ± 2.918 |
The estimator is unbiased; its noise grows with . Rademacher (±1) probes remove the diagonal’s contribution to the variance (Exercise 10): far less noise when the diagonal dominates (), about the same when off-diagonal terms dominate.
Part 2 trains a small CNF (velocity MLP with 4,546 parameters, RK4 with 8 steps, 2,500 Adam steps) on two moons:
| model | params | test NLL | − truth | samples in 99% region | train time |
|---|---|---|---|---|---|
| CNF, exact trace | 4,546 | 1.0707 | +0.046 | 96.7% | ~35 s |
| CNF, Hutchinson trace | 4,546 | 1.0727 | +0.048 | 96.4% | ~25 s |
| RealNVP: affine × 8 | 36,896 | 1.0622 | +0.038 | 98.9% | ~9 s |
| AR flow, | 8,248 | 1.0427 | +0.018 | 98.4% | ~3 s |

The ODE carries the data (left) to a Gaussian (third panel); samples run it backwards (05_continuous_flows.py).
A CNF with an eighth of RealNVP’s parameters is nearly as good; training with the noisy trace costs 0.002 nats. Part 3 shows what “guaranteed” means:
| RK4 steps | test NLL (2,000 points) | max round-trip error | network calls per direction |
|---|---|---|---|
| 2 | 1.0493 | 2.1e-01 | 8 |
| 4 | 1.0590 | 2.4e-02 | 16 |
| 8 | 1.0609 | 4.7e-04 | 32 |
| 16 | 1.0611 | 1.9e-05 | 64 |
| 32 | 1.0611 | 6.4e-07 | 128 |
Invertibility and the log-density are exact up to the ODE solver. With 2 steps the round trip is off by 0.2 and the reported NLL even looks 0.012 nats better — not a better model, a number that is no longer an exact log-density. A CNF trades the coupling layer’s architectural constraint for solver cost, in both directions. (Lectures 11–12 meet ODEs again: diffusion models’ probability-flow ODEs and flow matching train the same kind of velocity field without simulating it during training.)
6. Flows on discrete data: dequantization
6.1 The degeneracy
Images are discrete (binary ShapeWorld pixels; 8-bit photos). A continuous density fitted directly to discrete values has an easy way to win: put infinitely tall, infinitely thin spikes on the data values. “What density does a model assign to values between bins like 0.4, 0.42…?” (the slides) — none, and the likelihood goes to infinity. Script 03 (homework 1 Q1’s world 1: values 0..19, bits) shows it two ways:
| logistic bumps on the integers, scale | at the data (“bits”) | |
|---|---|---|
| 1 | +3.725 | 3.7286 |
| 0.1 | +2.122 | 3.4466 |
| 0.01 | −1.200 | 3.4443 |
| 0.001 | −4.522 | 3.4443 |
Every factor of 10 in the scale buys “bits”, while the probability of each unit cell — the real discrete probability — does not change. And maximum likelihood finds the spikes: a 20-component mixture trained on the raw integers reports −0.13, −3.56, −3.12, … “bits” on the test set, below the entropy and below zero, which no discrete model can do. These numbers are meaningless — Lecture 2’s “density at the center” trap, now with a flexible model.
6.2 Uniform dequantization is a bound
The fix (Theis, van den Oord & Bethge, 2016): add noise. Train the continuous density on with . Each discrete value owns the unit cube , and the model’s discrete probability is the mass of that cube:
By Jensen’s inequality ( is concave),
So the dequantized objective is a lower bound on the discrete log-likelihood: the reported bits are an upper bound on a code length, and the model cannot cheat by spiking — a spike would make tiny on most of the cube. The bound is tight exactly when is constant on each cube. (The slides’ semantics: “we want the integral of probability density within a discrete interval to approximate discrete probability mass.”)
6.3 Variational dequantization
Flow++ makes the noise learnable: draw and use importance sampling,
Uniform is the special case . A learned can follow the shape of inside each cube, so the model no longer has to be flat there — the same ELBO structure as the VAE of Lecture 7, with as the latent variable. Script 03, Part 3 (a Beta noise distribution per value, whose family contains the uniform):
| world | mixture | noise | bound (bits) | same model, exact discrete | Jensen gap | bound − truth |
|---|---|---|---|---|---|---|
| 1 ( = 3.454) | uniform | 3.4952 | 3.4703 | 0.0249 | +0.052 | |
| variational | 3.4537 | 3.4465 | 0.0072 | +0.010 | ||
| uniform | 3.4676 | 3.4556 | 0.0120 | +0.024 | ||
| variational | 3.4479 | 3.4449 | 0.0030 | +0.005 | ||
| 2 ( = 5.293) | uniform | 5.3105 | 5.3056 | 0.0049 | +0.008 | |
| variational | 5.3057 | 5.3055 | 0.0002 | +0.003 |
(For a 1-D mixture-CDF flow the exact cell mass is computable, which lets us see the Jensen gap directly.) Variational noise shrinks the gap 3.5–25×, and the bound moves close to the truth. The gain is largest in world 1, where the true mass piles up in value 0 and the density varies inside the cells.

Left: no dequantization — the density spikes on the integers (log scale). Middle: uniform dequantization. Right: learned noise distributions , far from uniform for (03_dequantization.py).
On ShapeWorld (§4.3) variational dequantization is worth 28 bits with the same flow. And the bound itself is loose: script 04, Part 2 importance-samples the noise, , which tightens as grows (the IWAE bound of Lecture 7):
| model | (the usual bound) | ||
|---|---|---|---|
| Glow steps, uniform noise | 124.8 | 110.7 | 105.8 |
| Glow steps, variational noise | 97.4 | 88.1 | 83.8 |
(200 test images.) At least 14–19 bits of the reported numbers are looseness of the bound, not the flow — and even is still a bound. When flows are compared with discrete models, the dequantization bound is part of what is being compared.
7. Where flows stand
The slides close with the ultimate goal — a likelihood-based model with fast sampling, fast inference, fast training, good samples and good compression — and note that flows achieve some of these criteria, but “how exactly do we design and compose flows for great performance? That’s an open question”, with requirements that may pose permanent challenges:
- dimensionality preserving: has as many numbers as — no compression into a small latent (the VAE’s strength, Lectures 7–8);
- invertibility: every layer must be a bijection, which constrains architectures (or, for CNFs, costs solver steps);
- cheap determinant: triangular structure, hence depth.
The scoreboard on ShapeWorld after six lectures (bits per image; floor 17.80):
| model family | best result | sampling | latent |
|---|---|---|---|
| autoregressive Transformer, 2×2 tokens (L4) | 18.79 (exact) | 72 sequential steps | none |
| gated PixelCNN (L3) | 22.28 (exact) | 288 sequential steps | none |
| Glow steps + variational dequantization (L6) | 94.7 (bound) | one pass | 288-D, invertible |
| full-covariance Gaussian (L6) | 205.7 (bound) | one pass | 288-D |
Flows deliver the promised one-pass sampling and an exact latent space, and they remain important as building blocks — posteriors in VAEs (IAF-VAE), dequantizers, compressors (Lecture 21), and the conceptual ancestors of continuous-time diffusion and flow-matching models (Lectures 11–12). As density models of discrete images they are behind.
8. Summary
- In dimensions : a volume ratio. Training needs the log-determinant for every example, so flows use layers with diagonal or triangular Jacobians; log-determinants add under composition.
- Affine flows are Gaussians (two moons: +0.88 nats; ShapeWorld: 205.7 bits). Elementwise flows model independent coordinates; interleaving them with linear mixing helps slowly.
- Coupling layers keep half the variables and transform the other half elementwise with parameters from any network of the kept half: triangular Jacobian, , one-pass inverse. They need depth (16 affine layers on the face are still 0.13 nats off where a 2-step AR flow is 0.02 off); more flexible transforms (mixture CDFs, splines) help per layer but may need numerical inverses.
- For images, partitions (checkerboard, squeeze + channel) matter (a bad partition cost 22 bits); Glow adds ActNorm and LU-parameterized 1×1 convolutions (−10 bits); Flow++ adds mixture-CDF couplings, attention and variational dequantization.
- Continuous flows need only a trace (), estimated with one Hutchinson probe; any network works, exactness holds up to the solver.
- Continuous densities on discrete data can spike to infinite likelihood. Uniform dequantization turns the flow’s likelihood into a lower bound on the discrete one; variational dequantization tightens it (3.5–25× smaller Jensen gaps in 1-D; −28 bits on ShapeWorld).
- On binary ShapeWorld, flows reach 95–155 bits (bounds) where autoregressive models reach 18.8–22.3 exact bits, and no flow sample is valid: one-pass sampling and an exact latent space have not bought sample quality here.
9. Common pitfalls
- Reporting flow likelihoods on discrete data without dequantization. Negative “bits” or values below the entropy are the spike degeneracy, not a good model (§6.1).
- Comparing a dequantized bound with an exact likelihood as if both were exact. The flow’s number is an upper bound; at least 14–19 bits of it were looseness on ShapeWorld (§6.3).
- Forgetting the preprocessing’s log-determinant. Scaling to , the logit transform, and squeezing all change (or, for squeeze, preserve) volume; only the complete map from to gives a density of .
- Couplings that never transform some variables. Masks must alternate (or be mixed by 1×1 convolutions); a variable kept in every layer is modelled by the base distribution alone.
- A bad partition. Masks should let each transformed variable see its neighbours (checkerboard, squeeze + channel).
- ActNorm without data-dependent initialization, or used like batch norm (different statistics per batch): the map must be fixed for each example to be a flow.
- Trusting a CNF’s density with too few solver steps. Check the round trip and the stability of the NLL as steps increase.
- Expecting temperature to fix samples. Reduced temperature helps when the latent’s centre decodes to typical data; here it decoded to ink-filled blobs.
10. Exercises
Exercise 1 (change of many variables). Starting from for any small region , derive . Show that for , .
Answer
by the substitution rule for multiple integrals. Since this equals for every small , the integrands agree. For the composition, (chain rule), and .
Exercise 2 (the coupling layer). For , , write the inverse, the Jacobian, and its log-determinant. Why may and be arbitrary networks, while the map must be elementwise and monotone?
Answer
Inverse: , . Jacobian: , log-det . and are only ever evaluated (on the kept half, which is available in both directions), never inverted, so they can be anything. The map on must be invertible for each fixed with a cheap determinant: elementwise affine with a positive scale is, and so is any elementwise monotone map (mixture CDF, spline).
Exercise 3 (NICE). Show that an additive coupling () has . Why can a stack of additive couplings never fit a narrow Gaussian starting from , and what does NICE add to fix it?
Answer
With the Jacobian is unit lower-triangular: determinant 1, volume preserved. A volume-preserving map from can move and shear mass but never concentrate it: the image of a unit-volume region has unit volume, so the density can never exceed the base’s maximum , while needs a density times higher. NICE ends with a diagonal scaling layer , contributing to the log-det.
Exercise 4 (affine flow MLE). For the flow , , show that the maximum-likelihood is the sample mean and the sample covariance. What does each likelihood evaluation cost if is a general matrix, and if it is lower triangular?
Answer
The model is with ; the Gaussian MLE gives , (any with is optimal: the flow is not identifiable). With general : solving and cost (once per parameter update, then per example). With triangular : back substitution and , .
Exercise 5 (squeeze and 1×1 convolutions). Show that the squeeze operation has . For Glow’s applied at every pixel of an image with channels, show and compare the cost with computing directly.
Answer
Squeeze permutes the entries of the tensor: its Jacobian is a permutation matrix, determinant ±1. For the 1×1 convolution, the full Jacobian is block-diagonal with copies of , so . , (unit triangular), (triangular): , , versus for a direct determinant — and its gradient is trivial.
Exercise 6 (ActNorm vs batch norm). Why is batch normalization (with batch statistics) not a flow layer, while ActNorm is? What is ActNorm’s log-determinant on a input?
Answer
With batch statistics, the output for one example depends on the other examples in the batch: the map from one to its is not a fixed function, so is ill-defined (at test time with running statistics it is a fixed affine map, which is a flow — but then train and test models differ). ActNorm uses parameters (initialized from one batch, then learned) — a fixed per-channel affine map: for .
Exercise 7 (the dequantization bound). Prove and that equality holds iff is constant (a.e.) on the cube. Then show the variational version with equality iff .
Answer
Jensen: for , with equality iff is a.s. constant. Variational: , with equality iff the ratio is constant, i.e. on the cube: = the model’s own conditional density on the cell. The gap is — the same identity as the ELBO gap of Lecture 7.
Exercise 8 (spikes). A logistic bump of scale centred on an integer with weight has density at . Show that of the density at the data decreases by bits per factor 10 in , and that the unit-cell mass converges to .
Answer
The logistic density at its centre is , so : dividing by 10 subtracts . The cell mass as (plus vanishing contributions from other bumps): the discrete probabilities converge to the training frequencies, while the “density bits” diverge to (script 03, Part 1).
Exercise 9 (instantaneous change of variables). Derive from the Euler step .
Answer
. For small , , so . Dividing by and letting : .
Exercise 10 (Hutchinson’s variance). For a matrix and a probe with independent components, mean 0, variance 1, show . Show that for Gaussian probes (i.e. ), and for Rademacher probes the diagonal’s contribution disappears.
Answer
. Write ; . The cross terms have variance for both probes; the diagonal terms have variance , which is for Gaussians and 0 for Rademacher (). Hence Rademacher is never worse, and much better when the diagonal dominates (script 05, ).
Exercise 11 (bits per dimension). Glow reports 3.35 bits per dimension on CIFAR-10 (, 8-bit). How many bits per image is that, and what would uniform dequantization of 8-bit data look like (noise range, preprocessing)? What is “bits per dimension” of our best flow on ShapeWorld?
Answer
bits per image. Dequantize with , , , then scale to by dividing by 256 — whose log-determinant must be included (bits per dimension then simply add 8). ShapeWorld: bits per dimension, against for the floor.
Exercise 12 (why depth?). In 2-D, why can a 2-layer AR flow be near-exact while 16 affine couplings are not? Give one change to the coupling flow that would close most of the gap.
Answer
The AR flow’s two layers are the chain rule with 20-component mixture CDFs — each conditional is maximally flexible, and nothing else is needed in 2-D. An affine coupling can only shift and scale given , so a multimodal conditional (several eyes and arcs at one ) must be built from many alternating cuts. Replacing the affine transform by a flexible monotone one (mixture CDF with more components, or a spline) makes each coupling a full conditional density transform — in 2-D that turns two couplings into exactly the AR flow.
11. Further reading
From the slides
- L. Dinh, D. Krueger, Y. Bengio. NICE: Non-linear Independent Components Estimation, 2014; L. Dinh, J. Sohl-Dickstein, S. Bengio. Density Estimation using Real NVP, ICLR 2017.
- D. P. Kingma, P. Dhariwal. Glow: Generative Flow with Invertible 1x1 Convolutions, NeurIPS 2018.
- J. Ho, X. Chen, A. Srinivas, Y. Duan, P. Abbeel. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design, ICML 2019.
- W. Grathwohl, R. T. Q. Chen, J. Bettencourt, I. Sutskever, D. Duvenaud. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models, ICLR 2019.
- T. Müller et al. Neural Importance Sampling, 2018 (piecewise-polynomial couplings); C.-W. Huang et al. Neural Autoregressive Flows, 2018; R. T. Q. Chen et al. Residual Flows for Invertible Generative Modeling, NeurIPS 2019; Y. Song, C. Meng, S. Ermon. MintNet, NeurIPS 2019.
- L. Theis, A. van den Oord, M. Bethge. A Note on the Evaluation of Generative Models, ICLR 2016 (uniform dequantization).
- G. Papamakarios et al. Normalizing Flows for Probabilistic Modeling and Inference, JMLR 2021; I. Kobyzev et al. Normalizing Flows: An Introduction and Review of Current Methods, IEEE PAMI 2021.
Added by these notes
- R. T. Q. Chen, Y. Rubanova, J. Bettencourt, D. Duvenaud. Neural Ordinary Differential Equations, NeurIPS 2018 (the instantaneous change of variables).
- M. F. Hutchinson. A Stochastic Estimator of the Trace of the Influence Matrix for Laplacian Smoothing Splines, 1990.
- C. Durkan, A. Bekasov, I. Murray, G. Papamakarios. Neural Spline Flows, NeurIPS 2019.
- E. Hoogeboom, J. Peters, R. van den Berg, M. Welling. Integer Discrete Flows and Lossless Compression, NeurIPS 2019 — flows that stay discrete (no dequantization).
- Y. Lipman et al. Flow Matching for Generative Modeling, ICLR 2023 — training continuous flows without simulating the ODE (Lectures 11–12).
12. Next lecture
Flows impose invertibility and pay for it: the latent has the data’s dimension, and every layer must be a bijection with a cheap determinant. Lecture 7 (latent variable models I) drops that constraint: a low-dimensional latent , a decoder that need not be invertible, and — which can no longer be computed exactly. We will see why sampling from the prior is a useless estimator, how importance sampling and an amortized encoder fix it, derive the evidence lower bound twice (Jensen, and KL — the same bound as variational dequantization), compare likelihood-ratio and reparameterization gradients, and train VAEs on 2-D data (homework 2 Q1) and on ShapeWorld.