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
{{ message }}
Repository navigation
Amortized VI: sample() and log_prob() should take one observation, like the other posteriors #2141
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 observationposterior.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.
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.
@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.
Activity
fruitstash commentedon Nov 2, 2026
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.
BHARATH0153 commentedon Nov 2, 2026
@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.
fix: amortized VI sample() and log_prob() take one observation
fix: amortized VI sample() and log_prob() take one observation
fix: amortized VI sample() and log_prob() take one observation
fix!: amortized VI sample() and log_prob() take one observation (#2193)