Skip to content

Posterior methods give internal errors for theta of the wrong shape #2136

Done

Description

@janfb

The fix for issue #2125 checks x against the shape of one observation when it enters a posterior. Theta has no such check: a theta with the wrong number of parameters fails deep inside the potential with an error that does not name theta.

posterior.log_prob(torch.zeros(4, 3), x=x_o)  # prior has 2 parameters
# RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1

Fix direction: check theta against the event shape of the prior in potential() and log_prob(), next to the check of x, and raise a ValueError such as "theta has 3 entries per row, but the prior has 2".

Related: bound with x_is_iid=False and a single x, the NPE potential pairs that x with every theta, while the NLE and NRE potentials raise because the batch sizes differ. They should behave the same.

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. added
    bug
    posteriors
    on Nov 1, 2026
  3. Ashwini8686 commented on Nov 13, 2026

    @Ashwini8686
    Contributor

    Hey @janfb! Quick update—I went ahead and put together the fix following your suggestion.

    I added checks right at the entry points of DirectPosterior.log_prob() and NeuralPosterior.potential() to validate incoming theta against self._theta_dim. Now it throws a clean ValueError upfront when shapes don't match instead of letting PyTorch blow up deep down with cryptic expansion errors.

    I also added a regression test (test_direct_posterior_shape_validation) in tests/posterior_sampler_test.py and everything passes cleanly locally.

    Could you assign this issue to me? Let me know if this approach looks good to you and I'll open up a PR for review!

  4. Ashwini8686 commented on Nov 15, 2026

    @Ashwini8686
    Contributor

    Hello @janfb ,
    Thanks for merging #2120! Just following up briefly on #2136 —I have the fix and test ready locally, so let me know if it's okay to open the PR.

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

Metadata

Metadata

Assignees

Labels

bugposteriors

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions