Skip to content

Amortized VI: sample() and log_prob() should take one observation, like the other posteriors #2141

Closed

Description

@janfb

After train_amortized(), VIPosterior.sample() and log_prob() treat several observations as a batch:

posterior.train_amortized(theta, x)
x_3 = x[:3]
posterior.sample((10,), x=x_3).shape        # (10, 3, theta_dim): one posterior per observation
posterior.log_prob(theta[:3], x=x_3)        # theta_i under q(theta | x_i), as pairs

The other posteriors use sample() and log_prob() for one observed data set. The direct posterior raises for several observations and points to sample_batched() and log_prob_batched(). MCMC, importance, rejection and single-x VI posteriors combine several observations as iid trials. Since PR #2140, amortized VI potential() and map() also raise for several observations. So sample() and log_prob() are the last methods of amortized VI that pair theta with x.

The how-to guide on VI already describes the intended use: sample() for one observation, sample_batched() for several, and log_prob() one observation at a time.

What should change:

  • Amortized VI sample() and log_prob() take one observation, and raise the direct posterior's error for several.
  • sample_batched() returns one posterior per observation, as sample() does now.
  • A new log_prob_batched() follows the direct posterior: theta of shape (num_samples, num_observations, theta_dim), and one value per sample and observation.

Tests: sample() and log_prob() raise for several observations; log_prob_batched() equals log_prob() for each observation; the shapes match the direct posterior's batched methods. The docstrings and the how-to guide need an update. Issue #2137 is about amortized VI as well.

Activity

  1. fruitstash commented on Nov 2, 2026

    @fruitstash

    Hey @janfb I came across this issue and was wondering if you're already working on it or planning to take it up. If not, would you be okay with me putting forward a proposal and working on it? Just wanted to check before proceeding.

  2. BHARATH0153 commented on Nov 2, 2026

    @BHARATH0153
    Contributor

    @janfb This can reuse what #2140 already adds: _process_x() validates x against potential_fn.x_event_shape, and _check_num_observations() gives the shared "takes one observation" error that amortized potential()/map() use there.
    Amortized sample() / log_prob(): process x, then call _check_num_observations(x.shape[0]). Factor the flow call into a private helper so sample_batched() still returns (*sample_shape, num_obs, theta_dim).
    With one observation, log_prob() broadcasts theta over that x; the current theta/x pairing goes away. Add log_prob_batched() mirroring DirectPosterior.log_prob_batched: theta (*sample_shape, num_obs, theta_dim) -> (*sample_shape, num_obs).
    Update both docstrings and the amortized section of docs/how_to_guide/26_variational_inference.ipynb, which still says VI has no log_prob_batched().
    Tests in tests/vi_test.py: both methods raise for several observations, log_prob_batched equals log_prob per observation, and shapes match the direct posterior's batched methods. Keep the existing sample_batched shape test.

  3. added 3 commits that reference this issue on Nov 17, 2026
    933e7af
    573359a
    9c74572
  4. added a commit that references this issue on Nov 23, 2026
    3ba77f0
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions