Skip to content

Fixes current density estimator bug - #1155

Merged
manuelgloeckler merged 2 commits into
mainfrom
1154-density-estimator-batched-sample-mixes-up-samples-from-different-posteriors
May 7, 2024
Merged

manuelgloeckler merged 2 commits into
mainfrom
1154-density-estimator-batched-sample-mixes-up-samples-from-different-posteriors

Conversation

@manuelgloeckler

Copy link
Copy Markdown
Contributor

What does this implement/fix? Explain your changes

Fixes #1154

TODO

  • Add tests to catch this. (check for other density estimators

@codecov

codecov Bot commented May 7, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.23%. Comparing base (9a8c7c0) to head (df8899a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1155      +/-   ##
==========================================
- Coverage   83.93%   74.23%   -9.71%     
==========================================
  Files          90       90              
  Lines        6930     6931       +1     
==========================================
- Hits         5817     5145     -672     
- Misses       1113     1786     +673     
Flag Coverage Δ
unittests 74.23% <100.00%> (-9.71%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
sbi/neural_nets/density_estimators/nflows_flow.py 63.46% <100.00%> (+0.71%) ⬆️

... and 23 files with indirect coverage changes

@michaeldeistler michaeldeistler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a ton!

*sample_shape,
condition_batch_dim,
)
+ self.input_shape

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! Would

(*sample_shape, condition_batch_dim, *self.input_shape)

also work? It's just a bit cleaner IMO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jup, fixed.

@manuelgloeckler

Copy link
Copy Markdown
Contributor Author

Added tests that check if batched log_prob/sample will be the same as unbatched. Will merge after tests passed.

@manuelgloeckler
manuelgloeckler merged commit fe55b1c into main May 7, 2024
dgedon added a commit that referenced this pull request Nov 23, 2026
…d and unconditional estimator defaults (#2158)

* fix(estimators): keep nflows sample_and_log_prob samples with their condition

nflows returns samples and log probs as (batch, sample). `sample_and_log_prob`
reshaped them without the transpose that `sample()` got in #1155, so with
several conditions it filed half of the samples and their log probs under the
wrong condition. The #1155 test now also checks `sample_and_log_prob` against
`log_prob`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(estimators): use self.net in the unconditional density estimator base

`UnconditionalDensityEstimator.log_prob` and `sample` used `self._neural_net`,
which is never set, so subclasses that keep these methods raised an
AttributeError. They now use `self.net` and keep the `eval()` call from #1470.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(samplers): thin serial slice_np chains once

`SliceSamplerSerial.run(n)` built each chain's `SliceSampler` with `thin`, which
already takes `thin` steps per kept sample, and then thinned the result again.
With `thin > 1` this cost `thin` times more potential evaluations and kept every
`thin**2`-th step. The chain sampler now uses `thin=1`, so `run(n)` takes `n`
steps and thins once, as `SliceSamplerVectorized` does. Seeded `slice_np`
results with `thin > 1` change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(posteriors): make mcmc_method and set_mcmc_method set the sampling method

`MCMCPosterior.mcmc_method` read `self._mcmc_method`, which `__init__` never
sets, and `set_mcmc_method` wrote that attribute while sampling reads
`self.method`. So the getter raised, and after setting a method the posterior
reported it but kept sampling with the old one. Both now use `self.method`, and
`set_mcmc_method` raises for unknown methods, as `__init__` does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(estimators): leave train/eval mode to the caller in unconditional log_prob

`UnconditionalDensityEstimator.log_prob` called `self.net.eval()`, so a subclass
whose `loss` uses `log_prob` was trained with Dropout and BatchNorm in eval mode.
The trainer sets the mode, as for the other estimators.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* refactor: type set_mcmc_method as MCMCPosterior, reshape as in sample()

`set_mcmc_method` returns the posterior itself, so its signature and docstring
now say `MCMCPosterior` and type checkers follow chained calls.
`NFlowsFlow.sample_and_log_prob` reshapes samples to `self.input_shape`, as
`sample()` does; the shapes are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Density Estimator batched sample mixes up samples from different posteriors

2 participants