Community研究&データ分析github.com

bayesian-pinn

Posterior uncertainty on neural solutions: BNNODE for ODEs, BayesianPINN for PDEs, prior choice, and reading a credible interval honestly. Use when a neural surrogate must carry an error bar.

bayesian-pinn とは?

bayesian-pinn is a Claude Code agent skill that posterior uncertainty on neural solutions: BNNODE for ODEs, BayesianPINN for PDEs, prior choice, and reading a credible interval honestly. Use when a neural surrogate must carry an error bar.

対応✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/9018447/julia-pro-p/tree/HEAD/skills/bayesian-pinn

Installed? Explore more 研究&データ分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Bayesian PINN — BNNODE & BayesianPINN

NeuralPDE.jl ships its own Bayesian engine built directly on AdvancedHMC and MCMCChains, independent of Turing (no @model block). Two entry points depending on whether the target equation is an ODE or a PDE.

Which posterior

QuestionUse
credible intervals on the solution of a known ODE/PDEBNNODE / BayesianPINN — this page
unknown coefficients fitted from data, with uncertaintyBNNODE with param = [prior, ...] — this page
point estimate only../neural-pde/SKILL.md, which is the cheaper path
a known model plus a neural correction, uncertainty on both../bayesian-ude-workflow/SKILL.md — Turing @model with an embedded solve

The distinction that decides it: a PINN posterior is over a function parameterised by a network, so the uncertainty carries the network's own ambiguity; a UDE posterior is over parameters of a known model, so the interval is usually tighter and more interpretable. Reach for BPINN when the solution field itself is the deliverable.

ODEs — BNNODE solver

The high-level interface: pass BNNODE as the algorithm to solve(prob, alg).

using NeuralPDE, Lux, AdvancedHMC, OrdinaryDiffEq

# Out-of-place form is REQUIRED — `du = f(u, p, t)`, not `f!(du, u, p, t)`
function lotka(u, p, t)
    [p[1] * u[1] - p[2] * u[1] * u[2],
     p[3] * u[1] * u[2] - p[4] * u[2]]
end
prob = ODEProblem(lotka, u0, tspan, p_true)

chain = Chain(Dense(1, 8, tanh), Dense(8, 8, tanh), Dense(8, 2))

alg = BNNODE(
    chain;
    dataset       = [t_obs, u_obs],          # for inverse problems / data fitting
    draw_samples  = 1500,
    priorsNNw     = (0.0, 2.0),              # (mean, std) of Gaussian weight prior — default
    phystd        = [0.05, 0.05],            # std of physics residual likelihood
    l2std         = [0.05, 0.05],            # std of data-fit likelihood
    param         = [Normal(1.0, 2.0)],      # priors on physical parameters (inverse)
    Adaptorkwargs = (Integrator = Leapfrog,),
    progress      = true,
)

sol = solve(prob, alg)

The lower-level function ahmc_bayesian_pinn_ode(prob, chain; ...) exposes the same kwargs for callers that want to drive HMC directly without going through the SciML solver interface.

Critical constraint: BPINN ODE solvers only accept the out-of-place form du = f(u, p, t). The mutating in-place form f!(du, u, p, t) will silently misbehave or error. Convert in-place ODEs before passing them to BNNODE.

PDEs — BayesianPINN discretizer

For full PDE systems (built with @parameters / @variables / Differential — see neural-pde for the symbolic-spec layer), use the BayesianPINN discretizer instead of PhysicsInformedNN:

using NeuralPDE, Lux, AdvancedHMC

discretization = BayesianPINN(
    chain,
    GridTraining(0.1);
    Kernel  = HMC(0.05, 4),                   # step size, leapfrog steps
    dataset = [u_obs, t_obs],
)

sol = solve(
    pde_system, discretization;
    draw_samples = 1500,
    priorsNNw    = (0.0, 2.0),
)

The result of either path (BNNODE or BayesianPINN) is an object whose sol.original field carries the underlying MCMCChains.Chains, fully compatible with mcmc-diagnostics (R-hat, ESS, ArviZ).

Prior choice for NN weights

The weight prior priorsNNw = (μ, σ) directly controls posterior identifiability. Defaults (0.0, 2.0) are fine for small networks; for wider networks, tighten the prior — wide priors on many weights produce multimodal posteriors that NUTS cannot escape.

Network widthRecommended σRationale
≤ 16 units/layer2.0Default; data informs the posterior
32–64 units/layer1.0Reduce neuron-permutation symmetry basin count
> 64 units/layer0.5 + label-symmetry constraintsOtherwise NUTS almost certainly fails to mix — reach for Pigeons

When NUTS does not mix

Neural-network weight symmetries (neuron permutation, sign flips) create disjoint posterior basins, and HMC bridges them badly. The signature is chains that individually look healthy — good per-chain ESS, no obvious trend — while R-hat stays above 1.01 across chains. More samples do not fix it: the chains are not in the same mode.

The cheap responses, in order:

  1. Tighten priorsNNw (table above). A narrower prior shrinks the permutation basins toward each other.
  2. Shrink the network. Every weight is a dimension of the posterior; a 64-unit net is a 4000-dimensional multimodal target and a 8-unit net may be adequate for a 1-D ODE.
  3. Temper. Parallel tempering with a Gaussian reference — Pigeons.jl, n_chains around 10, n_rounds 10, variational = GaussianReference() — swaps between tempered chains and crosses basins that single-temperature HMC cannot.
  4. Report the multimodality. If modes persist, the honest output is the set of modes with their weights, not the mean across them.

NeuralPDE does not export the log-density as a LogDensityProblems target, so a Pigeons run needs you to reconstruct it from the discretizer's loss (prior term plus physics residual plus data term, up to constants). Before writing that by hand, check whether tightening the prior and shrinking the network removes the need.

Diagnostics

Post-process sol.original, which is a MCMCChains.Chains:

  • R-hat < 1.01 on every NN weight and physical parameter, computed across chains — not per chain
  • ESS of at least a few hundred per parameter; the tail of the weight distribution is usually where it fails first
  • trace plots with no trend and no step: a step means the sampler found a new basin mid-run
  • posterior-predictive solves: draw 50–100 samples from the chain, run solve with each, and overlay against held-out data — the coverage of that band is the claim, not the band's width

For inverse problems, the physical parameters (p) should have narrow posteriors with bands narrower than the priors — if not, the data is not identifying them. Widen the data set or tighten the prior.

Done when

The chain inside sol.original shows R-hat below 1.01 and an effective sample size in the hundreds per parameter; the ODE was in out-of-place form before it reached BNNODE; priorsNNw was set from the network width rather than left at its default; and posterior predictive coverage was checked on held-out data. When R-hat stayed elevated through the warm start, the posterior came from a tempered parallel run (Pigeons) rather than a longer NUTS chain, and the physical parameters' credible bands are narrower than their priors — the evidence that the data identified them.

関連スキル