ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Normalizing Flows
Chapter 1: Mathematics of Invertible Transformations
Random Variables and Distributions
The Change of Variables Theorem
Computing Jacobian Determinants
Forward and Inverse Passes
Calculating Jacobians Practice
Chapter 2: Building Normalizing Flows
Defining a Normalizing Flow
Stacking Transformations
Planar and Radial Flows
Choosing Base Distributions
Implementing Planar Flows Practice
Chapter 3: Autoregressive Flows
Autoregressive Generative Models
Masked Autoencoders for Distributions
Masked Autoregressive Flow
Inverse Autoregressive Flow
Building Autoregressive Models Practice
Chapter 4: Coupling Architectures
Affine Coupling Layers
The RealNVP Architecture
Multi-Scale Architectures
ActNorm and Invertible Convolutions
Implementing a Coupling Layer Practice
Chapter 5: Training Flow Models
Maximum Likelihood Estimation
Loss Functions for Density Estimation
Data Dequantization Methods
Sampling Procedures
Training a Normalizing Flow Practice

The Change of Variables Theorem

To generate complex data distributions from simple ones, normalizing flows rely on a specific mathematical rule. When we pass a random variable through a mathematical function, its probability density changes. The change of variables theorem tells us exactly how to calculate that new density. This theorem is the mathematical engine that drives all normalizing flow architectures.

We can build an understanding of this theorem by starting with a single dimension. Imagine a simple one-dimensional continuous random variable zzz. Let us assume zzz follows a standard normal distribution. We want to apply an invertible function fff to zzz to create a new random variable xxx, resulting in the equation x=f(z)x = f(z)x=f(z). Because fff is an invertible mapping, we can also map backward using the inverse function z=f−1(x)z = f^{-1}(x)z=f−1(x).

What is the probability density of our new variable, denoted as px(x)p_x(x)px​(x)? A common mistake is to assume that px(x)p_x(x)px​(x) is simply equal to the base density pz(z)p_z(z)pz​(z). However, probability is determined by the area under the density curve, and the total area must always integrate to 1. When the function fff stretches or squishes the number line, the probability density must adjust accordingly to conserve total probability.

The conservation of probability dictates that the probability mass in a small region dzdzdz must equal the probability mass in the corresponding transformed region dxdxdx:

px(x)∣dx∣=pz(z)∣dz∣p_x(x) |dx| = p_z(z) |dz|px​(x)∣dx∣=pz​(z)∣dz∣

By rearranging this equation, we get the change of variables formula for a single dimension:

px(x)=pz(z)∣dzdx∣p_x(x) = p_z(z) \left| \frac{dz}{dx} \right|px​(x)=pz​(z)​dxdz​​

The term dzdx\frac{dz}{dx}dxdz​ is the derivative of the inverse function f−1(x)f^{-1}(x)f−1(x) with respect to xxx. We take the absolute value because probability densities must be strictly non-negative. Without the absolute value, a function with a negative slope would compute a mathematically impossible negative probability density.

Flow of a random variable through an invertible transformation showing forward and inverse mappings.

In machine learning, we rarely work with one-dimensional data. We deal with high-dimensional vectors representing images, audio, or text. If z\mathbf{z}z and x\mathbf{x}x are vectors in an NNN-dimensional space RN\mathbb{R}^NRN, the simple scalar derivative dzdx\frac{dz}{dx}dxdz​ is no longer sufficient. We must evaluate how every dimension of z\mathbf{z}z changes with respect to every dimension of x\mathbf{x}x. This multidimensional relationship is captured by a matrix of partial derivatives called the Jacobian matrix.

The change of variables theorem for multivariate distributions replaces the scalar derivative with the determinant of the Jacobian matrix:

px(x)=pz(z)∣det⁡(∂z∂x)∣p_x(\mathbf{x}) = p_z(\mathbf{z}) \left| \det \left( \frac{\partial \mathbf{z}}{\partial \mathbf{x}} \right) \right|px​(x)=pz​(z)​det(∂x∂z​)​

We can also write this equation explicitly using the inverse function z=f−1(x)\mathbf{z} = f^{-1}(\mathbf{x})z=f−1(x):

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

We can break down the components of this formula to understand how density estimation works in practice.

  1. px(x)p_x(\mathbf{x})px​(x) is the exact probability density of our data that we want to evaluate.
  2. pz(f−1(x))p_z(f^{-1}(\mathbf{x}))pz​(f−1(x)) is the probability density of the base distribution. We evaluate it by passing the data point x\mathbf{x}x backward through the inverse function to get z\mathbf{z}z.
  3. ∂f−1(x)∂x\frac{\partial f^{-1}(\mathbf{x})}{\partial \mathbf{x}}∂x∂f−1(x)​ is the Jacobian matrix. It tracks the rate of change of the inverse transformation across all dimensions.
  4. det⁡\detdet is the determinant operator. It measures the volume expansion or contraction of the multidimensional space caused by the transformation.
  5. The absolute value ensures the final density remains positive.

For this theorem to hold true, the function fff must be a bijection. A bijection is a mathematical function that is both one-to-one and onto. This strict requirement guarantees two specific properties. First, every vector z\mathbf{z}z maps to exactly one vector x\mathbf{x}x, and every x\mathbf{x}x maps back to exactly one z\mathbf{z}z. Second, the input and output dimensions are identical, which ensures the Jacobian is a square matrix. Determinants can only be computed for square matrices. If the function is not a bijection, probability mass could overlap or disappear.

Sometimes it is more convenient to define the change of variables using the forward transformation rather than the inverse. By applying the properties of inverse matrices, we know that the determinant of the inverse Jacobian is equal to the inverse of the determinant of the forward Jacobian. This gives us an alternative representation:

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

When training neural networks, multiplying many small probabilities together leads to numerical underflow. To maintain numerical stability, normalizing flows compute the log-likelihood of the data instead of the raw probability density. Taking the natural logarithm of both sides transforms the product into a sum:

log⁡px(x)=log⁡pz(z)−log⁡∣det⁡(∂x∂z)∣\log p_x(\mathbf{x}) = \log p_z(\mathbf{z}) - \log \left| \det \left( \frac{\partial \mathbf{x}}{\partial \mathbf{z}} \right) \right|logpx​(x)=logpz​(z)−log​det(∂z∂x​)​

This logarithmic form is the exact objective function you will optimize when training flow models in PyTorch. The first term evaluates how well the model maps data to high-probability regions of the simple base distribution. The second term, the log determinant, acts as a penalty that prevents the volume of the space from collapsing to zero during training.

© 2026 ApX Machine Learning