agentleFS
90184479018447/julia-pro-p/skills/bayesian-pinn/SKILL.md

bayesian-pinn

Skill0 starsChanged 3 days agoMITView on GitHub

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.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Shrink the network: Every weight is a dimension of the posterior.

    line 95
  2. Distinctive:

    Report the multimodality: If modes persist, the honest output is the set of modes with their weights, not the mean across them.

    line 97

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.