TL;DR
A normalizing flow is a generative model built from a chain of invertible, differentiable maps. It starts from a simple distribution that is easy to sample, usually a standard Gaussian, and pushes samples through the chain to get data-like samples. Because every map can be undone, the model can also score any data point exactly, by tracking how much each map stretches or squeezes probability.
Builds on: Change of Variables Formula, Multivariate Gaussian, Maximum Likelihood Estimation · Leads to: generative models that score as well as sample, and work on exact densities for sampling and anomaly detection
Where it's used
Image generation. Glow (Kingma and Dhariwal, 2018) built a flow that generated photorealistic faces at a resolution where most models of its day were still blurry. Its appeal was that the same network both generated images and reported how likely each image was under the model.
Density estimation and anomaly detection. A flow gives an exact log-likelihood for any input, that is, the Logarithm of the density the model assigns to it. A fraud or sensor-fault detector can flag inputs whose log-likelihood is far below that of normal data, without needing labelled examples of the fault.
Scientific sampling. Boltzmann generators (Noé and colleagues, 2019) use flows to propose molecular configurations with the correct equilibrium probabilities. Because the density is exact, each proposal can be reweighted to remove any bias from the model, which is why this setting prefers flows over models that only sample.
How it works
We will build a flow from a single number. Start with drawn from a standard Gaussian, and apply one map . The map stretches by 2 and shifts by 1. Trace one value through it.
-
Draw a base sample. Suppose comes out of the standard Gaussian. Check: is a single number near the centre of its bell, so this is a typical draw.
-
Apply the map. . Check: stretching 0.5 by 2 gives 1, and shifting by 1 gives 2.
-
Undo the map to score a point. Take the data point . Invert it: . This inverse is exact, which is the key property. Check: applying the forward map to 0.5 returns 2, so the two maps agree.
-
Track the stretch. The map multiplies lengths by 2, so a small interval of corresponds to an interval half as long in . Probability is conserved, so the density of is the density of divided by 2. Check: a flat piece of width 0.01 in becomes width 0.02 in . The same probability sits on twice the length, so the height halves.
-
Read off the density. The density of at 2 is the standard Gaussian density at 0.5, divided by 2. That is . Check: .
Step 4 holds the whole idea. A flow scores data by undoing the maps and multiplying the base density by a correction for how much space was stretched. Real flows stack many maps and let each one be a learned neural network, provided its inverse and its stretch factor can still be computed cheaply.
flowchart LR A["Sample z from a standard Gaussian"] --> B["Map f1"] B --> C["Map f2"] C --> D["Sample x, looks like the data"] D -. "undo the maps, track stretch" .-> A
The math
Let be an invertible, differentiable map from to , and let be a simple base density such as a standard Gaussian. If with , then the density of is
A flow is a composition of such maps, and training adjusts the parameters inside each .
Step 1 in symbols: the base sample
The base density in one dimension is , the standard Gaussian. Sampling is easy: draw from it, then apply .
Step 2 in symbols: the map
In the example, . This is invertible because is a single well-defined formula.
Step 3 in symbols: the inverse
Apply the inverse to a data point: . The density formula needs , not , so this step is the one that makes exact scoring possible.
Step 4 in symbols: the stretch factor
The factor is the absolute value of the derivative of the inverse. Here , so its derivative is . In more dimensions the derivative becomes the Jacobian matrix of , and its absolute Determinant plays the same role.
Step 5 in symbols: the density
Taking logs, which is what training uses: , where and is the Jacobian of the forward map. Training maximises the average of this over the data, which is maximum likelihood estimation.
The practical design rule follows from this formula. A layer needs an inverse that is cheap to compute, and a Jacobian whose determinant is cheap too. A triangular Jacobian (a matrix whose entries are zero on one side of the diagonal) is the standard choice, because the determinant of a triangular matrix is the product of its diagonal entries.
Worked examples
Let with . Find the density of at .
- The inverse is , so .
- The base density at 1 is .
- The derivative of the inverse is , so the stretch factor is .
- The density is .
Interpretation: is , and its density at 3 is . The flow gives the same answer as the direct Gaussian formula.
Let in two dimensions and let with . Find the density of at .
- The determinant of is , so is invertible and the stretch factor is .
- The inverse is . Applied to , it gives .
- The base density at is .
- The density is .
- Cross-check with the covariance: is Gaussian with covariance matrix , whose determinant is 4. The multivariate Gaussian formula gives with , so . It agrees with step 4 up to rounding.
Interpretation: a linear flow is a multivariate Gaussian in disguise. The flow view adds the most when the maps are nonlinear, because then there is no closed-form covariance to fall back on.
Many quantities are positive and skewed, such as waiting times or purchase amounts. Model as with . This is a flow with , which is invertible on positive numbers with . Find the density at .
- The inverse at is .
- The base density at 1 is .
- The derivative of the inverse is , which at is .
- The density is .
- In log form: , and . The two forms agree.
Interpretation: this is the log-normal distribution, which is what a flow with an exponential map produces. The exact density is available, so the model can score how plausible a purchase amount of dollars is, which a sampler alone could not do.
Try it: Using the same flow with , what is the density of at ?
Answer: The inverse is . The base density at 0 is . The stretch factor is . So the density is .
Common mistakes and tradeoffs
It is tempting to think that a flow can be any neural network that maps noise to data. In fact the map must be invertible and its Jacobian determinant must be computable, because the density needs both. A plain feedforward network with bends, such as ReLU, is not invertible in general, and computing its determinant directly would be far too expensive for images.
It is tempting to think that flows are the best generative models for every task. In fact they must keep the dimension fixed and stay invertible, which limits their architecture, and they often trail diffusion models and autoregressive models on sample quality for high-resolution images. Their main advantage is the exact, cheap density.
Use a normalizing flow when you need exact likelihoods, exact reweighting, or fast sampling in one pass, as in scientific sampling and anomaly detection. Avoid it when the data are high-dimensional and sample quality matters more than likelihood, or when a variational autoencoder with an approximate posterior is enough. An autoregressive model gives exact likelihoods too, but it samples one element at a time, which is slower.
History
The change of variables rule for integrals is centuries old, and statisticians used it to transform distributions long before neural networks. In 2014 Laurent Dinh, David Krueger and Yoshua Bengio proposed NICE, a flow with a triangular Jacobian that made the density cheap to compute. In 2015 Danilo Rezende and Shakir Mohamed showed how to use flows to make variational approximations more flexible, which gave the name "normalizing flow" its wide use. RealNVP (Dinh, Sohl-Dickstein and Bengio, 2016) added coupling layers that made flows practical for images, and Glow (2018) pushed them to photorealistic faces. The idea caught on because it gives exact densities, something most deep generative models cannot provide.
Recap
- A flow pushes a simple base distribution through a chain of invertible maps.
- The density of the output is the base density at the inverse point, times the stretch factor of the inverse.
- The stretch factor is the Jacobian determinant, so layers are designed to keep it cheap.
- Training is maximum likelihood on the exact log-density.
- Read next: variational autoencoders for a related latent-variable approach, and generative models more broadly.
Check yourself
QuestionWhat two things must a layer of a normalizing flow allow you to compute cheaply, and why does exact scoring need both?Show answer
The inverse of the layer, and the determinant of its Jacobian. Scoring a data point needs to find the base point, and it needs the stretch factor, the absolute Jacobian determinant, to correct the density for how space was stretched.
QuestionA flow is with . What is the density of at ?Show answer
The inverse is , and . The stretch factor is . The density is . Check: , whose density at 4 is .
QuestionWhy does a normalizing flow need an invertible map rather than any neural network?Show answer
Because the density formula evaluates the base density at , so the inverse must exist and be computable. If several inputs mapped to the same output, there would be no single base point to score, and the probability would not be well defined.
QuestionTwo flows both map a standard Gaussian in two dimensions. Flow A is and flow B is . Which one stretches space more, and by how much?Show answer
Flow A has Jacobian with determinant , so it multiplies areas by 4. Flow B has determinant , so it preserves areas. Flow A stretches space four times as much in area, though B is a shear that changes shape without changing area.
Sources
- Rezende and Mohamed, Variational Inference with Normalizing Flows (2015): https://arxiv.org/abs/1505.05770
- Dinh, Sohl-Dickstein and Bengio, Density estimation using Real NVP (2016): https://arxiv.org/abs/1605.08803
- Normalizing flow (Wikipedia): https://en.wikipedia.org/wiki/Normalizing_flow