Normalizing Flows: Foundations

Invertible transformations for density estimation: the change of variables formula, coupling layers, autoregressive flows, and the tradeoff between expressiveness and computational cost.

Advanced

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Change of Variables Formula
  5. Designing Invertible Transformations
  6. Coupling Layers (RealNVP)
  7. Autoregressive Flows (MAF/IAF)
  8. Residual Flows
  9. Training via Maximum Likelihood
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Derive the change of variables formula and the role of the Jacobian determinant.
  2. Explain coupling layers and why their Jacobian is triangular.
  3. Compare autoregressive flows (fast density, slow sampling) vs inverse autoregressive (fast sampling, slow density).
  4. Derive the maximum likelihood training objective for normalizing flows.
  5. Analyze the expressiveness-computation tradeoff.

Notation

  • zpZ(z)=N(0,I)\mathbf{z} \sim p_Z(\mathbf{z}) = \mathcal{N}(0, \mathbf{I}) — base distribution
  • fθ:RdRdf_\theta: \mathbb{R}^d \to \mathbb{R}^d — invertible transformation
  • x=fθ(z)\mathbf{x} = f_\theta(\mathbf{z}) — generated sample
  • detJf\det\mathbf{J}_f — Jacobian determinant of ff

Core Intuition

A normalizing flow transforms a simple distribution (Gaussian) into a complex one (data distribution) through a chain of invertible functions. Because each function is invertible, we can compute the exact likelihood of any data point — unlike VAEs (approximate) or GANs (no likelihood). The cost: invertibility constrains the architecture.

Interactive: Normalizing Flow Transformation

z ~ N(0, I)

x = f(z)

Flow type:

Key idea: Normalizing flows transform a simple base distribution (Gaussian) into a complex target through invertible mappings. More layers = more expressive. The change-of-variables formula tracks the density through each layer.

The Change of Variables Formula

If x=f(z)\mathbf{x} = f(\mathbf{z}) where ff is a diffeomorphism:

pX(x)=pZ(f1(x))detf1x=pZ(z)detJf(z)1.(1)\boxed{p_X(\mathbf{x}) = p_Z(f^{-1}(\mathbf{x})) \cdot \left|\det\frac{\partial f^{-1}}{\partial\mathbf{x}}\right| = p_Z(\mathbf{z}) \cdot \left|\det\mathbf{J}_f(\mathbf{z})\right|^{-1}.} \tag{1}

Log-likelihood:

logpX(x)=logpZ(f1(x))+logdetJf1(x).(2)\log p_X(\mathbf{x}) = \log p_Z(f^{-1}(\mathbf{x})) + \log\left|\det\mathbf{J}_{f^{-1}}(\mathbf{x})\right|. \tag{2}

Composition of flows: f=fKfK1f1f = f_K \circ f_{K-1} \circ \ldots \circ f_1:

logpX(x)=logpZ(z0)+k=1KlogdetJfk1(zk).(3)\log p_X(\mathbf{x}) = \log p_Z(\mathbf{z}_0) + \sum_{k=1}^K \log\left|\det\mathbf{J}_{f_k^{-1}}(\mathbf{z}_k)\right|. \tag{3}

Designing Invertible Transformations

Requirements:

  1. Must be invertible (bijective).
  2. Jacobian determinant must be efficiently computable (not O(d3)O(d^3)).
  3. Must be expressive enough to model complex distributions.

Strategies for efficient Jacobian:

  • Triangular Jacobian: det=\det = product of diagonal → O(d)O(d).
  • Fixed-point iteration for inversion.
  • Structured matrices (orthogonal, permutation).

Coupling Layers (RealNVP)

Split input x=[x1,x2]\mathbf{x} = [\mathbf{x}_1, \mathbf{x}_2]:

y1=x1,y2=x2exp(s(x1))+t(x1),(4)\mathbf{y}_1 = \mathbf{x}_1, \quad \mathbf{y}_2 = \mathbf{x}_2 \odot \exp(s(\mathbf{x}_1)) + t(\mathbf{x}_1), \tag{4}

where s,ts, t are arbitrary neural networks (no invertibility constraint on them).

Inverse (trivial):

x1=y1,x2=(y2t(y1))exp(s(y1)).(5)\mathbf{x}_1 = \mathbf{y}_1, \quad \mathbf{x}_2 = (\mathbf{y}_2 - t(\mathbf{y}_1)) \odot \exp(-s(\mathbf{y}_1)). \tag{5}

Jacobian: Lower triangular → det=iexp(si(x1))=exp(isi)\det = \prod_i \exp(s_i(\mathbf{x}_1)) = \exp(\sum_i s_i). Cost: O(d)O(d).

Expressiveness: Alternate which dimensions are "frozen" (x1\mathbf{x}_1) vs "transformed" (x2\mathbf{x}_2) across layers.


Autoregressive Flows (MAF/IAF)

Masked Autoregressive Flow (MAF):

xi=ziexp(αi(x1:i1))+μi(x1:i1).(6)x_i = z_i \cdot \exp(\alpha_i(x_{1:i-1})) + \mu_i(x_{1:i-1}). \tag{6}
  • Density evaluation: O(1)O(1) passes (all xix_i known → parallel).
  • Sampling: O(d)O(d) sequential steps (must compute x1,x2,x_1, x_2, \ldots in order).

Inverse Autoregressive Flow (IAF): Reverse the direction:

  • Sampling: O(1)O(1) passes (fast).
  • Density: O(d)O(d) sequential steps (slow).

Tradeoff: MAF for density estimation; IAF for fast generation.


Residual Flows

y=x+gθ(x),(7)\mathbf{y} = \mathbf{x} + g_\theta(\mathbf{x}), \tag{7}

where gθg_\theta is Lipschitz-constrained (gLip<1\|g\|_{\text{Lip}} < 1) to ensure invertibility.

Jacobian: Use the matrix determinant lemma or Hutchinson's trace estimator:

logdet(I+Jg)=trlog(I+Jg)k=1K(1)k+1ktr(Jgk).(8)\log|\det(\mathbf{I} + \mathbf{J}_g)| = \text{tr}\log(\mathbf{I} + \mathbf{J}_g) \approx \sum_{k=1}^K \frac{(-1)^{k+1}}{k}\text{tr}(\mathbf{J}_g^k). \tag{8}

Advantage: More expressive than coupling layers (transforms all dimensions simultaneously).


Training via Maximum Likelihood

L=Expdata[logpZ(f1(x))+logdetJf1(x)].(9)\mathcal{L} = -\mathbb{E}_{\mathbf{x} \sim p_{\text{data}}}\left[\log p_Z(f^{-1}(\mathbf{x})) + \log|\det\mathbf{J}_{f^{-1}}(\mathbf{x})|\right]. \tag{9}

No adversarial training, no reconstruction loss, no variational bound — exact maximum likelihood.


Common Pitfalls

Pitfall 1. Using too few coupling layers. Each layer only transforms half the dimensions; many layers needed for full expressiveness.

Pitfall 2. Forgetting the log-det-Jacobian term. Without it, the model ignores volume changes and produces poor density estimates.

Pitfall 3. Assuming flows scale to high dimensions easily. The invertibility constraint limits expressiveness; flows typically underperform diffusion models on image generation.


Summary

  • Normalizing flows: exact likelihood via change of variables.
  • Coupling layers: Split-transform-merge; triangular Jacobian, O(d)O(d) cost.
  • Autoregressive: Full expressiveness but sequential in one direction.
  • Residual flows: Lipschitz-constrained for invertibility; trace estimation for Jacobian.
  • Training: straightforward maximum likelihood.

Exercises

Exercise 1. Derive the Jacobian determinant for a coupling layer (equation 4).

Exercise 2. Prove that a composition of invertible functions is invertible with Jacobian = product of Jacobians.

Exercise 3. Show that MAF is equivalent to a Gaussian autoregressive model with learned mean and variance.

Exercise 4. For a residual flow with gLip=0.9\|g\|_{\text{Lip}} = 0.9: bound the condition number of the Jacobian.

Exercise 5. Compare the sample quality (FID) achievable by normalizing flows vs diffusion models on CIFAR-10. Explain the gap.