Normalizing Flows for Variational Inference

Summary

The quality of VI is capped by the family : with mean-field or Gaussian families “no solution is ever able to resemble the true posterior distribution,” so, “unlike other inferential methods such as MCMC, even in the asymptotic regime we are unable [to] recover the true posterior” (Rezende & Mohamed 2015, Sec. 1). A normalizing flow enriches the family by pushing a simple base density through a chain of invertible maps ; the change-of-variables formula keeps the density computable, . Choosing maps with Jacobian determinants (planar and radial flows) gives a scalable, arbitrarily flexible, reparameterizable posterior that drops into the amortized VI training loop unchanged.

Overview

The earlier notes locate VI’s two characteristic failures, under-dispersion and inability to represent correlation, skew or multimodality, in the family rather than the optimizer. Rezende & Mohamed cite Turner & Sahani (2011) for both: variance under-estimation “can result in poor predictions and unreliable decisions,” and limited posteriors “can also result in biases in the MAP estimates of any model parameters” (e.g. in time-series models). Prior remedies were structured mean-field (add some dependencies) and mixture posteriors, the latter requiring likelihood and gradient evaluations per mixture component per update.

Flows attack the problem from the direction ADVI’s optimal-transform result points to. ADVI uses a fixed transform plus a Gaussian and notes that the ideal would make the Gaussian exact but is unknowable. A flow learns a parametric transform by maximizing the ELBO; Kucukelbir et al. cite exactly this paper as the way to “improve accuracy” by “a cascade of simple transformations.”

Main Content

Normalizing flow ^def-normalizing-flow

For an invertible smooth and with ,

(Eq. 5). Composing maps, ,

(Eqs. 6-7). “The path traversed by the random variables … is called the flow and the path formed by the successive distributions is a normalizing flow.” Each map acts as a local expansion (lowering density) or contraction (raising it).

Law of the unconscious statistician for flows ^thm-lotus

(Eq. 8), with no Jacobian needed when does not depend on . A flow is therefore automatically reparameterized: sample from the base (itself ), push it through differentiable maps, and backpropagate. It is the reparameterization trick with a deeper .

The obstacle is cost: a generic invertible network layer has an Jacobian determinant. The paper’s contribution is two map families whose determinant is .

Planar and radial flows ^def-planar-radial

Planar: with and smooth elementwise (e.g. ). With , the matrix determinant lemma gives

(Eqs. 10-13). It contracts or expands density perpendicular to the hyperplane . Invertibility with requires , enforced by reparameterizing (Appendix A). Radial: with , , and

(Eq. 14): contraction or expansion around a reference point . Two successive transformations already turn a spherical Gaussian into a bimodal density (Fig. 1).

Flow-based free energy bound ^thm-flow-bound

With , the negative ELBO is

(Eq. 15). In the amortized setting an inference network maps to the base parameters and to the flow parameters . Cost is for deterministic layers of width , flow length and latent dimension (Sec. 4.3). Training (Algorithm 1) is AEVB with one extra line: .

Infinitesimal flows and the asymptotic claim (Sec. 3.2). Letting the flow length go to infinity gives a density evolving under a PDE. For the Langevin flow with the negative unnormalized log posterior, the Fokker-Planck stationary solution is , “i.e. the true posterior.” Hamiltonian flow on an augmented space is the dynamics of HMC. This yields a unifying view (Sec. 5): NICE (Dinh et al. 2014) is a finite volume-preserving flow with coupling layers and unit Jacobian; Hamiltonian variational inference (Salimans et al. 2015) is an infinitesimal volume-preserving flow that converges to the posterior but needs likelihood gradients at every step, at both training and test time.

Evidence (Sec. 6)

  • 2-D test densities with multimodality and periodicity (Table 1): planar flows with show “a substantial improvement in the approximation quality as we increase the flow length”; NICE reaches similar asymptotic quality but planar flows need “far fewer parameters” (Fig. 3).
  • Binarized MNIST, deep latent Gaussian model with 40 latents, bound on (Table 2): diagonal-covariance baseline ; planar NF for ; NICE for the same . Increasing flow length “systematically improves the bound” and “reduces the KL-divergence between the approximate posterior and the true posterior” (Fig. 4).
  • CIFAR-10 patches, 30 latents (Table 3): the reported improves monotonically from () to ().
  • Training used an annealed free energy, multiplying the term, which the authors found “to provide better results”: flexible posteriors are harder to optimize.

Examples

A planar flow layer ^ex-planar-code

import torch, torch.nn as nn, torch.nn.functional as F
 
class Planar(nn.Module):
    def __init__(self, D):
        super().__init__()
        self.u, self.w = nn.Parameter(torch.randn(D) * 0.01), nn.Parameter(torch.randn(D) * 0.01)
        self.b = nn.Parameter(torch.zeros(1))
 
    def forward(self, z):                         # z: (S, D)
        wu = self.w @ self.u                      # enforce w^T u >= -1 (Appendix A)
        u_hat = self.u + ((-1 + F.softplus(wu)) - wu) * self.w / (self.w @ self.w)
        a = z @ self.w + self.b                   # (S,)
        f = z + u_hat * torch.tanh(a)[:, None]
        psi = (1 - torch.tanh(a) ** 2)[:, None] * self.w
        logdet = torch.log(torch.abs(1 + psi @ u_hat) + 1e-8)
        return f, logdet
 
def neg_elbo(log_joint, mu, log_sig, flows, S=64):
    eps = torch.randn(S, mu.numel())
    z = mu + log_sig.exp() * eps                  # base draw, reparameterized
    logq = (-0.5 * eps**2 - log_sig - 0.9189385).sum(1)
    for fl in flows:
        z, ld = fl(z); logq = logq - ld           # Eq. 13
    return (logq - log_joint(z)).mean()           # Eq. 15

Used on a fixed-dimensional Bayesian posterior (no encoder), this is “ADVI with a learned transform.” Banana-shaped or funnel-shaped posteriors, the kind produced by multiplicative saturation-times-coefficient terms in a media mix model or by hierarchical scales, are the natural targets.

Practical caveats. A more flexible makes the ELBO surface harder (the annealing above; the Bayesian Workflow book: “the richer the family of approximations, the more challenging the optimization”). Reverse KL remains mode-seeking, so a flow can represent several modes but is not guaranteed to find them. Planar flows are weak per layer in high dimension, which is why later architectures (coupling, autoregressive and spline flows; see Normalizing Flows as Conditional Density Estimators) dominate in practice. And a flow posterior still needs the same verification as any other: [[Diagnosing Variational Inference (PSIS k-hat and VSBC)|]] works unchanged because has a computable density.

Connections

See Also