Search

Search papers and lessons

Normalizing Flow

Level 3 · 11 min read

A normalizing flow turns a simple distribution into a complex one through a chain of invertible maps, and it scores data exactly by tracking how each map stretches space.

On this page

  • Colour = topic mix
  • Linear algebra
  • Probability
  • Calculus
  • Discrete math
  • Generative models
  • Inference
  • ReadReady to readNot yet

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 zz drawn from a standard Gaussian, and apply one map x=f(z)=2z+1x = f(z) = 2z + 1. The map stretches by 2 and shifts by 1. Trace one value through it.

  1. Draw a base sample. Suppose z=0.5z = 0.5 comes out of the standard Gaussian. Check: zz is a single number near the centre of its bell, so this is a typical draw.

  2. Apply the map. x=2⋅0.5+1=2x = 2 \cdot 0.5 + 1 = 2. Check: stretching 0.5 by 2 gives 1, and shifting by 1 gives 2.

  3. Undo the map to score a point. Take the data point x=2x = 2. Invert it: z=(x−1)/2=0.5z = (x - 1)/2 = 0.5. This inverse is exact, which is the key property. Check: applying the forward map to 0.5 returns 2, so the two maps agree.

  4. Track the stretch. The map multiplies lengths by 2, so a small interval of xx corresponds to an interval half as long in zz. Probability is conserved, so the density of xx is the density of zz divided by 2. Check: a flat piece of width 0.01 in zz becomes width 0.02 in xx. The same probability sits on twice the length, so the height halves.

  5. Read off the density. The density of xx at 2 is the standard Gaussian density at 0.5, divided by 2. That is 0.3521/2≈0.17600.3521 / 2 \approx 0.1760. Check: φ(0.5)=e−0.125/2π≈0.3521\varphi(0.5) = e^{-0.125}/\sqrt{2\pi} \approx 0.3521.

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

Definition: Normalizing flow

Let ff be an invertible, differentiable map from Rd\mathbb{R}^d to Rd\mathbb{R}^d, and let pzp_z be a simple base density such as a standard Gaussian. If x=f(z)\mathbf{x} = f(\mathbf{z}) with z∼pz\mathbf{z} \sim p_z, then the density of x\mathbf{x} is

px(x)=pz(f−1(x)) ∣det⁡∂f−1∂x∣p_x(\mathbf{x}) = p_z\big(f^{-1}(\mathbf{x})\big)\, \left|\det \frac{\partial f^{-1}}{\partial \mathbf{x}}\right|

A flow is a composition f=fK∘⋯∘f1f = f_K \circ \cdots \circ f_1 of such maps, and training adjusts the parameters inside each fkf_k.

Step 1 in symbols: the base sample

The base density in one dimension is pz(z)=φ(z)=12πe−z2/2p_z(z) = \varphi(z) = \frac{1}{\sqrt{2\pi}} e^{-z^2/2}, the standard Gaussian. Sampling is easy: draw zz from it, then apply ff.

Step 2 in symbols: the map

In the example, f(z)=2z+1f(z) = 2z + 1. This is invertible because f−1(x)=(x−1)/2f^{-1}(x) = (x - 1)/2 is a single well-defined formula.

Step 3 in symbols: the inverse

Apply the inverse to a data point: f−1(2)=0.5f^{-1}(2) = 0.5. The density formula needs f−1f^{-1}, not ff, so this step is the one that makes exact scoring possible.

Step 4 in symbols: the stretch factor

The factor ∣∂f−1∂x∣\left|\frac{\partial f^{-1}}{\partial x}\right| is the absolute value of the derivative of the inverse. Here f−1(x)=(x−1)/2f^{-1}(x) = (x - 1)/2, so its derivative is 1/21/2. In more dimensions the derivative becomes the Jacobian matrix of f−1f^{-1}, and its absolute Determinant plays the same role.

Step 5 in symbols: the density

px(2)=φ ⁣(2−12)⋅12=φ(0.5)⋅12=0.3521⋅0.5≈0.1760p_x(2) = \varphi\!\left(\frac{2 - 1}{2}\right) \cdot \frac{1}{2} = \varphi(0.5) \cdot \frac{1}{2} = 0.3521 \cdot 0.5 \approx 0.1760

Taking logs, which is what training uses: log⁡px(x)=log⁡pz(z)−log⁡∣det⁡Jf(z)∣\log p_x(\mathbf{x}) = \log p_z(\mathbf{z}) - \log \left|\det J_f(\mathbf{z})\right|, where z=f−1(x)\mathbf{z} = f^{-1}(\mathbf{x}) and JfJ_f 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

Example
Example 1: a pure stretch (tiny)

Let f(z)=3zf(z) = 3z with z∼N(0,1)z \sim \mathcal{N}(0, 1). Find the density of xx at x=3x = 3.

  1. The inverse is f−1(x)=x/3f^{-1}(x) = x/3, so f−1(3)=1f^{-1}(3) = 1.
  2. The base density at 1 is φ(1)=12πe−1/2≈0.2420\varphi(1) = \frac{1}{\sqrt{2\pi}} e^{-1/2} \approx 0.2420.
  3. The derivative of the inverse is 1/31/3, so the stretch factor is 1/31/3.
  4. The density is 0.2420⋅13≈0.08070.2420 \cdot \tfrac{1}{3} \approx 0.0807.

Interpretation: xx is N(0,9)\mathcal{N}(0, 9), and its density at 3 is 118πe−1/2≈0.0807\frac{1}{\sqrt{18\pi}} e^{-1/2} \approx 0.0807. The flow gives the same answer as the direct Gaussian formula.

Example
Example 2: a two-dimensional linear flow (standard)

Let z∼N(0,I)\mathbf{z} \sim \mathcal{N}(\mathbf{0}, I) in two dimensions and let f(z)=Azf(\mathbf{z}) = A\mathbf{z} with A=[2011]A = \begin{bmatrix} 2 & 0 \\ 1 & 1 \end{bmatrix}. Find the density of x\mathbf{x} at x=(2,0)\mathbf{x} = (2, 0).

  1. The determinant of AA is 2⋅1−0⋅1=22 \cdot 1 - 0 \cdot 1 = 2, so AA is invertible and the stretch factor is 1/∣det⁡A∣=1/21/|\det A| = 1/2.
  2. The inverse is A−1=12[10−12]A^{-1} = \frac{1}{2}\begin{bmatrix} 1 & 0 \\ -1 & 2 \end{bmatrix}. Applied to x=(2,0)\mathbf{x} = (2, 0), it gives z=(1,−1)\mathbf{z} = (1, -1).
  3. The base density at (1,−1)(1, -1) is 12πe−(1+1)/2=e−12π≈0.05855\frac{1}{2\pi} e^{-(1 + 1)/2} = \frac{e^{-1}}{2\pi} \approx 0.05855.
  4. The density is 0.05855⋅12≈0.029280.05855 \cdot \tfrac{1}{2} \approx 0.02928.
  5. Cross-check with the covariance: x=Az\mathbf{x} = A\mathbf{z} is Gaussian with covariance matrix Σ=AAT=[4222]\Sigma = AA^{\mathsf{T}} = \begin{bmatrix} 4 & 2 \\ 2 & 2 \end{bmatrix}, whose determinant is 4. The multivariate Gaussian formula gives 12π⋅2exp⁡(−Q/2)\frac{1}{2\pi \cdot 2} \exp(-Q/2) with Q=xTΣ−1x=2Q = \mathbf{x}^{\mathsf{T}} \Sigma^{-1} \mathbf{x} = 2, so 14πe−1≈0.02927\frac{1}{4\pi} e^{-1} \approx 0.02927. 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.

Example
Example 3: a positive quantity from a log map (realistic)

Many quantities are positive and skewed, such as waiting times or purchase amounts. Model xx as x=ezx = e^{z} with z∼N(0,1)z \sim \mathcal{N}(0, 1). This is a flow with f(z)=ezf(z) = e^z, which is invertible on positive numbers with f−1(x)=ln⁡xf^{-1}(x) = \ln x. Find the density at x=ex = e.

  1. The inverse at x=ex = e is z=ln⁡e=1z = \ln e = 1.
  2. The base density at 1 is φ(1)≈0.2420\varphi(1) \approx 0.2420.
  3. The derivative of the inverse is ddxln⁡x=1x\frac{d}{dx}\ln x = \frac{1}{x}, which at x=ex = e is 1e≈0.3679\frac{1}{e} \approx 0.3679.
  4. The density is 0.2420⋅0.3679≈0.089020.2420 \cdot 0.3679 \approx 0.08902.
  5. In log form: log⁡px(e)=log⁡φ(1)+log⁡(1/e)=−1.4189−1=−2.4189\log p_x(e) = \log \varphi(1) + \log(1/e) = -1.4189 - 1 = -2.4189, and e−2.4189≈0.0890e^{-2.4189} \approx 0.0890. 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 ee dollars is, which a sampler alone could not do.

Try it: Using the same flow x=ezx = e^{z} with z∼N(0,1)z \sim \mathcal{N}(0, 1), what is the density of xx at x=1x = 1?

Answer: The inverse is ln⁡1=0\ln 1 = 0. The base density at 0 is φ(0)=1/2π≈0.3989\varphi(0) = 1/\sqrt{2\pi} \approx 0.3989. The stretch factor is 1/x=11/x = 1. So the density is 0.39890.3989.

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 f−1f^{-1} 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 x=4zx = 4z with z∼N(0,1)z \sim \mathcal{N}(0, 1). What is the density of xx at x=4x = 4?Show answer

The inverse is z=1z = 1, and φ(1)≈0.2420\varphi(1) \approx 0.2420. The stretch factor is 1/41/4. The density is 0.2420/4≈0.06050.2420 / 4 \approx 0.0605. Check: x∼N(0,16)x \sim \mathcal{N}(0, 16), whose density at 4 is 142πe−1/2≈0.0605\frac{1}{4\sqrt{2\pi}} e^{-1/2} \approx 0.0605.

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 f−1(x)f^{-1}(\mathbf{x}), 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 f(z)=2zf(\mathbf{z}) = 2\mathbf{z} and flow B is f(z)=[1101]zf(\mathbf{z}) = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}\mathbf{z}. Which one stretches space more, and by how much?Show answer

Flow A has Jacobian 2I2I with determinant 44, so it multiplies areas by 4. Flow B has determinant 1⋅1−1⋅0=11 \cdot 1 - 1 \cdot 0 = 1, 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