Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Conditional flow as an amortised posterior

Train a conditional flow on (x, y=Ax+η) pairs once, and it becomes the posterior p(x|y) for an inverse problem — one forward pass per observation, bimodal when the data underdetermines x

02 — Conditional flow as an amortised posterior

An inverse problem observes y=Ax+ηy = A x + \eta — a linear measurement of a hidden xx, blurred by noise η∼N(0,σ2I)\eta\sim\mathcal{N}(0,\sigma^2 I) — and asks for xx. When AA loses information (here A:R2→R1A:\mathbb{R}^2\to\mathbb{R}^1), many xx explain the same yy, so the answer is a posterior p(x∣y)∝p(y∣x) p(x)p(x\mid y)\propto p(y\mid x)\,p(x), not a point.

A conditional flow is a natural fit. Simulate pairs (x,y)(x, y) with x∼p(x)x\sim p(x) and y=Ax+ηy = Ax+\eta, then train pθ(x∣y)p_\theta(x\mid y) by the conditional NLL of notebook 01. The result is an amortised posterior Papamakarios et al. (2021), Winkler et al. (2019): the cost of inference is paid once at training time, and afterwards any observation yy yields posterior samples and densities in a single forward pass — no per-yy optimisation, no MCMC chain. Because the flow is fully expressive, the posterior can be multimodal when the observation genuinely underdetermines xx — something a Gaussian/Laplace approximation cannot do.

What you will see

1. The forward model: two-moons prior + a 1-D observation

The prior p(x)p(x) is the standardised two moons. The forward operator A=[1,1]A = [1, 1] collapses the plane onto the anti-diagonal, y=x1+x2y = x_1 + x_2, with Gaussian noise σ=0.1\sigma = 0.1. An observation yy therefore pins xx to a line x1+x2≈yx_1 + x_2 \approx y — and the posterior is the slice of the moons that line cuts through.

prior X (6000, 2), observation Y (6000, 1); y = x1 + x2 + N(0,0.1^2)
<Figure size 560x500 with 1 Axes>

Each line is one observation. Where it crosses one crescent the posterior is a single blob; where it crosses both, the posterior is bimodal — watch the y=0y=0 (anti-diagonal) line, which clips both moons.

2. Train the amortised posterior

A conditional coupling flow pθ(x∣y)p_\theta(x\mid y) (fixed N(0,I)\mathcal{N}(0,I) base, RQSplineCoupling(cond_dim=1) reading the scalar observation) trained by NLL on the simulated pairs. One run; the posterior for every yy comes for free afterwards.

trained amortised posterior: 350 epochs, val NLL -0.8465

3. The amortised posterior vs a rejection reference

For each observation yy we compare two posteriors. The reference is rejection sampling on the prior — keep prior draws whose AxAx falls within a hair of yy — which is exact but throws away almost every sample. The flow posterior is one batched sample(condition=y) call. They should agree, and the flow should recover the bimodal y=0y=0 case.

<Figure size 1500x520 with 3 Axes>

The flow posteriors sit on the moons, hug the observation line, and at y=0y=0 the flow splits into two modes — one per crescent — matching the reference. All three come from the same network, evaluated at three different yy.

4. Observation consistency

A correct posterior must be consistent with the measurement: pushing posterior samples back through AA should recenter on the observed yy with spread ≈σ\approx\sigma. We histogram Ax^A\hat x for the flow samples at each yy.

y=-1:  mean(Ax)=-1.007 (target -1.0),  std(Ax)=0.099 (target 0.1)
y=+0:  mean(Ax)=+0.005 (target +0.0),  std(Ax)=0.105 (target 0.1)
y=+1:  mean(Ax)=+0.993 (target +1.0),  std(Ax)=0.103 (target 0.1)
<Figure size 1500x360 with 3 Axes>

The reprojected samples center on yy with standard deviation close to the noise σ — the flow learned the likelihood, not just the prior support.

Recap — and the end of Part 7

amortised flow posterior (here)classical inversion
cost per new yyone forward passan optimisation / MCMC run
trainingonce, on simulated (x,y)(x,y)none (but solve every time)
multimodal posteriorsyes, by constructionneeds many restarts / chains
needs the forward modelonly to simulate pairsexplicitly, every solve

A conditional flow turns inference into amortisation: simulate (x,y=Ax+η)(x, y=Ax+\eta), fit pθ(x∣y)p_\theta(x\mid y) once, and read off the posterior — bimodal and all — for any observation in a single pass. This closes Part 7: from where to inject context (00), through conditional density estimation and the marginals-vs-couplings rule (01), to conditioning on an observation for inverse problems.

Where this goes next. When the prior is itself a (Gaussianization) flow rather than something we can simulate from cheaply, the amortised route is replaced by a plug-and-play scheme that alternates a data-fit step with the flow prior’s closed-form proximal/Gaussianization step — the subject of Part 16. The toy here is the amortised counterpart of those solvers.

References
  1. Papamakarios, G., Nalisnick, E., Rezende, D. J., Mohamed, S., & Lakshminarayanan, B. (2021). Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 22(57), 1–64.
  2. Winkler, C., Worrall, D. E., Hoogeboom, E., & Welling, M. (2019). Learning Likelihoods with Conditional Normalizing Flows. arXiv Preprint arXiv:1912.00042.