Skip to content

NPE with several iid observations fails in the importance, rejection and VI posteriors #2122

Closed as duplicate

Description

@janfb

With NPE, an x with several rows fails in the importance, rejection and VI posteriors, and in potential() of every posterior. Only log_prob() and sample() of the MCMC posterior treat the rows as iid observations.

The NPE potential supports iid observations since PR #1810 (issue #1804). PR #1813 made the MCMC posterior bind x with x_is_iid=True in log_prob() and sample(). All other posterior methods bind x without this flag. The NPE potential then defaults to x_is_iid=False: it treats the rows as separate observations and asserts that theta and x have the same batch size.

import torch
from sbi.inference import NPE
from sbi.utils import BoxUniform

prior = BoxUniform(-3 * torch.ones(2), 3 * torch.ones(2))
theta = prior.sample((200,))
trainer = NPE(prior)
trainer.append_simulations(theta, theta + 0.1 * torch.randn_like(theta)).train()
posterior = trainer.build_posterior(sample_with="importance")
posterior.log_prob(torch.zeros(1, 2), x=torch.zeros(3, 2))
# AssertionError: Batch size mismatch: 1 and 3.
Posterior Fails for an x with 3 rows
Importance log_prob(), sample(), estimate_normalization_constant(), potential()
Rejection log_prob(), sample(), potential()
VI train(), potential()
MCMC potential() only

The fix: these methods bind x with x_is_iid=True, as the MCMC posterior does. NLE and NRE potentials already default to x_is_iid=True, so their results stay the same. A test checks for each posterior that potential() with several rows equals the potential bound with x_is_iid=True, and that sampling runs.

Found in PR #2107, where CodeRabbit raised it for the importance posterior.

Activity

  1. BHARATH0153 commented on Oct 29, 2026

    @BHARATH0153
    Contributor

    @janfb I'd like to take this one. The failing calls all bind x without the flag so the NPE potential falls back to x_is_iid=False:importance log_prob/sample,rejection log_prob/sample, VI train, and the shared potential() (which is also where
    estimate_normalization_constant lands) Plan is to pass x_is_iid=True at those spots, the same way MCMC does in
    mlog_prob/sample, and keep it False where each theta is deliberately paired with its own x (VI train_amortized, MCMC sample_batched). One thing I'd like your call on:
    doing it in the base potential() also affects FMPE/NPSE, whose VectorFieldPotential
    defaults to False. but tell me if you'd
    rather scope this to the NPE family.
    Test would be parametrized over the four posteriors: potential() with a 3-row x must
    match the potential bound with x_is_iid=True, and sample() must run

  2. self-assigned this
    on Oct 31, 2026
  3. janfb commented on Oct 31, 2026

    @janfb
    ContributorAuthor

    Tracked together with issue #2125: the NPE potential first has to count the iid trials correctly, then all posteriors can bind several rows of x as iid. Closing in favour of issue #2125.

    Thanks, Jan

  4. BHARATH0153 commented on Nov 1, 2026

    @BHARATH0153
    Contributor

    Understood, and sorry for getting ahead of the scope question on #2122 — I should have waited for an answer before opening this, as we agreed on #2083.

    Thanks for the pointer to #2125. Agreed that binding x as iid in more posteriors would spread the wrong trial count, so this and the reduced-trial correction should land together rather than as a separate change. I'll leave #2122 to your internal fix and won't push further on it.

    The branch stays as-is if the test setup is useful as a reference, but I won't open anything else on it.

    I'll keep focusing on #2123 and stay within that scope.

    Thanks for the review and the clear explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions