Skip to content
class

RealNVPModel

extendsPretrainedModel
RealNVPModel(config: RealNVPConfig)

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

Architecture, prior and preprocessing knobs; see RealNVPConfig.

Attributes

Squash-then-logit stage (paper §4.1).
One entry per scale, each a nn.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

1
dunder

__init__

→None
__init__(config: RealNVPConfig)

Properties

2
prop

input_dim

→int
input_dim: int

Flattened dimensionality D of the latent.

prop

prior

→str
prior: str

Name of the factorised latent prior.

Instance methods

5
fn

bits_per_dim

→Tensor
bits_per_dim(x: Tensor)

Per-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 −Dlog⁡256-D \log 256 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.

fn

decode

→Tensor
decode(z: Tensor)

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).

fn

encode

→tuple[Tensor, Tensor]
encode(x: Tensor)

Map images in [0, 1] to the latent space.

Returns

tuple[Tensor, Tensor]

(z, log_det) with z of shape (B, D) — the factored-out parts concatenated in scale order — and log_det of shape (B,), summing the logit stage, every coupling and every flow batch-norm.

fn

log_prob

→Tensor
log_prob(x: Tensor)

Exact per-sample log-likelihood log⁡pX(x)\log p_X(x) in nats.