cjd_flows.testing

Public test utilities.

Use these helpers to verify custom bijections (e.g. with your own conditioner) against the library contract:

from cjd_flows.testing import check_bijection
check_bijection(my_layer, torch.randn(8, 4, 6, 6, dtype=torch.float64))
cjd_flows.testing.jacobian_log_abs_det(f, x)[source]

Per-sample log|det J_f(x)| via autograd (for small event sizes).

Parameters:
  • f – map from (B, *event) to (B, *event).

  • x (Tensor) – input batch.

Return type:

Tensor

Returns:

Tensor of shape (B,).

cjd_flows.testing.check_bijection(bijection, x, context=None, atol=None, check_jacobian=True, max_jacobian_dim=256)[source]

Asserts that bijection satisfies the CJD-Flows contract on input x.

Checks:
  1. shape preservation of forward and inverse,

  2. inverse(forward(x)) == x and forward(inverse(z)) == z,

  3. log_abs_det() is a finite 0-dim tensor,

  4. (optional) log_abs_det() equals the autograd log-determinant for every sample – which also verifies that it is constant,

  5. gradients w.r.t. all parameters are finite.

Use float64 inputs (and bijection.double()) for tight tolerances.

Return type:

None

Parameters:
cjd_flows.testing.check_flow_normalization(flow, n=200_000, radius=6.0)[source]

Monte-Carlo estimate of ∫ p(x) dx over a box (for 1D/2D flows).

Returns the estimate; a well-defined flow yields a value close to 1 if the box covers (almost) all the mass. The flow is evaluated in eval mode (so e.g. ActNorm is not data-initialized on the box samples); its training mode is restored afterwards.

Return type:

float

Parameters: