gensbi.models.maf#
Masked Autoregressive Flow (MAF) density model.
Provides MAFlow, a normalizing flow for exact log-density evaluation
and sampling built from stacked autoregressive layers, and its configuration
dataclass MAFlowParams.
Submodules#
Classes#
Masked Autoregressive Flow for exact density evaluation and sampling. |
|
Architecture parameters for |
Package Contents#
- class gensbi.models.maf.MAFlow(params)[source]#
Bases:
flax.nnx.ModuleMasked Autoregressive Flow for exact density evaluation and sampling.
Stacks
MaskedAutoregressivelayers separated by permutations, with an optional data-endStandardizebijection, over a standard-normal base distribution.Log-density follows the change-of-variables formula:
log_prob(x, cond) = base.log_prob(u) + logdet, whereu, logdet = chain.inverse(x, cond). The base distribution is built lazily and never entersnnxstate.- Parameters:
params (MAFlowParams) – Full architecture configuration; see
MAFlowParams.
- log_prob(x, cond=None)[source]#
Compute the change-of-variables log-density for a batch of samples.
- Parameters:
x (Array) – Data batch. Shape
(B, dim)whenchannels == 1, or(B, dim, C)whenchannels > 1(the channel axis is flattened internally to(B, dim * C)).cond (Array or None, optional) – Conditioning batch of shape
(B, cond_dim)forcond_channels == 1, or(B, cond_dim, C_cond)forcond_channels > 1(also flattened internally). PassNonefor an unconditional model.
- Returns:
Log-probability of shape
(B,).- Return type:
Array
- sample(key, cond=None, nsamples=None)[source]#
Draw samples from the flow.
- Parameters:
key (jax.random.PRNGKey) – Random key.
cond (Array or None, optional) – Conditioning batch of shape
(nsamples, cond_dim)forcond_channels == 1, or(nsamples, cond_dim, C_cond)forcond_channels > 1(flattened internally). If provided, the number of samples is inferred fromcond.shape[0]andnsamplesis ignored.nsamples (int or None, optional) – Number of samples to draw. Required when
condisNone.
- Returns:
Sample array of shape
(nsamples, dim, channels)for allC >= 1(C = 1gives(nsamples, dim, 1); channel axis is never collapsed).- Return type:
Array
- set_standardization(mean, std)[source]#
Set the data-end Standardize bijection’s mean/std buffers in place.
Accepts shapes
(dim,)(broadcast to(dim, 1)),(dim, 1),(C,)(per-channel broadcast), or a scalar broadcastable to(dim, channels).Raises ValueError if built with
standardize=False.- Return type:
None
- chain#
- channels#
- cond_channels#
- cond_dim#
- dim#
- flat_dim#
- class gensbi.models.maf.MAFlowParams[source]#
Architecture parameters for
MAFlow.Only
rngsanddimare required.transformerdefaults toAffine()(passRQSpline()for a spline flow).- Parameters:
rngs (nnx.Rngs) – Flax RNG container used to initialise all trainable parameters.
dim (int) – Dimensionality of the target variable.
cond_dim (int, optional) – Dimensionality of the conditioning input. Default is 0 (unconditional).
n_layers (int, optional) – Number of
MaskedAutoregressivelayers. Default is 5.transformer (Bijection or None, optional) – Elementwise bijection used by each autoregressive layer. If
None(default), anAffinebijection is constructed automatically in__post_init__.nn_width (int, optional) – Width of each hidden layer in the MADE conditioner network. Default is 64.
nn_depth (int, optional) – Number of hidden layers in the MADE conditioner network. Default is 2.
permutation (str, optional) – Permutation applied between autoregressive layers.
"reverse"(default) reverses the dimension ordering;"random"applies a random shuffle sampled at construction time.standardize (bool, optional) – If
True(default), append aStandardizebijection at the data end of the chain.zero_init (bool, optional) – If
True(default), zero-initialise the output layer of each MADE network so the flow starts as an identity transform.param_dtype (DTypeLike, optional) – Dtype for all stored (master) MADE kernel/bias parameters. Default is
float32.dtype (DTypeLike, optional) – Compute dtype knob threaded through the MADE conditioners. Default is
float32(unlike the bf16-default DiT-family models, MAF keeps fp32 compute by default pending dedicated stability testing — see the mixed-precision design spec). Log-det accumulation is unconditionally fp32 regardless of this knob.
- channels: int = 1#
- cond_channels: int = 1#
- cond_dim: int = 0#
- dim: int#
- dtype: jax.typing.DTypeLike#
- n_layers: int = 5#
- nn_depth: int = 2#
- nn_width: int = 64#
- param_dtype: jax.typing.DTypeLike#
- permutation: str = 'reverse'#
- rngs: flax.nnx.Rngs#
- standardize: bool = True#
- transformer: gensbi.normalizing_flows.bijections.base.Bijection | None = None#
- zero_init: bool = True#