Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.One variable at a time, in one direction or the other
An autoregressive model writes a joint distribution as a chain: first the first variable, then the second given the first, and so on. Each step can be written as ‘shift and stretch some noise’, so the whole chain is an invertible map from noise to data. Which way it is fast depends on what the network reads. If it reads the data, you can score data in one go but must draw samples one variable at a time (MAF). If it reads the noise, you draw samples in one go but must score outside data one variable at a time (IAF). The demo counts the passes each one needs.
| model | density | sampling |
|---|---|---|
| MAF | 5 | 3920 |
| IAF | 3920 | 5 |
Parallel WaveNet used an IAF student to synthesise speech in real time; MAF-style conditional flows are standard tools for simulation-based inference in astrophysics and engineering.
- 1. MAF evaluates a density in…
- 2. IAF draws a sample in…
- 3. The autoregressive Jacobian is…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.MAF, IAF and MADE
- Affine autoregressive layer: x_i = μ_i(x₁…x_(i−1)) + z_i·exp(α_i(x₁…x_(i−1))). Triangular Jacobian; log |det ∂x/∂z| = Σ α_i.
- MADE (Germain et al., 2015): one network outputs all μ_i and α_i at once; binary masks on the weights ensure output i sees only earlier inputs (degrees: input i has degree i, hidden units get degrees 1…D−1, output i connects to hidden degrees below i).
- MAF: conditioner reads x. Density: z_i = (x_i − μ_i(x))·exp(−α_i(x)) for all i in one pass, log p(x) = log N(z) − Σ α_i. Sampling: D sequential passes.
- IAF: conditioner reads z: x_i = z_i·exp(α_i(z₁…z_(i−1))) + μ_i(z₁…z_(i−1)). Sampling: one pass, and log q(x) = log N(z) − Σ α_i for free. Density of an arbitrary x: D sequential passes.
- Duality: training a MAF by maximum likelihood on data equals training an IAF (its inverse) by reverse KL on the data distribution, and vice versa (Papamakarios et al., 2017).
- Stacking: reverse (or permute) the variable order between layers so later variables can influence earlier ones; MAF also used batch normalisation between layers.
- Uses: MAF for density estimation and conditional density estimation (MAF on UCI tabular sets); IAF for flexible VAE posteriors (IAF-VAE, 3.11 bits/dim on CIFAR-10) and for Parallel WaveNet via probability density distillation.
- RealNVP as a special case: a coupling layer is an autoregressive layer with only two blocks, which is why it is fast both ways but less expressive per layer.
- 1. MADE enforces autoregressive structure with…
- 2. Between stacked layers, flows…
- 3. IAF is natural for…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Masks and passes
Exercise 1.
- MADE masks for one hidden layer.
- The MAF density pass and the sequential sampling pass.
Practice.
- Check MADE’s mask product: input i must not reach output j for j ≤ i.
- Verify that D sequential passes invert the density pass exactly.
- Choose MAF when you must score data (density estimation, likelihood-based inference); choose IAF when you mostly sample and score your own samples (VI, distillation).
- Stack 5–10 layers with order reversal; use bounded log-scales.
- Use nflows, Zuko or TensorFlow Probability (MaskedAutoregressiveFlow, with tfb.Invert for IAF) in production.
import zuko, torch
flow = zuko.flows.MAF(features=2, transforms=5, hidden_features=(64, 64)) # unconditional MAF
x = torch.randn(256, 2)
loss = -flow().log_prob(x).mean() # one pass per layer
samples = flow().sample((1000,)) # sequential inverse inside each layer
# IAF: invert the transform direction (fast sampling, slow density of external points)- 1. For simulation-based inference over data, prefer…
- 2. For a VAE's posterior, prefer…
- 3. Inverting one MAF layer in D dimensions takes…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Log-det. ∂x_i/∂z_j = 0 for j above i and ∂x_i/∂z_i = exp(α_i), so the Jacobian is lower-triangular and log |det| = Σ α_i, regardless of how complex μ and α are as functions of earlier variables.
Why D passes. In MAF sampling, x_i needs μ_i(x₁…x_(i−1)): after pass k, the first k coordinates are exact, because each depends only on earlier ones that were already exact. So exactly D passes are needed in general, which matches the exercise: the error fell from 6.9 to 0.042 over 5 passes and was 0 after the 6th.
Forward vs reverse KL. MAF trained on data minimises KL(p_data ‖ p_model) (mass-covering). IAF trained against an unnormalised target p̃ minimises KL(q ‖ p) = E_q[log q(x) − log p̃(x)] + log Z using its own reparameterised samples (mode-seeking); log q of those samples is free.
Order matters per layer. One affine autoregressive layer models each conditional as a location-scale family. The banana p(x2 | x1) = N(b(x1² − 1), 1) fits this exactly in the order x1 → x2, but p(x1 | x2) is bimodal for large x2, which one location-scale layer cannot represent. Stacking with reversals fixes this.
- 1. After k sampling passes in MAF, how many coordinates are exact?
- 2. IAF trained against an unnormalised target minimises…
- 3. For the banana, the hard conditional for one layer is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.What do MAF and IAF cost in each direction, and how much does the variable order matter?
History.
- M. Germain, K. Gregor, I. Murray and H. Larochelle, “MADE: masked autoencoder for distribution estimation” (ICML 2015).
- D. P. Kingma, T. Salimans, R. Jozefowicz, X. Chen, I. Sutskever and M. Welling, “Improved variational inference with inverse autoregressive flow” (NeurIPS 2016).
- G. Papamakarios, T. Pavlakou and I. Murray, “Masked autoregressive flow for density estimation” (NeurIPS 2017).
- A. van den Oord et al., “Parallel WaveNet: fast high-fidelity speech synthesis” (ICML 2018).
Exercise 2.
- MAF on moons: 2000 training / 2000 test points; MADE with 16 tanh hidden units, outputs μ and bounded α; 1 or 4 layers with order reversal; Adam, learning rate 0.02, batch 256, 400 steps.
- Sampling passes: a random 6-D MADE layer (32 hidden units); error of the sampled x after k = 1…6 sequential passes compared with the exact inverse.
- IAF by reverse KL: target banana x1 ~ N(0, 1), x2 − (x1² − 1) ~ N(0, 1) (normalised, so the KL estimate is exact); one IAF layer with natural (x1 first) or reversed (x2 first) order, and four layers starting reversed; 300 steps, batch 256; KL from 20000 fresh samples.
| measure | seed 0 | seed 1 | seed 2 |
|---|---|---|---|
| MAF 1 layer, moons NLL | 2.258 | 2.266 | 2.316 |
| MAF 4 layers, moons NLL | 1.748 | 1.763 | 1.847 |
| max error after 5 passes (6-D) | 0.042 | 0.0094 | 0.039 |
| max error after 6 passes | 0.0 | 0.0 | 0.0 |
| IAF 1 layer, x1 first, KL | 0.066 | 0.060 | 0.046 |
| IAF 1 layer, x2 first, KL | 0.238 | 0.249 | 0.239 |
| IAF 4 layers, x2 first, KL | 0.036 | 0.040 | 0.053 |
- Depth with reversals helped MAF by about 0.5 nats on moons.
- Sampling is sequential: after 5 of 6 passes the sample was still wrong by up to 0.04; after the 6th, exact (round-trip error 1e-15).
- Order matters for one layer: with x1 first the IAF matched the banana closely; with x2 first it could not represent the bimodal conditional (KL about 0.24). Four layers with reversals overcame the bad order.
- Caveat: small MADE networks and short training; the KL values include optimisation error.
MAF and IAF in AI, robotics and aerospace
- Simulation-based inference: conditional MAFs are the default density estimator in sbi (neural posterior and likelihood estimation) for parameters of physical simulators.
- Speech synthesis: Parallel WaveNet distilled an autoregressive WaveNet into an IAF that generates audio in parallel.
- Variational inference: IAF posteriors give tighter ELBOs in VAEs and Bayesian models; also used as flexible proposals in importance sampling.
References
- M. Germain, K. Gregor, I. Murray and H. Larochelle (2015). “MADE: masked autoencoder for distribution estimation.” ICML.
- D. P. Kingma et al. (2016). “Improved variational inference with inverse autoregressive flow.” NeurIPS.
- G. Papamakarios, T. Pavlakou and I. Murray (2017). “Masked autoregressive flow for density estimation.” NeurIPS.
- A. van den Oord et al. (2018). “Parallel WaveNet: fast high-fidelity speech synthesis.” ICML.