Flows¶
A normalizing flow pushes a simple
distribution through a sequence of invertible maps, and scores a value by the base density times
the inverse maps' Jacobian determinants (Rezende and Mohamed, 2015)
(normalizing flows). ConditionalFlow is a
conditional flow built from AffineCouplingLayers (Dinh et al., 2017).
flows
¶
Normalizing flows as continuous morphisms.
A normalizing flow defines a bijective map from a simple base distribution (standard normal) to a complex target distribution. The map is composed of invertible layers with tractable Jacobians, enabling exact log-density computation via the change-of-variables formula.
For conditional flows, each layer's parameters depend on the conditioning input x, making the flow a ContinuousMorphism:
p(y | x) = p_base(f^{-1}(y; x)) * |det df^{-1}/dy|
This module provides:
AffineCouplingLayer — single invertible affine coupling layer
ConditionalFlow — stack of coupling layers as a ContinuousMorphism
AffineCouplingLayer
¶
AffineCouplingLayer(domain: AnySpace, dim: int, mask_even: bool = True, hidden_dim: int = 64)
Bases: Module
Single affine coupling layer (RealNVP-style).
Splits the input z into two halves (z_a, z_b). One half passes through unchanged while the other is affinely transformed based on the first half (and the conditioning variable x):
z_a' = z_a (unchanged)
z_b' = z_b * exp(s(x, z_a)) + t(x, z_a) (transformed)
The Jacobian is triangular, so its log-determinant is sum(s(x, z_a)).
| PARAMETER | DESCRIPTION |
|---|---|
domain
|
Conditioning space.
TYPE:
|
dim
|
Total dimensionality of z.
TYPE:
|
mask_even
|
If True, z_a = even indices, z_b = odd indices. If False, reversed.
TYPE:
|
hidden_dim
|
Hidden layer width for the scale/shift network.
TYPE:
|
Source code in src/quivers/continuous/flows.py
forward
¶
Forward pass: base -> target.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Conditioning input.
TYPE:
|
z
|
Input vector. Shape (batch, dim).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
z_out
|
Transformed vector. Shape (batch, dim).
TYPE:
|
log_det
|
Log-determinant of the Jacobian. Shape (batch,).
TYPE:
|
Source code in src/quivers/continuous/flows.py
inverse
¶
Inverse pass: target -> base.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Conditioning input.
TYPE:
|
z_out
|
Transformed vector. Shape (batch, dim).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
z
|
Original vector. Shape (batch, dim).
TYPE:
|
log_det
|
Log-determinant (negative of forward). Shape (batch,).
TYPE:
|
Source code in src/quivers/continuous/flows.py
ConditionalFlow
¶
Bases: ContinuousMorphism
Conditional normalizing flow as a continuous morphism.
Stacks affine coupling layers to form an invertible transformation from a standard normal base to the target distribution, conditioned on input x.
The flow supports exact log-density computation:
log p(y | x) = log N(f^{-1}(y; x); 0, I)
+ sum_k log |det df_k^{-1}/dz_k|
Sampling uses:
z ~ N(0, I)
y = f_K(... f_2(f_1(z; x); x) ...; x)
| PARAMETER | DESCRIPTION |
|---|---|
domain
|
Conditioning space.
TYPE:
|
codomain
|
Target continuous space.
TYPE:
|
n_layers
|
Number of coupling layers.
TYPE:
|
hidden_dim
|
Hidden layer width for scale/shift networks.
TYPE:
|
Examples:
>>> from quivers import FinSet
>>> from quivers.continuous.spaces import Euclidean
>>> A = FinSet(name="context", cardinality=10)
>>> Y = Euclidean(name="output", dim=4)
>>> flow = ConditionalFlow(A, Y, n_layers=6)
>>> x = torch.tensor([0, 1, 2])
>>> samples = flow.rsample(x) # shape (3, 4)
Source code in src/quivers/continuous/flows.py
log_prob
¶
Exact log-density via change of variables.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Conditioning inputs.
TYPE:
|
y
|
Target values. Shape (batch, d).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-densities. Shape (batch,). |
Source code in src/quivers/continuous/flows.py
rsample
¶
Sample via forward pass through the flow.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Conditioning inputs.
TYPE:
|
sample_shape
|
Additional sample dimensions. |
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Samples. Shape (*sample_shape, batch, d). |
Source code in src/quivers/continuous/flows.py
Cited sources¶
Danilo Rezende and Shakir Mohamed. Variational Inference with Normalizing Flows. In Proceedings of the 32nd International Conference on Machine Learning, volume 37 of Proceedings of Machine Learning Research, 1530–1538. 2015. URL: https://proceedings.mlr.press/v37/rezende15.html.
Laurent Dinh, Jascha Sohl-Dickstein, and Samy Bengio. Density estimation using Real NVP. In International Conference on Learning Representations. 2017. URL: https://openreview.net/forum?id=HkpbnH9lx.