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.

Advanced

Prerequisites

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Autoregressive Models as Flows
  5. Masked Autoregressive Flow (MAF)
  6. Inverse Autoregressive Flow (IAF)
  7. The Density-Sampling Tradeoff
  8. Neural Spline Flows
  9. Masked Networks (MADE)
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Derive MAF from autoregressive density estimation.
  2. Explain the density-vs-sampling speed tradeoff between MAF and IAF.
  3. Construct a MADE network for parallel autoregressive evaluation.
  4. Explain neural spline flows and their advantage over affine transforms.
  5. Choose between MAF and IAF for different applications.

Notation

  • xix_iii-th dimension of the input
  • μi(),σi()\mu_i(\cdot), \sigma_i(\cdot) — autoregressive parameters
  • π\pi — ordering/permutation of dimensions

Core Intuition

Autoregressive flows exploit the chain rule of probability: any joint distribution can be decomposed as p(x)=ip(xix<i)p(\mathbf{x}) = \prod_i p(x_i | x_{<i}). 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

x1x2x3x4x5Triangular Jacobian (lower-triangular)
Step
2
TransformedDiagonal
Explore: Each xᵢ is transformed conditioned on x₁…xᵢ₋₁, yielding a triangular Jacobian with efficient O(d) inversion via sequential substitution.

Autoregressive Models as Flows

Any autoregressive model p(xix<i)=N(μi(x<i),σi2(x<i))p(x_i | x_{<i}) = \mathcal{N}(\mu_i(x_{<i}), \sigma_i^2(x_{<i})) defines an invertible transformation:

Forward (sampling from noise):

xi=μi(x<i)+σi(x<i)zi,ziN(0,1).(1)x_i = \mu_i(x_{<i}) + \sigma_i(x_{<i}) \cdot z_i, \quad z_i \sim \mathcal{N}(0, 1). \tag{1}

Inverse (mapping data to noise):

zi=xiμi(x<i)σi(x<i).(2)z_i = \frac{x_i - \mu_i(x_{<i})}{\sigma_i(x_{<i})}. \tag{2}

Jacobian (lower triangular):

xizj={σii=jcomplexi>j0i<j,detJ=iσi.(3)\frac{\partial x_i}{\partial z_j} = \begin{cases}\sigma_i & i = j \\ \text{complex} & i > j \\ 0 & i < j\end{cases}, \quad \det\mathbf{J} = \prod_i \sigma_i. \tag{3}

Masked Autoregressive Flow (MAF)

MAF (Papamakarios et al., 2017): Use the INVERSE direction as the flow:

Transform:zi=xiμi(x1:i1)σi(x1:i1).(4)\text{Transform:} \quad z_i = \frac{x_i - \mu_i(x_{1:i-1})}{\sigma_i(x_{1:i-1})}. \tag{4}

Density evaluation: Given data x\mathbf{x}, compute ALL ziz_i in ONE parallel pass (all x<ix_{<i} are known):

logp(x)=i[zi22logσi(x<i)]+const.(5)\log p(\mathbf{x}) = \sum_i \left[-\frac{z_i^2}{2} - \log\sigma_i(x_{<i})\right] + \text{const}. \tag{5}

Sampling: Must compute sequentially: x1x2xdx_1 \to x_2 \to \ldots \to x_d (each depends on previous).

Properties:

  • Density: O(1)O(1) passes (parallel). Fast for training/evaluation.
  • Sampling: O(d)O(d) sequential steps. Slow for generation.

Inverse Autoregressive Flow (IAF)

IAF (Kingma et al., 2016): Use the FORWARD direction:

Transform:xi=μi(z1:i1)+σi(z1:i1)zi.(6)\text{Transform:} \quad x_i = \mu_i(z_{1:i-1}) + \sigma_i(z_{1:i-1}) \cdot z_i. \tag{6}

Sampling: Given noise z\mathbf{z}, compute ALL xix_i in ONE pass... wait, no — xix_i depends on z<iz_{<i}, and ziz_i is the input. So sampling IS parallel.

Actually: sample z\mathbf{z}, then xi=fi(z1,,zi)x_i = f_i(z_1, \ldots, z_i). This is sequential in zz's ordering, but with MADE can be parallelized.

Density evaluation: Given data x\mathbf{x}, must invert sequentially to find z\mathbf{z}.

Properties:

  • Sampling: O(1)O(1) passes (parallel given noise). Fast for generation.
  • Density: O(d)O(d) sequential steps. Slow for evaluation.

The Density-Sampling Tradeoff

Density EvaluationSampling
MAFFast (O(1)O(1) passes)Slow (O(d)O(d) steps)
IAFSlow (O(d)O(d) steps)Fast (O(1)O(1) passes)
CouplingFast (O(1)O(1))Fast (O(1)O(1))

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 xizix_i \to z_i 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 KK bins.
  • Within each bin: a rational-quadratic function (monotone, smooth).
  • Parameters: bin widths wkw_k, heights hkh_k, and derivatives dkd_k at knots.
f(x)=RQS(x;{wk,hk,dk}k=1K).(7)f(x) = \text{RQS}(x; \{w_k, h_k, d_k\}_{k=1}^K). \tag{7}

Jacobian: f(x)f'(x) 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:

Wij(l)=Wij(l)Mij(l),Mij(l)=1[m(l)(j)m(l1)(i)],(8)W^{(l)}_{ij} = W^{(l)}_{ij} \cdot M^{(l)}_{ij}, \quad M^{(l)}_{ij} = \mathbb{1}[m^{(l)}(j) \geq m^{(l-1)}(i)], \tag{8}

where m(l)(k)m^{(l)}(k) is the "degree" of unit kk in layer ll — the maximum input index it's allowed to depend on.

Result: One forward pass through MADE computes μi(x<i)\mu_i(x_{<i}) and σi(x<i)\sigma_i(x_{<i}) for ALL ii simultaneously. This makes MAF density evaluation truly O(1)O(1) network evaluations.


Common Pitfalls

Pitfall 1. Assuming MAF can generate samples quickly. Each sample requires dd 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.