Bare RealNVP flow — multi-scale bijection plus the latent prior.
Entry points mirror the rest of the flow families:
encode(x) -> (z, log_det)— images to a flat(B, D)latent.decode(z) -> x— the exact inverse, back to(B, C, H, W).log_prob(x) -> (B,)— exact log-likelihood in nats.bits_per_dim(x) -> (B,)— the paper's reported metric.
Inputs live in [0, 1]: the logit squash that opens the flow is part
of the model, so feeding already-logit data double-counts it.
Parameters
configRealNVPConfigRealNVPConfig.Attributes
logitnn.Modulescalesnn.ModuleListnn.ModuleList of coupling (and
optional batch-norm) stages.Notes
Reference: Dinh, Sohl-Dickstein, and Bengio, "Density Estimation Using Real NVP", ICLR, 2017 (arXiv:1605.08803).
With batch normalisation enabled the forward pass uses batch statistics
while training, exactly as in the paper; decode always uses the
running estimates, so round-trip exactness is an eval()-mode
property.
Examples
>>> import lucid
>>> from lucid.models.generative.realnvp import RealNVPConfig, RealNVPModel
>>> cfg = RealNVPConfig(sample_size=8, num_scales=2, residual_blocks=1,
... base_dim=8)
>>> model = RealNVPModel(cfg).eval()
>>> x = lucid.rand((2, 3, 8, 8))
>>> z, log_det = model.encode(x)
>>> z.shape, log_det.shape
((2, 192), (2,))Used by 2
Constructors
1Properties
2Instance methods
5Per-sample bits/dim — the metric the paper reports in Table 1.
log_prob is the exact density over the model's own input
space, [0, 1]^D. The paper's number is measured against the
discrete 8-bit data, and the x / 256 inside its squash is
precisely that change of variables — worth
in nats, i.e. num_bits in bits/dim. The
canonical implementation applies the same offset in its loss.
Without it the reported figure sits a full 8 bits/dim below Table 1
and is not comparable to any published result.
Invert the flow — latent (B, D) back to images (B, C, H, W).
Exact rather than approximate: every stage is run backwards, so
decode(encode(x)[0]) recovers x up to float round-off (in
eval() mode when batch normalisation is enabled).
forward(x: Tensor)Exact per-sample log-likelihood in nats.