Overview
RealNVP is a normalizing flow based on coupling transformations. Unlike autoregressive flows, RealNVP can compute both forward and inverse transformations in parallel, making it very fast for both training and sampling.RealNVP is an alias for NICE with affine transformations. The two classes are equivalent.
Reference
Density estimation using Real NVP (Dinh et al., 2016)https://arxiv.org/abs/1605.08803
Class Definition
Parameters
The number of features in the data. Must be at least 2 for coupling to work.
The number of context features for conditional density estimation.
The number of coupling transformations to stack.
Whether random coupling masks are used. If
False, alternating checkered masks are used.Additional keyword arguments passed to
GeneralCouplingTransform:hidden_features: List of hidden layer sizes (default:[64, 64])activation: Activation function (default:ReLU)univariate: Univariate transformation constructor (default:MonotonicAffineTransform)
Usage Example
Conditional Flow
Training Example
Random Masks
Methods
forward(c=None)
Returns a normalizing flow distribution.
Arguments:
c(Tensor, optional): Context tensor of shape(*, context)
NormalizingFlow: A distribution with the following methods:sample(shape): Sample from the distribution (fast, parallel)log_prob(x): Compute log probability (fast, parallel)rsample(shape): Reparameterized sampling
When to Use RealNVP
Good for:
- Real-time generation and sampling
- Large-scale datasets (fast training)
- Applications requiring bidirectional speed
- Image generation (with spatial coupling)
- You need maximum expressivity (use NSF or NAF)
- You have very complex, multimodal distributions
- Features are highly correlated in complex ways
Tips
- More transformations: Since each layer only transforms half the features, use more transformations (5-10) compared to autoregressive flows.
-
Random masks: Set
randmask=Truewhen your features have inherent structure or ordering. -
Deeper networks: Coupling layers have less capacity, so use larger hidden layers
[256, 256]or[512, 512]. - Preprocessing: Combine with activation normalization or batch normalization for better performance.
Architecture Details
RealNVP uses coupling transformations:- Base distribution: Diagonal Gaussian
N(0, I) - Coupling mechanism: Split features into two groups using a binary mask
- Transformation: First group unchanged, second group transformed based on first
- Neural network: Standard MLP (no masking needed)
s and t are neural networks, and c is optional context.
Coupling vs. Autoregressive
| Property | RealNVP (Coupling) | MAF (Autoregressive) |
|---|---|---|
| Forward pass | Parallel | Parallel |
| Inverse pass | Parallel | Sequential |
| Training speed | Fast | Fast |
| Sampling speed | Fast | Slow |
| Expressivity | Medium | Medium |
| Layers needed | More (5-10) | Fewer (3-5) |
