Repository navigation
Open
Labels
API changesThis impacts the public API of the project (e.g. inference class).enhancementImprovement planned or wanted by the maintainershackathonFor our in-person hackathons with maintainers present, not a general up-for-grabs listinferenceTrainers and potentials in sbi/inference
Milestone
Description
-- inference
----- trainers
--------- npe
------------- npe.py
------------- snpe_a_correction.py
------------- snpe_c_loss.py
Then, the API for NPE (amortized) is:
from sbi.inference import NPE, DirectPosterior
trainer = NPE()
net = trainer.append_simulations(theta, x).train()
posterior = DirectPosterior(net, prior) # Or use `build_posterior()`For SNPE_A, it is:
from sbi.inference import NPE, DirectPosterior, snpe_a_correction
for r in range(3):
theta = proposal.sample((1000,))
x = simulator(theta)
trainer = NPE(density_estimator="Gaussian" if r < 2 else "mdn")
net = trainer.append_simulations(theta, x).train()
proposal_posterior = DirectPosterior(net, prior) # Or use `build_posterior()`
corrected_posterior = snpe_a_correction(proposal_posterior, proposal)
proposal = corrected_posteriorFor SNPE_C (atomic), it is:
from sbi.inference import NPE, DirectPosterior, snpe_c_atomic_loss
# First round is standard NPE.
theta, x = simulate_for_sbi(prior, simulator)
trainer = NPE()
net = trainer.append_simulations(theta, x).train()
proposal = DirectPosterior(net, prior).set_default_x(x_o) # Or use `build_posterior()`
# Later rounds use the APT loss.
for _ in range(1, 3):
theta, x = simulate_for_sbi(proposal, simulator)
net = trainer.append_simulations(theta, x).train(loss=snpe_c_atomic_loss)
proposal = DirectPosterior(net, prior).set_default_x(x_o) # Or use `build_posterior()`For SNPE_C (non-atomic), the only difference is that one would also pass proposal=proposal to append_simulations(), and one has to use MDNs.
Metadata
Metadata
Assignees
Labels
API changesThis impacts the public API of the project (e.g. inference class).enhancementImprovement planned or wanted by the maintainershackathonFor our in-person hackathons with maintainers present, not a general up-for-grabs listinferenceTrainers and potentials in sbi/inference
Activity
gmoss13 commentedon Feb 26, 2025
Some additional context. Currently, the inference structure is:
-- inference
----- trainers
--------- npe
------------- npe_base.py
------------- npe_a.py
------------- npe_b.py (which is not implemented)
------------- npe_c.py
The difference between
npe_aandnpe_cis only in how we deal with the proposal over several rounds (i.e. via an atomic loss as innpe_cor by correcting the proposal as innpe_a. It would be good to seperate this correction from the classes so that it can be exposed to writing custom training loops as above. The overallinferencestructure should remain backwards compatible, i.e. the user should still be able to importNPE_CandNPE_A(and theSNPE_ASNPE_Caliases), which should function as before.jnsbck commentedon Apr 15, 2025
Can make a seperate issue, but I just noticed that the tutorials need updating with the new
DirectPosteriorAPI.Refactor NPE losses to Strategy Pattern (Phase 1) - fixes sbi-dev#1241 …
patelshivani2283-lab commentedon Apr 16, 2026
Hi
I’m interested in working on this issue.
Before starting, I wanted to ask if there’s a specific part of this refactor that would be a good entry point (e.g separating the loss logic or restructuring the trainers). I’d also appreciate any pointers on the expected design direction.
Thanks!
janfb commentedon Apr 16, 2026
Hi @patelshivani2283-lab
Thanks for offering your help here!
This issues is already being worked on, see the PR #1755.
We are currently a bit behind with reviewing PRs and curating issues, but please have a look at other open issues to work on.
Thanks 🙏
Jan