NormalizingFlow class implements a normalizing flow for a random variable X towards a base distribution p(Z) through an invertible transformation f.
Mathematical Formulation
The density of a realization x is given by the change of variables formula: To sample from p(X), realizations z ~ p(Z) are mapped through the inverse transformation g = f⁻¹.The log absolute determinant of the Jacobian (LADJ) is computed automatically through the transform’s
call_and_ladj method, enabling efficient density evaluation.Constructor
An invertible transformation f that maps between the target and base spaces. Must implement
inv() for the inverse transformation and call_and_ladj() for computing both the forward transformation and its log absolute determinant Jacobian.The base distribution p(Z) in the latent space. Samples from this distribution are transformed to the target space. The distribution is automatically wrapped in
Independent if needed to match event dimensions.Properties
Returns the batch shape of the base distribution. This determines how many independent distributions are represented in a single object.
Returns the event shape after applying the inverse transform to the base distribution’s event shape. This is the shape of a single sample.
Always
True. The distribution supports reparameterized sampling for gradient-based optimization.Methods
log_prob
x- Sample points at which to evaluate the log probability
- Log probability density values with shape matching the batch shape
- Applies the forward transform: z = f(x)
- Computes the log absolute determinant Jacobian (LADJ)
- Returns: log p(z) + LADJ
rsample
shape- Desired sample shape (prepended to batch_shape + event_shape)
- Samples with shape
shape + batch_shape + event_shape
- Samples z from the base distribution
- Applies the inverse transform: x = f⁻¹(z)
- Returns x
rsample_and_log_prob
shape- Desired sample shape
- Tuple of (samples, log_probabilities)
- More efficient than calling
rsample()andlog_prob()separately - Reuses intermediate computations from the inverse transform
expand
batch_shape- Target batch shapenew- Optional instance to populate (internal use)
- Expanded distribution instance
Examples
Basic Usage
With Custom Transforms
Composed Flows
Batched Distributions
References
Normalizing flows enable flexible density estimation by transforming simple base distributions into complex target distributions while maintaining tractable likelihoods.
- Tabak, E. G., & Turner, C. V. (2013). A Family of Non-parametric Density Estimation Algorithms. Communications on Pure and Applied Mathematics. Link
- Rezende, D., & Mohamed, S. (2015). Variational Inference with Normalizing Flows. ICML 2015. arXiv:1505.05770
- Papamakarios, G., et al. (2021). Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research. arXiv:1912.02762
See Also
- Transforms - Available transformations for building flows
- Flows - High-level flow architectures
- Special Distributions - Other distribution types
