Skip to main content
Open In Colab

Sampling and evaluating a density

The transport sections built maps and fields that turn samples of a simple source into samples of a target. A generative model often has a second job. Given a point xx, it should report how likely the model finds that point, the value pX(x)p_X(x) of its density. You need this number to train by maximum likelihood, to compare models on held-out data, and to flag inputs the model finds unlikely. Sampling alone does not give you this number. If you push samples z∼pZz \sim p_Z through a function GG, the outputs x=G(z)x = G(z) follow some density pXp_X. To evaluate it at one point you have to know which zz produced xx and how much GG stretched the space around zz. A normalizing flow is a generative model built so that both are easy to compute: GG is invertible, and the stretch has a formula.

The change of variables formula

Let ZZ have density pZp_Z on Rd\mathbb{R}^d and let GG be an invertible, differentiable map of Rd\mathbb{R}^d onto itself. The density of X=G(Z)X = G(Z) is pX(x)=pZ(G−1(x)) ∣det⁡JG−1(x)∣,p_X(x) = p_Z\bigl(G^{-1}(x)\bigr)\,\bigl|\det J_{G^{-1}}(x)\bigr| , where JG−1J_{G^{-1}} is the Jacobian matrix of the inverse map. The determinant measures how much G−1G^{-1} scales a small volume around xx. Mass is conserved: the mass in a small cell around zz ends up in the image of that cell, so if GG spreads the cell over a larger volume, the density falls by the same factor. In 1D the Jacobian is the derivative, and the formula reads pX(x)=pZ(z) ∣dz/dx∣p_X(x) = p_Z(z)\,|dz/dx| with z=G−1(x)z = G^{-1}(x). The transport maps and couplings section already built such a map. With pZ=N(0,1)p_Z = \mathcal{N}(0, 1), CDF Φ\Phi, and the mixture CDF F1F_1, the increasing map G=F1−1∘Φ,G−1=Φ−1∘F1,G = F_1^{-1} \circ \Phi, \qquad G^{-1} = \Phi^{-1} \circ F_1 , sends standard normal samples to samples of the mixture. It is the map TT of the transport sections, with zz in place of x0x_0. It is increasing, so it is invertible, and it is a normalizing flow. The code below evaluates its change of variables density on a grid. It computes G−1G^{-1} from the mixture CDF and the derivative dz/dxdz/dx by central differences. The result is the mixture density pX=p1p_X = p_1, with weights (0.3,0.5,0.2)(0.3, 0.5, 0.2), means (−2,1,3.5)(-2, 1, 3.5) and standard deviations (0.4,0.6,0.3)(0.4, 0.6, 0.3). The code also confirms that G−1(G(z))=zG^{-1}(G(z)) = z.
Output from cell 4 Histogram of 200,000 samples G(z)G(z), the mixture density (thick orange) and the change of variables density (thin blue). The two curves coincide. Sampling runs GG forward on a draw from N(0,1)\mathcal{N}(0, 1). Evaluating runs G−1G^{-1} on the query point and corrects by the derivative. A neural flow uses the same two operations, with GG built from layers whose inverses and Jacobian determinants are cheap to compute.

Composing maps

A single invertible layer is rarely flexible enough, so flows stack several. Write G=GK∘⋯∘G1G = G_K \circ \cdots \circ G_1, start from z0=zz_0 = z, and set zk=Gk(zk−1)z_k = G_k(z_{k-1}), so that x=zKx = z_K. The determinant of a product of Jacobians is the product of their determinants, so the log-density picks up one term per layer: log⁡pX(x)=log⁡pZ(z)−∑k=1Klog⁡∣det⁡JGk(zk−1)∣.\log p_X(x) = \log p_Z(z) - \sum_{k=1}^{K} \log \bigl|\det J_{G_k}(z_{k-1})\bigr| . The terms add, so you can compute them layer by layer during the forward pass. The quantile map splits into two layers. The first, G1=ΦG_1 = \Phi, sends N(0,1)\mathcal{N}(0, 1) to the uniform density on (0,1)(0, 1). The second, G2=F1−1G_2 = F_1^{-1}, sends the uniform density to the mixture. The code below computes each log-derivative by central differences at five points and adds the terms.
The first layer cancels log⁡pZ(z)\log p_Z(z) exactly, because the uniform density equals 1 on (0,1)(0, 1). The second layer then carries the whole log-density of the mixture. The last two columns agree to the printed precision.

Continuous flows

Stacking more and smaller layers leads to a flow in continuous time. A velocity field vtv_t moves each particle by x˙t=vt(xt)\dot x_t = v_t(x_t), and the map from x0x_0 to x1x_1 is invertible because you can run the same equation backward in time. The log-determinant of a stack becomes an integral along each trajectory. The integral follows from the continuity equation of the dynamic transport section, ∂tpt+∇⋅(ptvt)=0\partial_t p_t + \nabla \cdot (p_t v_t) = 0. Expanding the divergence of the product gives ∂tpt+vt⋅∇pt+pt ∇⋅vt=0.\partial_t p_t + v_t \cdot \nabla p_t + p_t\, \nabla \cdot v_t = 0 . Along a trajectory XtX_t the chain rule gives ddtpt(Xt)=∂tpt(Xt)+∇pt(Xt)⋅vt(Xt)\frac{d}{dt} p_t(X_t) = \partial_t p_t(X_t) + \nabla p_t(X_t) \cdot v_t(X_t), which is the sum of the first two terms. So ddtpt(Xt)=−pt(Xt) ∇⋅vt(Xt)\frac{d}{dt} p_t(X_t) = -p_t(X_t)\,\nabla \cdot v_t(X_t). Dividing by pt(Xt)p_t(X_t) and integrating from 00 to 11, ddtlog⁡pt(Xt)=−∇⋅vt(Xt),log⁡p1(X1)=log⁡p0(X0)−∫01∇⋅vt(Xt) dt.\begin{aligned} \frac{d}{dt} \log p_t(X_t) &= -\nabla \cdot v_t(X_t), \\ \log p_1(X_1) &= \log p_0(X_0) - \int_0^1 \nabla \cdot v_t(X_t)\,dt . \end{aligned} This is the instantaneous change of variables formula of continuous normalizing flows (Chen et al., 2018). The divergence is the trace of the Jacobian of vtv_t, so it replaces the determinant of the discrete formula. To evaluate log⁡p1(x)\log p_1(x) at a point, integrate the trajectory backward from xx to find x0x_0, and subtract the integral of the divergence along the way. The displacement interpolation of the dynamic transport section gives a check with a known answer. Each particle moves at the constant velocity T(x0)−x0T(x_0) - x_0, and in 1D the divergence is ∂xvt\partial_x v_t. The code below tracks 16,001 particles at evenly spaced quantile levels. At each time it estimates ∂xvt\partial_x v_t at every particle from the velocities and positions of its neighbors, and it integrates −∂xvt-\partial_x v_t in time with the trapezoid rule. At t=1t = 1 the result matches the log of the mixture density at T(x0)T(x_0).
Output from cell 9 Left, seven particle paths of the displacement interpolation. Right, the log-density along each path from the integrated divergence. The dots at t=1t = 1 are the log of the mixture density at the end of each path. The largest error, printed above, is at the particle that starts near x0=−0.52x_0 = -0.52 (green). It ends in the low-density gap between the first two components. Its neighbors head for different components, so the field is steep there at small tt, and the finite differences in xx are least accurate. For a particle with T′(x0)>0T'(x_0) > 0 the integral has a closed form, ∫01T′(x0)−11−t+t T′(x0) dt=log⁡T′(x0)\int_0^1 \frac{T'(x_0) - 1}{1 - t + t\,T'(x_0)}\,dt = \log T'(x_0). The continuous formula therefore reproduces the change of variables formula for the map TT.

Flows, decoders and the models that follow

An invertible map keeps the dimension: zz and xx both live in Rd\mathbb{R}^d. A latent-variable model such as the variational autoencoder decodes a low-dimensional zz into a high-dimensional xx. Its decoder has no inverse, so the density of xx is an integral over all zz that could have produced it, and training works with a lower bound on that integral. A flow has an exact density, but its latent space has as many dimensions as the data. The likelihood of a continuous flow is exact, but each evaluation integrates an ODE together with a divergence, and maximum likelihood training repeats that inside every gradient step. Flow matching trains the field of a continuous normalizing flow by regression on a chosen probability path (Lipman et al., 2022). It never evaluates a likelihood during training. After training, the formula above still gives the likelihood of any point under the learned field. The probability flow ODE of score-based diffusion is a deterministic flow with the same marginals as the diffusion, and the same integral of the divergence evaluates likelihoods for a diffusion model (Song et al., 2020). Key references: (Chen et al., 2018; Lipman et al., 2022; Song et al., 2020)

References

  • Chen, R., Rubanova, Y., Bettencourt, J., Duvenaud, D. (2018). Neural ordinary differential equations. arXiv [cs.LG]. DOI
  • Lipman, Y., Chen, R., Ben-Hamu, H., Nickel, M., Le, M. (2022). Flow Matching for generative modeling. arXiv [cs.LG]. Link
  • Song, Y., Sohl-Dickstein, J., Kingma, D., Kumar, A., Ermon, S., et al. (2020). Score-based generative modeling through stochastic differential equations. arXiv [cs.LG]. DOI

Found an error on this page? Report an issue.