gensbi.normalizing_flows.bijections.transformers#
Elementwise transformers parameterised per-dimension by a conditioner.
Pure functions of (value, params) — no learnable state of their own.
Classes#
Functions#
|
Clamp with a straight-through gradient (NumPyro IAF trick). |
Inverse of softplus: |
|
|
Apply the RQ spline (or its inverse) to a scalar |
Module Contents#
- class gensbi.normalizing_flows.bijections.transformers.Affine(clamp_min=-5.0, clamp_max=3.0)[source]#
Elementwise affine transform with log-scale clamping.
The parameter layout per dimension is
[shift mu, log-scale a](num_params == 2). Log-scale values are clamped to[clamp_min, clamp_max]before exponentiation using a straight-through gradient (NumPyro IAF trick).Forward (noise→data):
x = u * exp(a) + mu; logabsdet =+sum(a). Inverse (data→noise):u = (x - mu) * exp(-a); logabsdet =-sum(a).- Parameters:
clamp_min (float, optional) – Lower bound for log-scale clamping. Default is -5.0.
clamp_max (float, optional) – Upper bound for log-scale clamping. Default is 3.0.
- forward(u, params)[source]#
Map noise to data:
x = u * exp(a) + mu.- Parameters:
u (Array) – Noise-space input of shape
(dim,).params (Array) – Per-dimension parameters of shape
(dim, 2); column 0 is the shiftmuand column 1 is the log-scalea.
- Returns:
x (Array) – Data-space output.
logabsdet (Array) – Sum of clamped log-scales:
sum(a).
- Return type:
tuple[jax.Array, jax.Array]
- forward_dim(u_i, params_i)[source]#
Scalar noise-to-data transform for a single dimension.
Used by the sequential sampling scan in the autoregressive loop, which accumulates the per-dimension log-determinant as it goes.
- Parameters:
u_i (Array) – Scalar noise-space value for one dimension.
params_i (Array) – Parameter vector of length 2 for the same dimension; index 0 is the shift
mu, index 1 is the log-scalea.
- Returns:
x_i (Array) – Scalar data-space output:
u_i * exp(a) + mu.logabsdet_i (Array) – Forward log-determinant contribution for this dimension:
a.
- Return type:
tuple[jax.Array, jax.Array]
- inverse(x, params)[source]#
Map data to noise:
u = (x - mu) * exp(-a).- Parameters:
x (Array) – Data-space input of shape
(dim,).params (Array) – Per-dimension parameters of shape
(dim, 2); column 0 is the shiftmuand column 1 is the log-scalea.
- Returns:
u (Array) – Noise-space output.
logabsdet (Array) – Negative sum of clamped log-scales:
-sum(a).
- Return type:
tuple[jax.Array, jax.Array]
- class gensbi.normalizing_flows.bijections.transformers.RQSpline(num_bins=8, range_bound=5.0, min_bin_width=0.001, min_bin_height=0.001, min_derivative=0.001)[source]#
Elementwise monotonic rational-quadratic spline on
[-B, B].Linear (identity) tails outside the spline interval. The mapping is parameterised per dimension; with zero-initialised parameters the spline reduces to the identity, so the flow warm-starts as a standard normal (same warm-start contract as
Affine).The parameter layout per dimension is
[widths(K), heights(K), inner_derivatives(K-1)]of total length3K - 1. Reference: Durkan et al. 2019 (https://arxiv.org/abs/1906.04032).- Parameters:
num_bins (int, optional) – Number of spline bins
K. Default is 8.range_bound (float, optional) – Half-width
Bof the spline interval[-B, B]. Default is 5.0.min_bin_width (float, optional) – Minimum fractional bin width after softmax normalisation. Default is 1e-3.
min_bin_height (float, optional) – Minimum fractional bin height after softmax normalisation. Default is 1e-3.
min_derivative (float, optional) – Minimum knot derivative value. Default is 1e-3.
- _knots(params)[source]#
Raw params -> (x_knots, y_knots, derivatives), each over K+1 knots.
- Parameters:
params (jax.Array)
- forward(u, params)[source]#
Map noise to data (vectorised inverse spline pass).
- Parameters:
u (Array) – Noise-space input of shape
(dim,).params (Array) – Per-dimension spline parameters of shape
(dim, 3K-1).
- Returns:
x (Array) – Data-space output.
logabsdet (Array) – Negative sum of log-derivatives of the spline:
-sum(log g'(x)), whereg'is the forward spline derivative evaluated at the corresponding data-space position.
- forward_dim(u_i, params_i)[source]#
Scalar noise-to-data transform for a single dimension.
Used by the sequential sampling scan in the autoregressive loop, which accumulates the per-dimension log-determinant as it goes.
- Parameters:
u_i (Array) – Scalar noise-space value for one dimension.
params_i (Array) – Spline parameter vector of length
3K-1for the same dimension.
- Returns:
x_i (Array) – Scalar data-space output.
logabsdet_i (Array) – Forward log-determinant contribution for this dimension:
-log g'(x_i)(matchingforward()).
- Return type:
tuple[jax.Array, jax.Array]
- inverse(x, params)[source]#
Map data to noise (fast vectorised spline pass).
- Parameters:
x (Array) – Data-space input of shape
(dim,).params (Array) – Per-dimension spline parameters of shape
(dim, 3K-1).
- Returns:
u (Array) – Noise-space output.
logabsdet (Array) – Positive sum of log-derivatives of the spline at each input:
+sum(log g'(x)), whereg'is the forward spline derivative (data→noise direction).
- gensbi.normalizing_flows.bijections.transformers._clamp(a, lo, hi)[source]#
Clamp with a straight-through gradient (NumPyro IAF trick).
- Parameters:
a (jax.Array)
lo (float)
hi (float)
- Return type:
jax.Array
- gensbi.normalizing_flows.bijections.transformers._inv_softplus(y)[source]#
Inverse of softplus:
xsuch thatsoftplus(x) == y(y > 0).- Parameters:
y (jax.Array)
- Return type:
jax.Array
- gensbi.normalizing_flows.bijections.transformers._rqs_apply(z, x_knots, y_knots, derivatives, inverse)[source]#
Apply the RQ spline (or its inverse) to a scalar
z.Returns
(out, logderiv)wherelogderiv = log(dy/dx)evaluated at the relevant x (the same forward derivative is used for both directions; the caller flips its sign forforward). Outside[-B, B]the map is the identity (logderiv 0).- Parameters:
z (jax.Array)
x_knots (jax.Array)
y_knots (jax.Array)
derivatives (jax.Array)
inverse (bool)