cjd_flows.models¶
High-level model builders.
USFlow() assembles a uniformly scaling (constant-Jacobian-determinant)
flow from the library’s building blocks:
from cjd_flows.models import USFlow
flow = USFlow((2,), blocks=8, base="ggmix") # 2-D density
flow = USFlow((1, 28, 28), blocks=6, base="ggmix") # images (ConvNet conditioners)
Every flow has the structure
[block_1, …, block_n, final_linear, Shift, GlobalScale]
with block_i = [mix_i, coupling_i] or, with conjugate=True,
block_i = [mix_i, coupling_i, mix_i^{-1}] (the “adjoint” blocks
A^{-1} ∘ C ∘ A of VeriFlow / USFlows). The Shift centres the
latents (data-initialized). Only the final linear layer and the global scale
change volume, so log|det J| is a single learnable constant.
- cjd_flows.models.USFlow(event_shape, *, blocks=8, coupling='additive', conditioner='auto', hidden=(256, 256), activation='relu', mixing='lu', conjugate=False, final_linear=True, shift='coordinate', global_scale=True, base='gaussian', p=2.0, n_components=4, context_dim=0, masked=False, conditioner_kwargs=None)[source]¶
Builds a uniformly scaling flow.
- Parameters:
event_shape (
Union[int,Sequence[int],Size]) – data shape,(D,)for vectors or(C, *spatial).blocks (
int) – number of coupling blocks.coupling (
str) –"additive"(NICE-style, piecewise affine with ReLU conditioners),"gin"(affine with zero-sum log-scale; more expressive per block, not piecewise affine) or"autoregressive"(shift-only MADE; vectors only).conditioner (
Union[str,Callable]) –"auto"(MLP for vectors, ConvNet for images),"mlp","conv","jet"or a factoryf(shape_in, shape_out) -> nn.Module.hidden (
Sequence[int]) – hidden widths/channels of the conditioners.activation (
str) – conditioner nonlinearity (relu/leaky_relukeep the flow piecewise affine).mixing (
Optional[str]) – linear layer before every coupling:"lu","sl-lu"(unit determinant),"orthogonal","householder","permutation","actnorm"orNone.conjugate (
bool) – use adjoint blocksA^{-1} C A(requiresmixing).final_linear (
bool) – append a free LU layer at the end.shift (
Optional[str]) – append a learnable, data-initializedShiftbefore the global scale:"coordinate"(one offset per latent entry),"channel"orNone.global_scale (
bool) – append a learnable scalar scale (the “uniform scaling”).base (
Union[str,BaseDistribution]) – base distribution name or instance, seemake_base().p (
float) – norm of the radial base.n_components (
int) – mixture components of the base (if applicable).context_dim (
int) – dimension of an optional conditioning vector.masked (
bool) – use masked (instead of split) additive couplings – the original USFlows formulation. Split couplings are cheaper.conditioner_kwargs (
Optional[dict]) – extra keyword arguments for the conditioner.
- Return type:
- cjd_flows.models.VeriFlow(event_shape, *, blocks=5, hidden=(128, 128, 128), base='ggmix-monotone', p=1.0, **kwargs)[source]¶
Verification-friendly preset: piecewise-affine (ReLU) conditioners, masked additive couplings in adjoint LU blocks, and an L1-radial base with a monotone learnable profile, so latent density level sets are L1 balls (polytopes) and their pre-images are exactly encodable in MILP/SMT.
- cjd_flows.models.make_base(base, event_shape, p=2.0, n_components=4, learn_scale=False)[source]¶
Creates a base distribution by name.
- Parameters:
base (
Union[str,BaseDistribution]) –one of
BASESor aBaseDistributioninstance."gaussian"is a fixed standard normal; all other names give an Lp-radial distribution (with normp) whose 1-D norm distribution is learnable:"laplace"– fixedp-generalized normal (Laplace for p=1),"gg"/"gg-monotone"– generalized gamma (monotone tiesa=d),"ggmix"/"ggmix-monotone"– mixture of generalized gammas,"spline"– rational-quadratic spline on the log-radius,"monotone"– monotone (radially decreasing) profile,"khinchine"– mixture of uniform balls (monotone by construction),"student-t"– heavy-tailed radial t,"lognormal-mix"– log-normal mixture (shell profile, not monotone),"radial-mixture"– mixture of radial components with learnable centres (multi-modal latent).
event_shape (
Union[int,Sequence[int],Size]) – latent event shape.p (
float) – Lp norm exponent of the radial base.n_components (
int) – number of mixture components (*mix/radial-mixture).learn_scale (
bool) – learn the global radial scale. Defaults toFalsebecause the flow’sGlobalScalealready covers it.
- Return type: