Skip to content

Bayesian PINNs

overview

A Bayesian physics-informed network recovers an unknown coefficient from noisy data together with its uncertainty. In jNO a parameter gets a sampler with .bayesian(...) (MCMC) or .vi(...) (variational inference), and crux.solve() runs it; the samplers come from blackjax (NUTS, HMC, MALA, SGLD, SGHMC). The API is in Training → Bayesian Sampling.

The four NUTS chains of the reaction coefficient k of tutorial 01 with the 90% credible band and the true value, the posterior histogram beside them, and the neural surrogate fitted to the noisy sensors on the left.

The tutorials

tutorial what it shows
A coefficient with its uncertainty fit a surrogate, freeze it, sample one reaction coefficient with NUTS; the 90 % interval covers the truth
A Poisson source from sparse sensors 16 source coefficients from 14 sensors in half the square; the posterior standard deviation is a map

Train, then freeze

Sampling a coefficient while the surrogate is still training samples against a moving target. Fitting the surrogate first and freezing it gives the sampler a fixed posterior. The whole pattern is two jno.core calls:

# phase 1: fit the surrogate to the noisy data
u_net = jno.nn(foundax.mlp(1, output_dim=1, hidden_dims=32, num_layers=3, key=jax.random.PRNGKey(0)))
u_net.optimizer(optax.adam(2e-3))
u = u_net(x) * x * (1 - x)                        # hard u(0) = u(1) = 0
jno.core([(u - u_obs).mse]).solve(1500)

# phase 2: freeze it, then sample k against the now fixed forward map
u_net.freeze()
k = jno.np.parameter((1,), name="k").initialize(jax.nn.initializers.constant(1.0))
k.bayesian(blackjax.nuts, step_size=1e-2, warmup=150, keep=300, num_chains=4, init_jitter=0.5)

uf = (u_net(x) * x * (1 - x)).scalar.bind(x=x)
residual = (-LAMBDA * uf.xx + k * uf - f_known) / 0.05   # −λu″ + k u − f = 0
jno.core([residual.mse]).solve(450)

ch = k.posterior_samples                            # (num_chains, keep, 1)
print(jnp.mean(ch), jno.bayesian.rhat(ch))

This gives \(k=2.027\) with a 90 % interval \([1.942,\ 2.112]\) around the true 2.0, and R-hat 1.003. Through crux.eval([expr]) any expression that depends on a sampled parameter is pushed through the chain, which gives posterior predictive arrays.

References

  • Yang, L., Meng, X., & Karniadakis, G. E. (2021). B-PINNs: Bayesian physics-informed neural networks for forward and inverse PDE problems with noisy data. Journal of Computational Physics, 425, 109913.
  • Hoffman, M. D., & Gelman, A. (2014). The No-U-Turn Sampler. Journal of Machine Learning Research, 15(1), 1593–1623.