Autoregressive Flows: MAF & IAF
Maximum expressiveness with triangular Jacobians: Masked Autoregressive Flow, Inverse Autoregressive Flow, neural spline flows, and the fundamental density-vs-sampling speed tradeoff.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Autoregressive Models as Flows
- Masked Autoregressive Flow (MAF)
- Inverse Autoregressive Flow (IAF)
- The Density-Sampling Tradeoff
- Neural Spline Flows
- Masked Networks (MADE)
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive MAF from autoregressive density estimation.
- Explain the density-vs-sampling speed tradeoff between MAF and IAF.
- Construct a MADE network for parallel autoregressive evaluation.
- Explain neural spline flows and their advantage over affine transforms.
- Choose between MAF and IAF for different applications.
Notation
- — -th dimension of the input
- — autoregressive parameters
- — ordering/permutation of dimensions
Core Intuition
Autoregressive flows exploit the chain rule of probability: any joint distribution can be decomposed as . By parameterizing each conditional as a simple transform of a base variable, we get a flow with a FULL triangular Jacobian (not block-triangular like coupling flows). This gives maximum expressiveness per layer — but at the cost of sequential computation in one direction.
Autoregressive Flow
Autoregressive Models as Flows
Any autoregressive model defines an invertible transformation:
Forward (sampling from noise):
Inverse (mapping data to noise):
Jacobian (lower triangular):
Masked Autoregressive Flow (MAF)
MAF (Papamakarios et al., 2017): Use the INVERSE direction as the flow:
Density evaluation: Given data , compute ALL in ONE parallel pass (all are known):
Sampling: Must compute sequentially: (each depends on previous).
Properties:
- Density: passes (parallel). Fast for training/evaluation.
- Sampling: sequential steps. Slow for generation.
Inverse Autoregressive Flow (IAF)
IAF (Kingma et al., 2016): Use the FORWARD direction:
Sampling: Given noise , compute ALL in ONE pass... wait, no — depends on , and is the input. So sampling IS parallel.
Actually: sample , then . This is sequential in 's ordering, but with MADE can be parallelized.
Density evaluation: Given data , must invert sequentially to find .
Properties:
- Sampling: passes (parallel given noise). Fast for generation.
- Density: sequential steps. Slow for evaluation.
The Density-Sampling Tradeoff
| Density Evaluation | Sampling | |
|---|---|---|
| MAF | Fast ( passes) | Slow ( steps) |
| IAF | Slow ( steps) | Fast ( passes) |
| Coupling | Fast () | Fast () |
Implication:
- Training (needs density): MAF is preferred.
- Generation (needs sampling): IAF is preferred.
- Both fast: Coupling flows — but less expressive per layer.
Combining: Use MAF for training the density model; distill into IAF for fast sampling (Parallel WaveNet).
Neural Spline Flows
Problem with affine transforms: Each mapping is linear (given conditioning). Limited expressiveness per dimension.
Solution (Durkan et al., 2019): Replace affine with monotone rational-quadratic splines:
- Divide each dimension into bins.
- Within each bin: a rational-quadratic function (monotone, smooth).
- Parameters: bin widths , heights , and derivatives at knots.
Jacobian: is the spline derivative (analytic, cheap).
Advantage: Much more expressive per layer. Fewer layers needed. State-of-the-art density estimation.
Masked Networks (MADE)
MADE (Germain et al., 2015): A single neural network that computes ALL autoregressive conditionals simultaneously.
Trick: Mask the weight matrices to enforce the autoregressive property:
where is the "degree" of unit in layer — the maximum input index it's allowed to depend on.
Result: One forward pass through MADE computes and for ALL simultaneously. This makes MAF density evaluation truly network evaluations.
Common Pitfalls
Pitfall 1. Assuming MAF can generate samples quickly. Each sample requires sequential network evaluations — impractical for high dimensions.
Pitfall 2. Using fixed dimension ordering. Random or learned orderings can significantly improve performance. Use multiple random orderings (ensemble).
Pitfall 3. Using affine transforms when splines are available. Spline flows achieve the same density quality with 5-10x fewer layers.
Summary
- MAF: Fast density, slow sampling; ideal for training and evaluation.
- IAF: Fast sampling, slow density; ideal for generation (after training via other means).
- Fundamental tradeoff arises from the direction of the autoregressive decomposition.
- MADE networks enable parallel computation of autoregressive parameters.
- Neural spline flows dramatically improve per-layer expressiveness over affine transforms.
- Coupling flows sacrifice per-layer expressiveness for bidirectional speed.
Exercises
Exercise 1. For a 784-dimensional MAF (MNIST flattened): compute the number of sequential steps needed to generate one sample.
Exercise 2. Derive the Jacobian determinant of a rational-quadratic spline element.
Exercise 3. Design the MADE mask matrices for a 4-dimensional input with 2 hidden layers of width 8.
Exercise 4. Prove that MAF with a single MADE layer is equivalent to a Gaussian autoregressive model.
Exercise 5. Compare BPD (bits per dimension) of MAF vs RealNVP vs Neural Spline Flow on MNIST. Explain the gap.