Machine Learning for Astroparticle Physics:
A Crash-course in SBI

Lecture 3b - Inference heads and parameter models

Christoph Weniger, University of Amsterdam (GRAPPA)

Today's Roadmap

1
The SBI pipeline
simulator, inference loss, embedding
2
Why NPE works
minimising NLL recovers the posterior
3
The Gaussian head
simplest head, and where it breaks
The thread: start from the SBI pipeline anatomy (simulator, inference loss, embedding); see why minimising the NLL recovers the posterior (NPE); then build the simplest inference head, the Gaussian head, and find where it breaks. Flows and continuous-time heads come in the next lecture.

The SBI pipeline

Three building blocks: simulator, inference loss, embedding network.

The SBI pipeline — the simulator

Simulator — encodes the physics.
Maps parameters \(\boldsymbol\theta\) to data realisations \(\mathbf{x} \sim p(\mathbf{x}\mid\boldsymbol\theta)\).

Examples:

  • Strong-lensing image generators (source + lens model + PSF + noise).
  • Gravitational-wave waveform models (LALSuite, IMRPhenom).
  • Cosmological N-body / hydro (Quijote, CAMELS).
  • Particle-physics event generators (Pythia, MadGraph).

⚠ Failure mode: wrong model → wrong inference. Misspecification of the simulator propagates into the posterior, often invisibly.

The SBI pipeline — the inference loss

Inference loss \(\mathcal{L}\) — defines the statistical question.
The choice of loss is what turns the network into a particular kind of estimator.

The loss decides what we're solving:

  • NPE → NLL of \(\boldsymbol\theta^\star\) under \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\).
  • NRE → binary cross-entropy on a joint-vs-marginal classifier.
  • NLE → NLL of \(\mathbf{x}^\star\) under \(q_\phi(\mathbf{x}\mid\boldsymbol\theta)\).
  • Flow matching → velocity-regression on a probability path.
  • Diffusion → denoising score matching.

Same simulator, same network, different loss ⇒ different statistical task.

The SBI pipeline — the embedding network

Embedding network \(T_\phi(\mathbf{x})\) — data compression.
Maps high-dimensional observations to a low-dimensional summary that the inference head can chew on.

Why we need it:

  • Strong lensing: \(\mathbf{x}\) is a \(256\times 256\) image, \(\sim\!65{,}000\) pixels.
  • Gravitational waves: \(\sim\!16{,}000\) time-series samples per detector.
  • Cosmology: density fields of \(\sim\!10^9\) voxels.

A raw MLP would have too many parameters and no inductive bias. A CNN (for images), Transformer (for sequences), or GNN (for catalogues) does the compression. Trained jointly with the inference head, so \(T_\phi\) learns features informative for the posterior, not for any predefined label. Architecture choices are the topic of Lec 4.

Why NPE works

Minimising NLL on simulator pairs recovers the Bayesian posterior, in two idealisations and two proofs.

NPE, NRE, NLE, one table to rule them all

NPE NRE NLE
Models \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) \(\log r_\phi(\boldsymbol\theta, \mathbf{x})\) \(q_\phi(\mathbf{x}\mid\boldsymbol\theta)\)
Loss NLL cross-entropy NLL
Sampling direct, single forward pass MCMC on \(p(\boldsymbol\theta)\,e^{f_\phi}\) MCMC on \(q_\phi\,p(\boldsymbol\theta)\)
Amortised yes yes (re-MCMC per obs) partial (re-MCMC per obs)
High-D \(\boldsymbol\theta\) flow head needed scales via marginal targeting (TMNRE)hard, \(\mathbf{x}\) usually higher-D
Production examplesDINGO (GW PE) saqqara (LISA SGWB) field models / cosmology
NPE stays the spine of this lecture. NRE and NLE are background colour, you should be able to name them and explain when to reach for each.

What does an NPE network actually learn?

In Lec 2 we trained \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) on a fixed dataset of pairs \((\boldsymbol\theta_n, \mathbf{x}_n)\) by minimising the NLL. Today the pairs come from a simulator, \(\boldsymbol\theta_n \sim p(\boldsymbol\theta)\), \(\mathbf{x}_n \sim p(\mathbf{x}\mid\boldsymbol\theta_n)\). Same loss, same network. This is neural posterior estimation (NPE).

\(\mathbf{x}\)
observation
network \(f_\phi\)
density parameters
\(q_\phi(\boldsymbol\theta\mid\mathbf{x})\)
conditional density on \(\boldsymbol\theta\)
NLL loss
−log \(q_\phi(\boldsymbol\theta^\star\mid\mathbf{x})\)
\[ \int q_\phi(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta \;=\; 1 \quad\forall\,\mathbf{x} \qquad\qquad \mathcal{L}(\phi) \;=\; -\frac{1}{N}\sum_{i} \log q_\phi(\boldsymbol\theta^{(i)} \mid \mathbf{x}^{(i)}) \]

The NLL loss is just maximum likelihood: maximise \(\prod_i q_\phi(\boldsymbol\theta^{(i)}\mid\mathbf{x}^{(i)})\), the joint probability of all observed parameters given their data, which is the same as minimising \(-\!\sum_i \log q_\phi(\boldsymbol\theta^{(i)}\mid\mathbf{x}^{(i)})\).

Claim: at the minimum of \(\mathcal{L}\), the learned density \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) is the Bayesian posterior.

Papamakarios & Murray 2016 (mixture-density); Lueckmann et al. 2017; Greenberg et al. 2019; review: Cranmer, Brehmer, Louppe (PNAS 2020).

Training data is a distribution

The simulator pairs \(\mathcal{D} = \{(\boldsymbol\theta^{(i)}, \mathbf{x}^{(i)})\}_{i=1}^{N}\) define an empirical joint distribution:

\[ \hat p(\boldsymbol\theta, \mathbf{x}) \;=\; \frac{1}{N}\sum_{i=1}^{N}\, \delta(\boldsymbol\theta-\boldsymbol\theta^{(i)})\, \delta(\mathbf{x}-\mathbf{x}^{(i)}) \]

Which factorises into prior and simulator-likelihood:

\[ \hat p(\boldsymbol\theta, \mathbf{x}) \;=\; \hat p(\boldsymbol\theta)\,\hat p(\mathbf{x}\mid\boldsymbol\theta), \qquad \hat p(\boldsymbol\theta) = \frac{1}{N}\sum_i \delta(\boldsymbol\theta-\boldsymbol\theta^{(i)}), \qquad \hat p(\mathbf{x}\mid\boldsymbol\theta^{(i)}) \;=\; \delta(\mathbf{x}-\mathbf{x}^{(i)}) \]

In SBI the data-generating process is known by construction, it is the simulator:

(i) sample \(\boldsymbol\theta \sim p(\boldsymbol\theta)\) from the prior, (ii) run the forward model \(\mathbf{x} \sim p(\mathbf{x}\mid\boldsymbol\theta)\), (iii) store the pair.

In the \(N\to\infty\) idealisation, \(\hat p \to p(\boldsymbol\theta, \mathbf{x}) = p(\boldsymbol\theta)\,p(\mathbf{x}\mid\boldsymbol\theta)\), the delta-combs smooth into a continuous density. This limit is mathematical / conceptual; in practice we always work with finite samples.

Infinite-data limit — minimising NLL gives the posterior

Take two idealisations: (i) \(N\to\infty\), so \(\hat p \to p(\boldsymbol\theta, \mathbf{x}) = p(\boldsymbol\theta)\,p(\mathbf{x}\mid\boldsymbol\theta)\); (ii) the network is infinitely flexible, so \(q(\boldsymbol\theta\mid\mathbf{x})\) is any function with \(\int q(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta = 1\) for every \(\mathbf{x}\).

\[ \mathcal{L}[q] \;\longrightarrow\; -\!\iint p(\boldsymbol\theta, \mathbf{x})\, \log q(\boldsymbol\theta\mid\mathbf{x})\, d\boldsymbol\theta\, d\mathbf{x} \]

Functional derivative w.r.t. \(q(\boldsymbol\theta\mid\mathbf{x})\), under the constraint \(\int q(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta = 1\):

\[ \left.\frac{\delta \mathcal{L}[q]}{\delta q(\boldsymbol\theta\mid\mathbf{x})}\right|_{\int q(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta\,=\,1} \;=\; 0 \]

\[ \Longrightarrow\quad q^\star(\boldsymbol\theta\mid\mathbf{x}) \;=\; \frac{p(\boldsymbol\theta, \mathbf{x})}{\int p(\boldsymbol\theta', \mathbf{x})\,d\boldsymbol\theta'} \;=\; p(\boldsymbol\theta\mid\mathbf{x}) \]

The learned \(q_\phi(\boldsymbol\theta\mid\mathbf{x})\) is the Bayesian posterior implied by the simulator. A finite network just interpolates this idealised answer.

Infinite-data limit — two proofs

Starting from \(\mathcal{L}[q] = -\!\iint p(\boldsymbol\theta, \mathbf{x})\, \log q(\boldsymbol\theta\mid\mathbf{x})\, d\boldsymbol\theta\, d\mathbf{x}\), we want the minimiser \(q^\star\).

(a) Lagrange multiplier. Add \(\int\!\lambda(\mathbf{x})\bigl(\int q(\boldsymbol\theta\mid\mathbf{x})\,d\boldsymbol\theta - 1\bigr)\,d\mathbf{x}\) to \(\mathcal{L}\).

\[ -\frac{p(\boldsymbol\theta,\mathbf{x})}{q(\boldsymbol\theta\mid\mathbf{x})} + \lambda(\mathbf{x}) = 0 \]

\(\Rightarrow q^\star(\boldsymbol\theta\mid\mathbf{x}) \propto p(\boldsymbol\theta,\mathbf{x})\). Normalising over \(\boldsymbol\theta\) fixes \(\lambda(\mathbf{x}) = p(\mathbf{x})\) and gives

\[ q^\star(\boldsymbol\theta\mid\mathbf{x}) = p(\boldsymbol\theta\mid\mathbf{x}). \]

(b) KL divergence. Rewrite \(\mathcal{L}\) by conditioning on \(\mathbf{x}\):

\[ \mathcal{L}[q] = \mathbb{E}_{\mathbf{x}}\!\big[\mathrm{KL}\!\left(p(\cdot\mid\mathbf{x})\,\|\,q(\cdot\mid\mathbf{x})\right)\big] + H(\boldsymbol\theta\mid\mathbf{x}) \]

The entropy term does not depend on \(q\). KL \(\ge 0\) with equality iff \(q = p(\cdot\mid\mathbf{x})\), so

\[ q^\star(\boldsymbol\theta\mid\mathbf{x}) = p(\boldsymbol\theta\mid\mathbf{x}). \]

Same answer, two lenses: (a) is mechanical (stationary point of a constrained functional); (b) is geometric (NLL = KL up to a \(q\)-independent constant, so minimising NLL = projecting \(p(\cdot\mid\mathbf{x})\) onto the model family under KL).

The Gaussian head

The simplest concrete inference head, multivariate \(\boldsymbol\mu_\phi(x)\) with input-dependent \(\sigma_\phi(x)\), and where it breaks.

Heteroscedastic noise — variance as a second output

Our first model baked in a single \(\sigma_\theta\). Make \(\sigma\) a function of \(x\); same network, two heads: \(\mu_\phi(x)\) and \(\log\sigma_\phi(x)\). The NLL trades off mean accuracy against calibrated width.

\[ q_\phi(\theta\mid x) \;=\; \mathcal{N}\!\bigl(\theta \,\big|\, \mu_\phi(x), \sigma_\phi(x)^2\bigr), \qquad \mathcal{L} = -\!\sum_n \log q_\phi(\theta_n\mid x_n) \]

Toy: noise is small on the left, large on the right. The grey band is the learned \(\sigma(x)\), it tracks the true scale instead of averaging over \(x\).

The Gaussian-head network — the simplest inference head

Density family

\( q_\phi(\boldsymbol\theta\mid x) \;=\; \mathcal{N}\!\bigl(\boldsymbol\theta\,\big|\,\boldsymbol\mu_\phi(x), \boldsymbol\Sigma_\phi(x)\bigr) \)

Both mean and covariance are NN outputs, the network learns where to be confident.

NPE loss on simulator pairs \((\boldsymbol\theta_n, x_n)\):

\( \mathcal{L}(\phi) = -\tfrac{1}{N}\sum_n \log q_\phi(\boldsymbol\theta_n\mid x_n) \)

For diagonal \(\Sigma\): \(\;\tfrac{1}{2}\bigl((\theta-\mu)/\sigma\bigr)^2 + \log\sigma\). Constant \(\Sigma\) reduces to MSE, the linear-regression case from Lec 2.

// Train q_phi(theta|x) on simulator pairs (theta_n, x_n)
for step = 1, 2, ... :
    sample mini-batch B of pairs
    mu, log_sigma = f_phi(x)
    L = mean( 0.5*((theta-mu)/sigma)^2 + log_sigma )
    phi <- Adam(phi, grad_phi L)
What's next. A Gaussian head can only represent unimodal, elliptical posteriors. Real posteriors are often multimodal or curved, and that is what motivates flows, flow matching, and diffusion in the next lecture.

Live demo — Gaussian-head NN on a linear forward model

Forward model: \(t_i = a + b\,x_i + \varepsilon\), 20-point grid, \(\varepsilon\sim\mathcal{N}(0,0.25^2)\). Priors \(a\sim\mathcal U[-1,1]\), \(b\sim\mathcal U[-2,2]\). Network: MLP \([20{\to}32{\to}16{\to}5]\), output \((\mu_a,\mu_b,\log\sigma_a,\log\sigma_b,\rho)\).

Left: a single observation \(\mathbf{x}\). Right: the learned posterior \(q_\phi(a,b\mid\mathbf{x})\) (orange ellipse) vs the true \((a^\star,b^\star)\) (green star). The same trained network handles any new \(\mathbf{x}\), amortised inference.