Skip to content
BIASED

The change of variables

How an invertible map turns a simple density into a complex one and still keeps the likelihood exact: the change of variables, the Jacobian determinant and coupling layers.

Exact likelihood and samples

Generate and evaluate

In Wasserstein GANs, a critic guided a generator without providing a normalized density for each input. A normalizing flow trades architectural freedom for an invertible map and an exactly evaluable density. Unlike a VAE's approximate posterior and ELBO, its encoding is deterministic and its likelihood needs no variational bound.

Keep the directions explicit. Here f:x↦zf:x\mapsto z encodes data into the base distribution, following this course's notation. Write F=f−1F=f^{-1} for generation, z↦xz\mapsto x. The linked longer note calls its generating map ff; the mathematics is the same after exchanging the names.

A standard flow uses differentiable bijections with differentiable inverses between spaces of the same dimension. Draw zz from a known base density, then generate x=F(z)x=F(z). To score an observed xx, encode it to z=f(x)z=f(x) and account for the change in volume. Invertibility alone does not tell us that both directions are cheap; the layer design determines that.

The change of variables on a line

Preserve probability, not density height

Take the reviewed new example: pZ(z)=2zp_Z(z)=2z on [0,1][0,1] and generation x=F(z)=3−2zx=F(z)=3-2z. The support becomes [1,3][1,3], orientation reverses and every small interval doubles in length. Its probability stays the same, so its density halves. Encoding is z=f(x)=(3−x)/2z=f(x)=(3-x)/2.

In one dimension,

pX(x)=pZ(z)∣F′(z)∣=pZ(f(x)) ∣f′(x)∣.p_X(x)=\frac{p_Z(z)}{|F'(z)|}=p_Z(f(x))\,|f'(x)|.

For this example pX(x)=(3−x)/2p_X(x)=(3-x)/2 on [1,3][1,3] and zero outside. Integrating gives one. The absolute value is essential: reflection cannot produce a negative density.

Reading off only the base density fails. For Z∼N(0,1)Z\sim\mathcal N(0,1) and generation F(z)=2zF(z)=2z, the guess pZ(x/2)p_Z(x/2) integrates to two. Dividing by the generating slope gives the actual density pZ(x/2)/2p_Z(x/2)/2. Density is probability per unit length, not the probability of one exact point.

Two bumps out of one

One peak can become two

Consider the reviewed smooth generating map

F(z)=z+0.8tanh⁡(2z),F′(z)=1+1.6sech⁡2(2z).F(z)=z+0.8\tanh(2z),\qquad F'(z)=1+1.6\operatorname{sech}^2(2z).

Here sech⁡(u)=1/cosh⁡(u)\operatorname{sech}(u)=1/\cosh(u). The derivative is positive, so the map is strictly increasing. Its bounded added term also ensures F(z)→±∞F(z)\to\pm\infty, making it onto the real line. Positivity of a derivative alone would not prove that last property.

Near the origin this map stretches more than it does in the tails. For a standard normal base, at z=0.5z=0.5 it gives x≈1.109x\approx1.109, slope 1.6721.672 and density 0.21060.2106. Dividing the Gaussian density by the varying slope creates two peaks. A smooth bijection preserves neither peak height nor the number of density peaks.

There is a different restriction: a diffeomorphism of the whole real space applied to a Gaussian keeps density positive everywhere. It cannot exactly create an open gap of zero density or concentrate all mass onto a lower-dimensional surface. A two-component Gaussian mixture has no zero-density gap; it is representable in principle, although well-separated modes may require extreme stretching.

The Jacobian and volume

Length becomes volume

For dd coordinates, Jf(x)J_f(x) contains derivatives of encoded outputs with respect to inputs. Locally the map acts like a linear transformation, whose absolute determinant multiplies volume. Therefore

pX(x)=pZ(f(x)) ∣det⁡Jf(x)∣,log⁡pX(x)=log⁡pZ(z)+log⁡∣det⁡Jf(x)∣.p_X(x)=p_Z(f(x))\,|\det J_f(x)|,\qquad \log p_X(x)=\log p_Z(z)+\log|\det J_f(x)|.

Equivalently, generation divides by ∣det⁡JF(z)∣|\det J_F(z)|. Their determinants are reciprocal at corresponding points. A shear with determinant one can change shapes while preserving local volume. A negative determinant reverses orientation; a zero determinant prevents a differentiable inverse at that point.

A bijection need not be a diffeomorphism: F(z)=z3F(z)=z^3 is one-to-one and onto, but its derivative vanishes at zero and its inverse is not differentiable there. Standard flow layers avoid this singularity. The density formula here assumes the appropriate nonzero determinant, not merely the ability to write an inverse symbol.

Stacking layers

Add local log-volume changes

Compose encoders h0=xh_0=x, hk=fk(hk−1)h_k=f_k(h_{k-1}), z=hKz=h_K. The chain rule multiplies Jacobians, determinants multiply, and logarithms add:

log⁡pX(x)=log⁡pZ(z)+∑k=1Klog⁡∣det⁡Jfk(hk−1)∣.\log p_X(x)=\log p_Z(z)+\sum_{k=1}^K\log|\det J_{f_k}(h_{k-1})|.

Each term is evaluated at its own layer input. For two scalar encoding steps with derivatives −2-2 and 1/41/4, the total absolute factor is 1/21/2 and the log correction is −log⁡2-\log2. Reversing orientation does not change that absolute factor.

To evaluate a point, first run the encoding layers and accumulate their log-determinants; then evaluate the base density at the resulting zz and add the correction. Generation starts from a base draw and applies fK−1,…,f1−1f_K^{-1},\ldots,f_1^{-1} in that order. “Forward” alone is an ambiguous word; specify which map you mean.

Triangular Jacobians

Make the determinant affordable

A dense d×dd\times d determinant generally costs O(d3)O(d^3) with elimination. A triangular matrix has determinant equal to the product of its diagonal entries, so its log-absolute-determinant costs O(d)O(d) once those entries are available. Computing the neural network still has its own cost.

For an encoding Jacobian with diagonal 2,−3,1/42,-3,1/4, the absolute determinant is 3/23/2. Off-diagonal entries can bend or shear the transformation without appearing in this product.

A triangular Jacobian with nonzero diagonal gives local invertibility. Global inversion needs more: each coordinate transformation, with the preceding coordinates fixed, must be one-to-one and onto its required range. A strictly positive exponential scale in an affine coordinate transformation meets those conditions on the real line.

Coupling layers

Keep one part while changing the other

An affine coupling encoder splits the input into xA,xBx_A,x_B:

zA=xA,zB=xB⊙exp⁡(s(xA))+t(xA).z_A=x_A,\qquad z_B=x_B\odot\exp(s(x_A))+t(x_A).

The differentiable functions s,ts,t can be expressive and need not themselves be invertible. Once zAz_A is known, the inverse is explicit:

xA=zA,xB=(zB−t(zA))⊙exp⁡(−s(zA)).x_A=z_A,\qquad x_B=(z_B-t(z_A))\odot\exp(-s(z_A)).

The Jacobian is block triangular, with diagonal entries one for the preserved part and exp⁡(sj(xA))\exp(s_j(x_A)) for the transformed part. Thus log⁡∣det⁡Jf∣=∑jsj(xA)\log|\det J_f|=\sum_j s_j(x_A). Alternating the preserved part allows later layers to transform every coordinate.

For a new two-coordinate calculation, choose s(x1)=log⁡2s(x_1)=\log2 and t(x1)=x1t(x_1)=x_1. Then f(x1,x2)=(x1,2x2+x1)f(x_1,x_2)=(x_1,2x_2+x_1), F(z1,z2)=(z1,(z2−z1)/2)F(z_1,z_2)=(z_1,(z_2-z_1)/2) and the encoding determinant is two. This is an explicitly specified arithmetic example, not a trained network or a copied experiment.

Training and the limits of flows

The volume term is part of training

Maximum likelihood minimizes the mean negative value of the exact log-density. For the generating affine map F(z)=az+bF(z)=az+b, a>0a>0, and a standard normal base, the negative log-density is

−log⁡pX(x)=(x−b)22a2+log⁡a+12log⁡(2π).-\log p_X(x)=\frac{(x-b)^2}{2a^2}+\log a+\frac12\log(2\pi).

For nonconstant observations, the best bb is the sample mean and the best a2a^2 is the sample variance with denominator NN. Omitting log⁡a\log a rewards increasing the scale without paying for the extra volume. With all observations identical, the unconstrained Gaussian likelihood instead diverges as a→0a\to0; a finite fit requires additional constraints.

The reviewed synthetic experiment draws 4,096 Gaussian values with seed 9303, mean parameter 0.450.45 and scale parameter 1.61.6. The fitted mean and scale are 0.4553040.455304 and 1.6050131.605013. Mean negative log likelihood is 1.8920711.892071 nats; doubling the fitted scale worsens it to 2.2102182.210218. Without the volume term, the apparent objective improves from 1.4189391.418939 to 1.0439391.043939. These are generated teaching data, not a real-world performance result.

The optional notebook reproduces this fit, the reflected triangular density and the new coupling calculation. It uses only Python's standard library, with no downloads, and is not required for progress.

Exact density does not mean that training found the true distribution. Check held-out fit and numerical conditioning. Discrete pixels also need an explicit dequantization/preprocessing model before interpreting continuous density as discrete likelihood; the longer note derives that distinction. Flows use invertible maps with a normalized base density. Next, Sampling unnormalized densities asks how to sample when we can evaluate an energy but cannot normalize the density.

Sources

Adapted with Codex assistance from the reviewed English and Russian notes at 82b6cb0. The reflection, smooth-map and Gaussian-fit examples follow that reviewed first-party source; questions and the coupling arithmetic are newly composed. No external dataset, model weight, source figure or third-party notebook is imported.

Scientific references: Papamakarios et al. (2021), §§2.1–2.2, 3.1 and 5.5 for change of variables, coupling and support restrictions; Rezende and Mohamed (2015) for normalizing flows in variational inference. Earlier conceptual credits remain in the pinned longer note. These are factual references, not an inferred license to copy their teaching assets.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Density and volume in a flowoptional

    Fit the reviewed synthetic affine flow, compare complete and missing-Jacobian objectives, then check reflection and a new two-coordinate coupling.

    ~20 min · easier on a computer · Read the matching topic. Python standard library only; no downloads.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.