You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NPE can use several iid observations through its potential: with x_is_iid=True, it sums the log posteriors of the trials and corrects for the prior. Three things go wrong around this.
The potential counts the trials by the first dimension of x, not against the condition shape of the estimator. For a 2x2 observation passed without batch dimension, it counts two trials. MCMC binds observations as iid, so it then samples q(θ|x_o)² / p(θ) instead of q(θ|x_o), without an error.
map() binds x without x_is_iid, and the NPE potential defaults to a batch. So map() of an MCMC posterior on an NPE potential fails with a batch size assertion, while sample() of the same posterior works. With init_method="posterior" it works by accident, because sample() rebinds the potential as iid before the gradient ascent. The joint map() of an ensemble of such posteriors fails the same way.
The rejection and importance sampling posteriors bind without x_is_iid too, so their sample() and log_prob() fail with the same assertion.
Minimal code example
posterior=npe.build_posterior(sample_with="mcmc")
posterior.set_default_x(x_iid) # two iid observationsposterior.sample((1000,)) # worksposterior.map() # AssertionError; sample_with="rejection" or "importance" fails in sample()
Error message or traceback
AssertionError: Batch size mismatch: 1000 and 2. When performing batched sampling for multiple `x`, the batch size of `theta` must match the batch size of `x`.
Proposed fix
I think the potential should count the trials against the condition shape of the estimator, in one place. Then map(), MCMC, rejection and importance sampling can all bind several observations as iid trials. I tried the map() part in PR #2106 and took it out again, because it needs that count first.
@aniketpandey05 thanks for the offer. This goes together with issue #2122 into the posterior refactoring we do internally, so it is not open for a PR. Your other PRs are welcome: I will review PR #2128 and PR #2129 after November 9.
Activity
aniketpandey05 commentedon Oct 30, 2026
@janfb if not assigned may i pick it up?
janfb commentedon Oct 31, 2026
@aniketpandey05 thanks for the offer. This goes together with issue #2122 into the posterior refactoring we do internally, so it is not open for a PR. Your other PRs are welcome: I will review PR #2128 and PR #2129 after November 9.
Thanks, Jan
fix: count iid trials by the observation shape, and combine iid obser…