cjd_flows.core¶
Core abstractions of CJD-Flows.
The central invariant of the library is that every bijection has a constant
Jacobian determinant, i.e. log|det J_f(x)| does not depend on x (nor on
an optional context). The contract therefore separates the map from its
log-determinant:
Bijection.forward(x, context=None) -> z(normalizing direction: data -> latent)Bijection.inverse(z, context=None) -> x(generative direction: latent -> data)Bijection.log_abs_det() -> Tensor(0-dim tensor, depends only on parameters)
so that a flow’s log-density is
log p(x) = log p_Z(f(x)) + log|det J_f|
where the second summand is a single scalar shared by all inputs. Consequently, density level sets in data space are exactly the pre-images of density level sets of the base distribution (“density level-set preservation”).
Conventions¶
Tensors have exactly one leading batch dimension followed by the event dimensions, i.e. shape
(B, *event_shape). Vector data hasevent_shape == (D,); image-like data has(C, H, W)(any number of spatial dimensions is allowed). The channel dimension is always dim1.Bijections are plain
torch.nn.Moduleobjects without hidden caches, so they work withtorch.compile,state_dict,.to(),.double(), DDP, etc.
- class cjd_flows.core.Bijection[source]¶
Bases:
ModuleBase class for all bijections with constant Jacobian determinant.
Subclasses implement
forward(),inverse()and (if the determinant is not one)log_abs_det().- piecewise_affine¶
Trueif the map is piecewise affine provided its conditioner networks are piecewise affine (e.g. ReLU MLPs/CNNs). Such flows admit exact SMT/MILP encodings.- Type:
- forward(x, context=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- log_abs_det()[source]¶
Constant
log|det J_f|of the forward (normalizing) map as a 0-dim tensor.- Return type:
- class cjd_flows.core.VolumePreserving[source]¶
Bases:
BijectionMarker base class for bijections with
|det J| == 1.
- class cjd_flows.core.InverseBijection[source]¶
Bases:
BijectionSwaps the directions of a bijection.
- __init__(bijection)[source]¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- Parameters:
bijection (Bijection)
- forward(x, context=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class cjd_flows.core.Chain[source]¶
Bases:
BijectionComposition
f = f_n ∘ ... ∘ f_1(applied in list order in the forward direction).- __init__(*bijections)[source]¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(x, context=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class cjd_flows.core.Identity[source]¶
Bases:
VolumePreserving- forward(x, context=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class cjd_flows.core.BaseDistribution[source]¶
Bases:
ModuleInterface for (learnable) base distributions.
Subclasses provide
event_shape,log_prob(z) -> (B,)andsample(sample_shape) -> (*sample_shape, *event_shape).- forward(z)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class cjd_flows.core.StandardNormal[source]¶
Bases:
BaseDistributionIsotropic standard normal base distribution (non-learnable).
- class cjd_flows.core.Flow[source]¶
Bases:
ModuleA normalizing flow with constant Jacobian determinant.
- Parameters:
bijection (
Union[Bijection,Sequence[Bijection]]) – normalizing mapf(data -> latent). Lists are wrapped in aChain.base (
BaseDistribution) – base distributionp_Zover the latent space.context_dim (
int) – dimension of an optional context vector passed to all conditioners (e.g. class embeddings or the soft-training noise scale).default_context (
Optional[float]) – value used whencontextis omitted butcontext_dim > 0(0.0for soft training, where sigma=0 is the clean data distribution).event_shape (
Union[int,Sequence[int],Size,None]) – shape of a single data point. Defaults to theevent_shapeof the first layer of the bijection (e.g. the input shape of a leadingSqueeze) and otherwise to the base distribution’s event shape. Pass it explicitly if the first layer does not declare its input shape but the bijection changes the shape.
The model computes
log p(x) = log p_Z(f(x)) + log|det J_f|.- __init__(bijection, base, context_dim=0, default_context=0.0, event_shape=None)[source]¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- log_prob(x, context=None)[source]¶
Exact log-density
log p(x)of shape(B,).Inputs with more than one leading batch dimension are flattened and reshaped back.
- forward(x, context=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- latent_log_prob(x, context=None)[source]¶
log p_Z(f(x))– equalslog p(x)up to the constantlog|det J_f|.
- rsample(sample_shape=(), context=None)[source]¶
Like
sample()but differentiable w.r.t. the flow parameters.
- fit(*args, **kwargs)[source]¶
Trains the flow; see
cjd_flows.train.fit().