This page picks up where the READMEs stop. The workspace README fits a Gaussianization flow and reads off its entropy, the gauss-flows-maps README checks one conditioned layer, and the gauss-flows-models README compares the closed-form RBIG fit with maximum likelihood. Here we cover the calling convention every layer shares, a flow assembled by hand from the layers of gauss-flows-maps, and where to go for NumPyro.
One event at a time¶
Every layer acts on a single event. For a layer with event shape , transform_and_log_det takes one and returns with the scalar ; inverse_and_log_det does the same in the other direction, and the two log-determinants cancel at matching points. A batch is a jax.vmap over the event, never a leading axis the layer handles itself, which is the convention of flowjax.
import jax
import jax.numpy as jnp
import jax.random as jr
from gauss_flows_maps.bijections import MixtureGaussianCDF
# Shapes: B = 32 events; D = 3 dims; K = 8 mixture components
# today (gauss-flows-maps)
# G(x)_d = Φ⁻¹(F_d(x_d)), F_d a K-component Gaussian-mixture CDF
marginal = MixtureGaussianCDF(n_components=8, shape=(3,))
x = jnp.array([0.5, -1.2, 2.0])
# today (gauss-flows-maps)
# z = G(x) and log |det J_G(x)|
# (D,) → (D,), ()
z, log_det = marginal.transform_and_log_det(x)
batch = jr.normal(jr.key(0), (32, 3))
# the same layer on every event of the batch
# (B, D) → (B, D), (B,)
zs, log_dets = jax.vmap(marginal.transform_and_log_det)(batch)
print(z.shape, log_det.shape, zs.shape, log_dets.shape)This prints (3,) () (32, 3) (32,): one event gives one log-determinant, and the batch gives one per event.
A layer’s forward direction is the one it was written for, and a marginal CDF Gaussianizes going forward. A flowjax distribution samples in the forward direction of its bijection and scores densities in the inverse one, so a flow from data to a Gaussian is wrapped in Invert before it becomes a distribution, as the next section does.
A flow from the layers of gauss-flows-maps¶
gaussianization_flow builds the map in one call, where each is a marginal step and each a rotation. The same flow can be assembled by hand, which is the way to swap one of its layers for another of gauss-flows-maps (a spline marginal, an LU rotation, a coupling). We start each mixture with its means spread over and each rotation from random reflections, since layers that all start alike receive the same gradient, and fit it to 5,000 samples of the mixed Laplace sources of the READMEs.
import einx
import equinox as eqx
import jax.numpy as jnp
import jax.random as jr
from flowjax.bijections import Chain, Invert
from flowjax.distributions import Normal, Transformed
from gauss_flows_maps.bijections import HouseholderRotation, MixtureGaussianCDF
from gauss_flows_models.inference import fit_gaussianization_flow
# Shapes: N = 5,000 samples; D = 3 dims; K = 8 mixture components; L = 4 blocks
data_key, flow_key, fit_key = jr.split(jr.key(0), 3)
# user
# one block R ∘ G, with spread-out mixture means and random reflections
def make_block(key):
means_key, householder_key = jr.split(key)
# today (gauss-flows-maps)
marginal = MixtureGaussianCDF(n_components=8, shape=(3,))
means = jr.uniform(means_key, (3, 8), minval=-3, maxval=3)
marginal = eqx.tree_at(lambda m: m.means, marginal, means)
# today (gauss-flows-maps)
rotation = HouseholderRotation(n_reflections=3, shape=(3,))
reflections = jr.normal(householder_key, rotation.params.shape)
rotation = eqx.tree_at(lambda r: r.params, rotation, reflections)
return Chain([marginal, rotation])
# T = R_L ∘ G_L ∘ ⋯ ∘ R_1 ∘ G_1, from the data to the latent
blocks = [make_block(key) for key in jr.split(flow_key, 4)]
# p_θ(x) = 𝒩(T(x) | 0, I) |det J_T(x)|, sampled through T⁻¹
flow = Transformed(Normal(jnp.zeros(3)), Invert(Chain(blocks)))
# x_n = W s_n, with s_nd ~ Laplace(0, 1)
# → (N, D)
sources = jr.laplace(data_key, (5000, 3))
mixing = jnp.array([[1.0, 0.6, 0.2], [0.0, 1.0, 0.5], [0.3, 0.0, 1.0]])
samples = einx.dot("sample source, dim source -> sample dim", sources, mixing)
# today (gauss-flows-models)
# θ̂ = argmax_θ Σ_n log p_θ(x_n), with a tenth of the samples held out for early stopping
flow, losses = fit_gaussianization_flow(
fit_key, flow, samples, learning_rate=5e-3, show_progress=False
)
print(f"{min(losses['val']):.3f}")This prints 5.252, the negative log-likelihood per sample on the held-out tenth at the best epoch, whose parameters the fit returns, in nats. The exact entropy of these samples is 5.109, and the held-out negative log-likelihood exceeds it by the KL divergence from the data to the fit, here 0.14 nats. gaussianization_flow stacks its blocks with flowjax’s Scan instead of a Python list, so its depth costs one compiled block; a hand-built flow of many identical blocks can do the same with eqx.filter_vmap over make_block and flowjax.bijections.Scan.
A closed-form start¶
fit_rbig fits the same kind of flow with no gradients: in each block a PCA of the current samples gives the rotation and a Gaussian mixture fitted to each coordinate by EM gives the marginal step, and the samples move through the block before the next one is fitted. The result is a flowjax distribution, so fit_gaussianization_flow can refine it; the gauss-flows-models README compares the two fits, and the RBIG warm-start tutorial shows them on a 2-D density.
Flows in NumPyro¶
gauss_flows does not import NumPyro. A flow as a NumPyro distribution, a NumPyro distribution as a flow’s base, and a flow as a variational guide live in pyrox-flows.