to_pyro_distribution log_prob crashes for one observation without a plate

Open
#2,302 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

@M-Colley is already working on this.

Since Dec 3, 2026.

  • #2304 by @M-Colley — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python, pytorch

Research direction

Read sbi/utils/pyroutils.py, especially ConditionalDensityEstimatorDistribution.log_prob(), and the existing cases in tests/pyroutils_test.py. Add coverage for a single observation without pyro.plate, checking that log_prob returns a scalar and that the reported failure is addressed; the issue also proposes an MCMC regression check. PR #2304 is already open against this issue, so check its status before starting.

Written by the indexing model from the issue text.

Description

Bug description

When using a trained likelihood in a Pyro model with a single observation and without pyro.plate, calling pyro.sample() with to_pyro_distribution(likelihood, theta) raises a TypeError. The same model works when the observation is wrapped inside pyro.plate("trials", 1).

The issue comes from ConditionalDensityEstimatorDistribution.log_prob() in sbi/utils/pyroutils.py, line 135:

return lp.reshape(*sample_shape, *self.batch_shape)

When there is only one observation and no plate, both sample_shape and self.batch_shape are empty, so the call becomes lp.reshape(), which raises an error because PyTorch expects a shape argument.

The existing tests in tests/pyroutils_test.py always use a plate, even for a single trial, so this case is not covered.

Minimal code example
import pyro
import pyro.distributions as pdist
import torch
from pyro.infer import MCMC, NUTS

from sbi.inference import NLE
from sbi.utils import BoxUniform
from sbi.utils.pyroutils import to_pyro_distribution

prior = BoxUniform(-torch.ones(2), torch.ones(2))
theta = prior.sample((500,))
x = theta + 0.1 * torch.randn_like(theta)

likelihood = NLE(prior).append_simulations(theta, x).train(max_num_epochs=5)

x_o = torch.tensor([0.5, -0.5])

def model():
    th = pyro.sample("theta", pdist.Uniform(-torch.ones(2), torch.ones(2)).to_event(1))
    pyro.sample("x", to_pyro_distribution(likelihood, th), obs=x_o)

MCMC(NUTS(model), num_samples=100, warmup_steps=50).run()
Error message
  File "sbi/utils/pyroutils.py", line 135, in log_prob
    return lp.reshape(*sample_shape, *self.batch_shape)
TypeError: reshape() missing 1 required positional arguments: "shape"
Expected behavior

The model should evaluate the log probability of a single observation without requiring a pyro.plate. The log probability should be a scalar with shape (), consistent with the behavior when using pyro.plate("trials", 1).

Proposed fix

Replace:

return lp.reshape(*sample_shape, *self.batch_shape)

with:

return lp.reshape(sample_shape + self.batch_shape)

Concatenating the two torch.Size objects always passes a valid shape to reshape(), including when both shapes are empty.

Verification

I tested the change locally. With a fully trained network (2000 simulations), the Pyro posterior mean was approximately (0.49, -0.48), which matches the posterior mean from sbi's own NLE posterior with the same network, approximately (0.49, -0.49).

Proposed regression test

A test in tests/pyroutils_test.py that:

  1. uses a single observation without pyro.plate;
  2. checks that log_prob returns a scalar with shape ();
  3. checks that MCMC runs without raising an exception.
Additional context

PR #2254 modifies the same function but does not change this reshape, so the issue remains with it.

Environment:

  • sbi: main (3968ef24)
  • Python: 3.12.16
  • PyTorch: 2.15.0
  • Pyro: 1.9.2
  • Device: CPU
  • Inference method: NLE (to_pyro_distribution)
  • Rounds: single-round

I used an AI coding assistant to help identify and verify this bug.

Dominant language
Python
Stars
894
Forks
306
Avg merge
5d 21h
Merged PRs (30d)
61

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sbi-dev/sbi

All issues in sbi-dev/sbi

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.