Skip to content

New organization of SNPE methods #1241

Open

Description

@michaeldeistler

-- 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_posterior

For 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.

Activity

  1. added
    enhancement
    on Aug 28, 2024
  2. added
    API changes
    hackathon
    on Feb 26, 2025
  3. gmoss13 commented on Feb 26, 2025

    @gmoss13
    Contributor

    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_a and npe_c is only in how we deal with the proposal over several rounds (i.e. via an atomic loss as in npe_c or by correcting the proposal as in npe_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 overall inference structure should remain backwards compatible, i.e. the user should still be able to import NPE_C and NPE_A (and the SNPE_A SNPE_C aliases), which should function as before.

  4. jnsbck commented on Apr 15, 2025

    @jnsbck
    Contributor

    Can make a seperate issue, but I just noticed that the tutorials need updating with the new DirectPosterior API.

  5. added a commit that references this issue on Mar 25, 2026
    0544f19
  6. patelshivani2283-lab commented on Apr 16, 2026

    @patelshivani2283-lab
    Contributor

    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!

  7. janfb commented on Apr 16, 2026

    @janfb
    Contributor

    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

  8. added
    inference
    on Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API changesenhancementhackathoninference

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions