Normalizing Flows: Tractable Density via Invertible Transforms

Invertible maps that turn a simple density into a richer one: exact log-likelihood from the change-of-variables formula, and layer designs that keep the Jacobian determinant cheap.

Advanced · Undergraduate mathematics · about 22 minutes

Reading map and next steps

Intuition

The idea in plain words, before the symbols.

A normalizing flow is an invertible, differentiable map that warps a simple distribution into a richer one (Rezende and Mohamed, 2015).

Start from a simple base distribution, such as a standard Gaussian. Then apply an invertible transformation that bends and stretches space into a richer model distribution.

Because the transform is invertible, you get:

  • Exact sampling: sample zz from the base, push forward to x=f(z)x=f(z).
  • Tractable log-likelihood under the flow model: map xx back to z=f−1(x)z=f^{-1}(x) and account for how volumes change.

The price is architectural: you must design ff so it is invertible and the needed map direction plus Jacobian determinant are cheap to compute.

The two terms of the log-likelihood can pull in opposite directions. A map that squeezes space raises density through its volume term, but it may have to start from a base point far in the tail to reach the point being scored. The practice after the demo compares two such maps.

Study prompts for this section: Intuition
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

Demo

Change an input, predict the result, and see which quantities respond.

ExploreChange one control at a time and name what responds.An optional observation guide follows the demo.

Explore Normalizing Flows: Tractable Density via Invertible Transforms

Loading the interactive demo…

Observation guide

Optional and ungraded. Opening a guide saves nothing.

Choose what to inspect in Normalizing Flows: Tractable Density via Invertible Transforms, then open its guide.

The demo below asks you to predict the density effect before revealing the Jacobian correction. The exact log-likelihood always has two terms: the base log-density at the inverse point and the log-volume correction from the inverse Jacobian.

Study prompts for this section: Demo
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

Practice

Two affine flows of a standard normal score the same point. Predict which gives the higher density, check both log terms, then change the point and the maps.

Loading the exercise…

Mathematics

Definitions, assumptions and the derivation.

Change of variables

Let x=f(z)x=f(z) where ff is invertible and z∼pzz\sim p_z. Then:

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

so:

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

The determinant term is the volume correction. Relative to the base density at the corresponding latent point, an inverse map that squeezes a small data-space volume into a smaller latent-space volume multiplies density by a factor below one; an inverse map that expands that volume multiplies it by a factor above one.

Equivalently, expansion by the forward map spreads probability over more data-space volume and lowers density. For example, in one dimension x = 2z gives p_x(x) = p_z(x / 2) / 2. When comparing different points or transforms, the base-density term can also change, so the determinant alone does not determine which total density is larger.

This is the tractable-density advantage of flows, but also the constraint: a flexible transform is useful for likelihood work only when the needed map direction and log-determinant stay tractable.

The volume term does not rank densities by itself

For a one-dimensional affine map x=az+bx=az+b with a≠0a\neq0 and a standard normal base, the change of variables reads

log⁡px(x)=−12log⁡(2π)−12(x−ba)2−log⁡∣a∣.\log p_x(x)=-\tfrac12\log(2\pi)-\tfrac12\left(\frac{x-b}{a}\right)^2-\log|a|.

Compare map A, x=2zx=2z, with map B, x=z/2x=z/2, at the point x=1x=1. B's volume term is larger, +log⁡2+\log 2 against −log⁡2-\log 2. But B must start from z=2z=2, two standard deviations out, while A starts from z=1/2z=1/2:

log⁡pA(1)−log⁡pB(1)=(−18−log⁡2)−(−2+log⁡2)=158−2log⁡2≈0.489>0.\log p_A(1)-\log p_B(1)=\Big(-\tfrac18-\log 2\Big)-\Big(-2+\log 2\Big)=\tfrac{15}{8}-2\log 2\approx0.489>0.

The same happens with CC, x=3z+1x=3z+1, and DD, x=z/3+1x=z/3+1, at x=2x=2: D's volume term is larger by 2log⁡32\log 3, yet log⁡pC(2)−log⁡pD(2)=409−2log⁡3≈2.247\log p_C(2)-\log p_D(2)=\tfrac{40}{9}-2\log 3\approx2.247. The Jacobian determinant matters only together with the base density at the preimage.

The absolute value ignores orientation. The map x=−2zx=-2z has the same volume term as x=2zx=2z, and at x=1x=1 the same density, because its preimage z=−1/2z=-1/2 has the same standard normal density as 1/21/2. With a base distribution that is not symmetric, the two densities can differ.

Maps that are not one-to-one

The formula needs exactly one preimage. For x=z2x=z^2, both z=xz=\sqrt x and z=−xz=-\sqrt x reach a point x>0x>0, and each contributes its base density divided by ∣dx/dz∣=2x|dx/dz|=2\sqrt x:

px(x)=pz(x)+pz(−x)2x,x>0.p_x(x)=\frac{p_z(\sqrt x)+p_z(-\sqrt x)}{2\sqrt x},\qquad x>0.

One branch alone integrates to 1/21/2. At x=0x=0 the derivative vanishes, and below 00 there is no preimage. Normalizing flows avoid these cases by building ff from invertible layers whose Jacobian determinants are never zero.

Composition of flows

If f=fK∘⋯∘f1f=f_K\circ\cdots\circ f_1 maps latent variables forward, with zk=fk(zk−1)z_k=f_k(z_{k-1}) and zK=xz_K=x, then the inverse-Jacobian log-determinants add as a negative sum of forward log-determinants:

log⁡px(x)=log⁡pz(z0)−∑k=1Klog⁡∣det⁡∂fk∂zk−1∣.\log p_x(x)=\log p_z(z_0)-\sum_{k=1}^K\log\left|\det\frac{\partial f_k}{\partial z_{k-1}}\right|.

This follows from the determinant of a product of Jacobians. A general d×dd\times d determinant costs O(d3)O(d^3) operations, so practical flows are built from layers with cheap Jacobians. The coupling layers of Real NVP (Dinh et al., 2017), for example, have triangular Jacobians, whose log-determinant is just the sum of the logs of the dd diagonal entries. Papamakarios et al. (2021) review the main designs.

A one-dimensional check of the sum: following z1=2z0z_1=2z_0 by x=z1/4x=z_1/4 gives the single map x=z0/2x=z_0/2, and the forward log-determinants log⁡2+log⁡14=−log⁡2\log 2+\log\tfrac14=-\log 2 equal that map's.

Study prompts for this section: Mathematics
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

Code

The same calculation as runnable code, in the notation of the derivation.

import numpy as np

# A tiny 2D linear flow: x = A z + b, with z ~ N(0, I)
A = np.array([[1.2, 0.3], [0.1, 0.9]])
b = np.array([0.5, -0.2])
Ainv = np.linalg.inv(A)

def log_pz(z):
    d = z.shape[0]
    return -0.5 * float(z @ z) - 0.5 * d * np.log(2 * np.pi)

x = np.array([1.0, 0.0])
z = Ainv @ (x - b)
log_px = log_pz(z) + np.log(abs(np.linalg.det(Ainv)))

print("det(A):", round(float(np.linalg.det(A)), 3))
print("z:", np.round(z, 3))
print("log p_x(x):", round(float(log_px), 3))

The second block computes the comparisons from the Mathematics section and checks that the two-branch density for x=z2x=z^2 integrates to one.

import numpy as np

def log_density(x, a, b):
    # x = a z + b with z ~ N(0, 1): the base density at the preimage, then the volume term
    z = (x - b) / a
    return -0.5 * np.log(2 * np.pi) - 0.5 * z**2 - np.log(abs(a))

A, B = log_density(1, 2, 0), log_density(1, 0.5, 0)
C, D = log_density(2, 3, 1), log_density(2, 1 / 3, 1)
print("log p_A(1) - log p_B(1):", round(A - B, 6), "vs 15/8 - 2 ln 2 =", round(15 / 8 - 2 * np.log(2), 6))
print("log p_C(2) - log p_D(2):", round(C - D, 6), "vs 40/9 - 2 ln 3 =", round(40 / 9 - 2 * np.log(3), 6))
print("x = -2z and x = 2z agree at x = 1:", bool(np.isclose(log_density(1, -2, 0), A)))

# x = z^2 is two-to-one: sum both preimages, each divided by |dx/dz| = 2 sqrt(x)
phi = lambda z: np.exp(-z**2 / 2) / np.sqrt(2 * np.pi)
def square_density(x):
    r = np.sqrt(x)
    return (phi(r) + phi(-r)) / (2 * r)

u = np.linspace(1e-9, 12, 1_200_001)       # substitute x = u^2, dx = 2u du, to pass the 1/sqrt(x) peak
print("both branches integrate to", round(float(np.trapezoid(square_density(u**2) * 2 * u, u)), 6),
      "; one branch to", round(float(np.trapezoid(phi(u) / (2 * u) * 2 * u, u)), 6))
Study prompts for this section: Code
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

In focus

Concept: Normalizing Flows: Tractable Density via Invertible Transforms

What is the smallest example of Normalizing Flows you can work through by hand, and what does it show?

BeforeMaximum LikelihoodNextDiffusion, Score-Based Models & Flow Matching
DetailsGenerative Models

After the first pass

Go deeper

Optional material for a second reading. Sources are always shown; the other panels open from their headings or from these links, and closing one keeps what you wrote or revealed.
What to look for in each sectionChoose a section and what you expect to see in it, then read short advice.

Section by section

Choose what to look for in each section

Invertible maps that turn a simple density into a richer one: exact log-likelihood from the change-of-variables formula, and layer designs that keep the Jacobian determinant cheap.

Choose what to look for (no demo yet)01 / Intuition
Illustration of an invertible grid warp with local volume-correction patches.
What to look for

Start with the picture, metaphor, or geometric mechanism.

Choose first

Choose what you expect to see in this section of Normalizing Flows: Tractable Density via Invertible Transforms.

Questions to ask of the pictureChoose a part of the picture and what you expect, then read short advice.

Looking at the picture

Questions to ask of the picture

Invertible maps that turn a simple density into a richer one: exact log-likelihood from the change-of-variables formula, and layer designs that keep the Jacobian determinant cheap.

4 of 4 sections writtenNo live demo yet
Question

Which part of the picture should you look at first?

Choose first

Pick the part of the picture you expect to explain Normalizing Flows: Tractable Density via Invertible Transforms.

Sources

References for this page

A reference shows where an idea comes from; it does not vouch for every step on this page.
paper · 2015Variational Inference with Normalizing FlowsRezende and Mohamed
Why it is cited

Introduces the term normalizing flow and the change-of-variables density update for a sequence of invertible maps, used there to enrich variational posteriors (ICML 2015).

Open source
paper · 2017Density estimation using Real NVPDinh, Sohl-Dickstein, and Bengio
Why it is cited

Builds flows from affine coupling layers whose Jacobians are triangular, so the log-determinant is a sum of per-dimension terms (ICLR 2017).

Open source
paper · 2021Normalizing Flows for Probabilistic Modeling and InferencePapamakarios et al.
Why it is cited

A review of normalizing flows: the change-of-variables formula, composition, and the main layer designs that keep the inverse and the Jacobian determinant cheap (Journal of Machine Learning Research, 2021).

Open source
book · 2024Deep Learning: Foundations and ConceptsBishop and Bishop
Why it is cited

Springer. Chapter 18 is a textbook treatment of normalizing flows.

Open source
What each claim rests on1 claim, each with its sources, where it appears on this page, and its caveats.

Claims and sources

Invertible maps that turn a simple density into a richer one: exact log-likelihood from the change-of-variables formula, and layer designs that keep the Jacobian determinant cheap.

Checked against the cited passages

The checks were made by this site, not by an independent reviewer.

Normalizing flows compose invertible maps to turn a simple initial density into a richer one; for a tractable finite flow, the log density at a point is computed exactly by evaluating the base log density at the corresponding preimage and adding the inverse-Jacobian log determinant.
What the sources support

Rezende and Mohamed define a normalizing flow as transforming a probability density through a sequence of invertible mappings, state that repeated change-of-variables moves an initial density through that se...

Where to see it on this page
Equation 1
px(x)=pz(f−1(x))∣det⁡∂f−1∂x∣,p_x(x)=p_z\big(f^{-1}(x)\big)\left|\det\frac{\partial f^{-1}}{\partial x}\right|,
Equation 2
log⁡px(x)=log⁡pz(f−1(x))+log⁡∣det⁡∂f−1∂x∣.\log p_x(x)=\log p_z\big(f^{-1}(x)\big)+\log\left|\det\frac{\partial f^{-1}}{\partial x}\right|.
Caveat

Rezende and Mohamed frame flows for variational approximate posteriors and density transformation; they do not by themselves justify broad claims about modern data modeling, sampling quality, stability, or u...

StatusChecked against the cited passages.

Rezende and Mohamed support normalizing flows as sequences of invertible maps that turn a simple initial density into a richer one. They derive the change-of-variables update for a finite flow using inverse or forward Jacobian determinants, and give the log density of the composition as the base log density minus the summed forward log-determinants. The page's equations, code (an affine flow) and demo look up the preimage and add the inverse-Jacobian log-volume correction.

Explain it without the pageWrite your own explanation before asking for feedback. Your draft and any hints stay while this page is open.

Practice · Normalizing Flows: Tractable Density via Invertible Transforms

Try the idea in your own words

Invertible maps that turn a simple density into a richer one: exact log-likelihood from the change-of-variables formula, and layer designs that keep the Jacobian determinant cheap.

Concept · Selected for practice

Normalizing Flows: Tractable Density via Invertible Transforms

What it rests on: Sources: Variational Inference with Normalizing Flows; Density estimation using Real NVP; Normalizing Flows for Probabilistic Modeling and Inference

Context and links
Choose a task

Explain the mechanism

For Normalizing Flows: Tractable Density via Invertible Transforms: What is the smallest example of Normalizing Flows you can work through by hand, and what does it show? Explain your answer, including what changes, why, and which assumption matters.

No answer yet

A rough first thought is enough. Your draft stays when you change tasks.

Your draft stays on this page and is cleared when you leave or reload.

A little help · Explain

Open one hint at a time. These are suggestions, not your answer or a grade.

0 of 3 hints shown for this question.

    Clearing your answer keeps this help record. Outside help is your own report; this page cannot check it.

    Where am I stuck? (optional)
    Your own description, not an automatic diagnosis

    Choose one, or leave this unspecified. Select it again to clear it.

    Take your draft to a feedback conversation

    No AI feedback runs here. You can copy a prompt to use elsewhere; nothing is sent automatically. Review the text before sharing, and leave out private information.

    Write an attempt before copying a feedback prompt.

    A draft, or an AI reply to it, is not a test of what you have learned. To check that, try a different case later without help.

    Ask about this pagePick one item on this page and copy a prompt about it into an AI assistant you already use. Notes you write here stay in this browser.
    Ask about this pageClose
    ConceptNormalizing Flows: Tractable Density via Invertible TransformsSources: Variational Inference with Normalizing Flows; Density estimation using Real NVP; Normalizing Flows for Probabilistic Modeling and InferenceA = np.array([[1.2, 0.3], [0.1, 0.9]])

    Your question

    Choose what your question is about

    Pick the idea, equation, source, code, claim, misconception or demo state you want to ask about. The prompt you copy includes it, so the answer can stay on that item.
    Next local actionNo local draft saved yet

    Open the draft below to save one note and next action in this browser.

    conceptGenerative Models

    Normalizing Flows: Tractable Density via Invertible Transforms

    Starting question

    What is the smallest example of Normalizing Flows you can work through by hand, and what does it show?

    SourcesCheck the 4 cited sources listed under Sources.Notes can be saved for this item
    Points of view on this item

    Fixed prompts generated from this item. They are not comments from people or independent reviews.

    Learner’s first stepAsk what would make "Normalizing Flows: Tractable Density via Invertible Transforms" feel predictable rather than familiar.
    Assumption

    The 4 cited sources must support this exact item, not just the surrounding topic.

    Source-checking summary

    Connect the definition to one equation, piece of code or demo before widening the discussion.

    Proposed experiment

    Change one input, then check that the same relationship holds in the mathematics, the code and the demo.

    Next action

    You can state the mechanism in your own words

    Evidence4 checks
    ObservationChecking for a saved observation
    ActionReady for one action
    PromptLearner prompt ready to copy
    Go to this item
    01ObservationChecking what this browser has saved
    02EvidenceChecking for a saved observation
    03SourcesCheck the 4 cited sources listed under Sources.
    04Next stepSave one next action
    Local action draftNo local draft saved yetOpen when you are ready to write one next action
    Local action draft

    This draft stays in this browser and is attached to this item only.

    No local draft saved.
    Evidence to inspect
    • What the 4 cited sources say about this exact item
    • The definition, its prerequisites, and a contrasting concept
    • The equation or code that makes the concept concrete
    • One demo state that shows what stays the same as the inputs change
    What would resolve this
    • You can state the mechanism in your own words
    • You can name the prerequisite that would clear up the confusion
    • You can predict how the result changes when one input changes
    Reference idconcept/concept-notebook/generative-models/normalizing-flows concept:generative-models/normalizing-flows