What are Normalizing Flows?
Normalizing flows are a powerful class of generative models that transform a simple base distribution (like a standard normal) into a complex target distribution through a series of invertible transformations. The key insight is that we can express complex probability distributions by warping simple ones.Mathematical Foundation
Given a random variable and a transformation , normalizing flows use the change of variables formula to compute the probability density: where:- is a simple base distribution (e.g., standard normal)
- is an invertible transformation
- The determinant term accounts for how the transformation warps space
The absolute value of the Jacobian determinant ensures the density remains non-negative, as it measures how volumes change under the transformation.
How Flows Enable Density Estimation
Normalizing flows enable efficient density estimation through two key operations:- Sampling: Draw from the base distribution and map through the inverse:
- Density evaluation: For any point , compute using the change of variables formula
- Fast sampling (forward direction)
- Exact likelihood computation (inverse direction)
The NormalizingFlow Class
Zuko implements normalizing flows through theNormalizingFlow distribution class, which wraps PyTorch’s Distribution interface.
Architecture
TheNormalizingFlow class (defined in zuko/distributions.py:39-139) connects a transformation with a base distribution:
The
NormalizingFlow class automatically handles dimension alignment between the transformation’s codomain and the base distribution’s event shape.Log Probability Computation
The log probability implements the change of variables formula:z = f(x)is the transformed valueladjis the log absolute determinant of the Jacobian- The sum combines base log probability with the volume correction
Sampling
Sampling uses the inverse transformation:The method uses
rsample (reparameterized sampling) when available to enable gradient-based optimization through the sampling process.Efficient Sampling with Log Probability
For training, you often need both samples and their log probabilities. Thersample_and_log_prob method computes both efficiently:
Practical Example
Here’s a complete example building a normalizing flow for 2D data:Composing Multiple Transformations
Real-world flows typically compose multiple transformations. Zuko’sComposedTransform enables this:
References
The implementation follows these foundational papers:- Tabak et al. (2013) - A Family of Non-parametric Density Estimation Algorithms
- Rezende & Mohamed (2015) - Variational Inference with Normalizing Flows
- Papamakarios et al. (2021) - Normalizing Flows for Probabilistic Modeling and Inference
Next Steps
Lazy Distributions
Learn how Zuko makes flows trainable with PyTorch
Transformations
Explore the transformation building blocks
