Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Density Estimator Class #952

Merged
merged 6 commits into from
Feb 28, 2024
Merged

Density Estimator Class #952

merged 6 commits into from
Feb 28, 2024

Conversation

gmoss13
Copy link
Contributor

@gmoss13 gmoss13 commented Feb 20, 2024

fix #932

Goals:

  • General density estimator class to wrap around different models (Flows, MDNs, ...).
  • Training Objectives for SNPE, SNRE, SNLE taking density estimators as args
  • Reduce code repetition of .train() methods

@gmoss13 gmoss13 linked an issue Feb 20, 2024 that may be closed by this pull request
@gmoss13
Copy link
Contributor Author

gmoss13 commented Feb 20, 2024

@michaeldeistler @janfb this is bare bones at the moment, could use some feedback. Specifically, how much do we want DensityEstimator to handle? Should it already implement different potentials and samplers?

Copy link
Contributor

@michaeldeistler michaeldeistler left a comment

Choose a reason for hiding this comment

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

Overall, I like the structure. I believe that a more natural file location would be sbi/neural_nets/density_estimators/....

Beyond that I made many detailed comments below. Most important would be:

  1. I do not think that the FlowDensityEstimator is working right now because it does not follow the nflows API.
  2. Please write of the FlowDensityEstimator, even if they are only API tests.

sbi/density_estimators/__init__.py Outdated Show resolved Hide resolved
sbi/density_estimators/base.py Outdated Show resolved Hide resolved
sbi/density_estimators/base.py Outdated Show resolved Hide resolved
sbi/density_estimators/base.py Outdated Show resolved Hide resolved
sbi/density_estimators/base.py Outdated Show resolved Hide resolved
sbi/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/density_estimators/flow.py Outdated Show resolved Hide resolved
@michaeldeistler
Copy link
Contributor

As for your question: I think the scope of the DensityEstimator is just fine. It should not handle potentials and samplers.

@gmoss13
Copy link
Contributor Author

gmoss13 commented Feb 22, 2024

Thanks for the comments! I applied most of the suggestions, but have some questions regarding a couple of your comments, where I could use some extra clarification.

tests/density_estimator_test.py Outdated Show resolved Hide resolved
tests/density_estimator_test.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/base.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
@michaeldeistler
Copy link
Contributor

michaeldeistler commented Feb 27, 2024

New PR:

  • utils.posterior_nn() should return wrapped density estimator --> add tests
  • rewrite .train() to use .loss and MCMCPosterior.log_prob() should use DensityEstimator.log_prob() and DirectPosterior.sample() should use DensityEstimator.sample(), etc...

@gmoss13
Copy link
Contributor Author

gmoss13 commented Feb 27, 2024

Thanks again for the comments! New commit should resolve these.

New PR:

  • utils.posterior_nn() should return wrapped density estimator --> add tests
  • rewrite .train() to use .loss and MCMCPosterior.log_prob() should use DensityEstimator.log_prob() and DirectPosterior.sample() should use DensityEstimator.sample(), etc...

Once this PR is is merged, we can work on these!

Copy link

codecov bot commented Feb 27, 2024

Codecov Report

Attention: Patch coverage is 89.65517% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 76.08%. Comparing base (3aeb775) to head (0853270).

Files Patch % Lines
sbi/neural_nets/density_estimators/base.py 72.72% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #952      +/-   ##
==========================================
+ Coverage   76.02%   76.08%   +0.06%     
==========================================
  Files          80       83       +3     
  Lines        6332     6361      +29     
==========================================
+ Hits         4814     4840      +26     
- Misses       1518     1521       +3     
Flag Coverage Δ
unittests 76.08% <89.65%> (+0.06%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gmoss13 gmoss13 marked this pull request as ready for review February 27, 2024 11:23
Copy link
Contributor

@janfb janfb left a comment

Choose a reason for hiding this comment

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

great work! 👏

Added another round of comments. Happy to discuss later when we meet.

sbi/neural_nets/density_estimators/base.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/base.py Show resolved Hide resolved
tests/density_estimator_test.py Outdated Show resolved Hide resolved
tests/density_estimator_test.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
sbi/neural_nets/density_estimators/flow.py Outdated Show resolved Hide resolved
@gmoss13 gmoss13 requested a review from janfb February 27, 2024 16:49
@gmoss13 gmoss13 changed the title [WIP] - Density Estimator Class Density Estimator Class Feb 27, 2024
Copy link
Contributor

@manuelgloeckler manuelgloeckler left a comment

Choose a reason for hiding this comment

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

Great, thanks for finishing it. Let's get it merged.

@manuelgloeckler manuelgloeckler merged commit 9d6d13c into main Feb 28, 2024
3 checks passed
Copy link
Contributor

@janfb janfb left a comment

Choose a reason for hiding this comment

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

looks good, thanks a lot!

I think the unticked goals in the PR description can be moved to future PRs / separate issues.

@janfb janfb deleted the general_density_estimator branch February 28, 2024 08:18
@francois-rozet
Copy link

francois-rozet commented Feb 28, 2024

Hello, just a comment that it might be worth adding a sample_and_log_prob or rsample_and_log_prob method to the DensityEstimator interface as some flows cannot (or very slowly) get the density of arbitrary values, but can get the density of their own samples. This is the case of inverse autoregressive flows (IAFs, basically reversed MAFs), which are very fast to sample from but whose density is slow to evaluate. This could be beneficial for algorithms that rely on samples and their density (almost all SNPE methods).

By the way, Zuko flows already have an rsample_and_log_prob method 😉

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.

New structure for loss
5 participants