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

RatioEstimator abstraction #1097

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

RatioEstimator abstraction #1097

wants to merge 11 commits into from

Conversation

bkmi
Copy link
Contributor

@bkmi bkmi commented Mar 22, 2024

What does this implement/fix? Explain your changes

It introduces a RatioEstimator abstraction. It will wrap neural networks that process x and theta to estimate ratios. The base class is suggestive of how to create extensions that use alternative data types (dict, etc.) while flexible enough to handle how embedding works and how embedded data is combined. Natural extensions include ratio estimators with specific network architectures such as transformers or convnets.

This implements #992 which is the ratio specific issue for #1046. It also implements the ratio part of #957 and makes the documentation confirm to the shape goals of #1041.

Does this close any currently open issues?

Closes:
#992

Also #1036 since it eliminates a confusingly name and now-irrelevant class.

The others are more general and require more work on subjects related to ratios before closing.

Any relevant code examples, logs, error output, etc?

Nope

Any other comments?

There is a test that doesn't pass, namely #1090, but I think it is not because of my changes.

Checklist

Put an x in the boxes that apply. You can also fill these out after creating
the PR. If you're unsure about any of them, don't hesitate to ask. We're here to
help! This is simply a reminder of what we are going to look for before merging
your code.

  • I have read and understood the contribution
    guidelines
  • I agree with re-licensing my contribution from AGPLv3 to Apache-2.0.
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have reported how long the new tests run and potentially marked them
    with pytest.mark.slow.
  • New and existing unit tests pass locally with my changes
  • I performed linting and formatting as described in the contribution
    guidelines
  • I rebased on main (or there are no conflicts with main)

@bkmi
Copy link
Contributor Author

bkmi commented Mar 22, 2024

I added a test. it's extremely simple, but it checks that the estimated ratios are the correct size . (like log_prob).

Copy link
Contributor

@tomMoral tomMoral left a comment

Choose a reason for hiding this comment

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

A few comments on the base class, to match with what has been discussed in #1046 and #1072

sbi/neural_nets/ratio_estimators.py Outdated Show resolved Hide resolved
sbi/neural_nets/ratio_estimators.py Outdated Show resolved Hide resolved
sbi/neural_nets/ratio_estimators.py Outdated Show resolved Hide resolved
sbi/neural_nets/ratio_estimators.py Outdated Show resolved Hide resolved
@bkmi
Copy link
Contributor Author

bkmi commented Mar 25, 2024

I will follow up after Friday.

@bkmi bkmi requested a review from tomMoral April 9, 2024 13:52
@bkmi
Copy link
Contributor Author

bkmi commented Apr 9, 2024

@tomMoral I think this is done now.

Copy link

codecov bot commented Apr 9, 2024

Codecov Report

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

Project coverage is 77.25%. Comparing base (46db263) to head (8bae751).
Report is 10 commits behind head on main.

❗ Current head 8bae751 differs from pull request most recent head 9c39ff1. Consider uploading reports for the commit 9c39ff1 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1097      +/-   ##
==========================================
- Coverage   85.26%   77.25%   -8.02%     
==========================================
  Files          89       90       +1     
  Lines        6616     6629      +13     
==========================================
- Hits         5641     5121     -520     
- Misses        975     1508     +533     
Flag Coverage Δ
unittests 77.25% <95.55%> (-8.02%) ⬇️

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

Files Coverage Δ
sbi/inference/potentials/ratio_based_potential.py 100.00% <100.00%> (ø)
sbi/inference/snre/snre_base.py 94.28% <100.00%> (ø)
sbi/neural_nets/__init__.py 100.00% <ø> (ø)
sbi/neural_nets/classifier.py 100.00% <100.00%> (ø)
sbi/neural_nets/ratio_estimators.py 92.59% <92.59%> (ø)

... and 23 files with indirect coverage changes

Copy link
Contributor

@tomMoral tomMoral left a comment

Choose a reason for hiding this comment

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

A few extra comments but overall it looks good!

sbi/neural_nets/classifier.py Outdated Show resolved Hide resolved
sbi/neural_nets/ratio_estimators.py Show resolved Hide resolved
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.

Hi Ben,

thanks a lot! The shape-PR for DensityEstimators is finally merged in #1066 so we can start pushing this along. See a few questions below.

sbi/neural_nets/ratio_estimators.py Outdated Show resolved Hide resolved
sbi/neural_nets/ratio_estimators.py Show resolved Hide resolved
"theta_shape", ((1,), (2,), (1, 1), (2, 2), (1, 1, 1), (2, 2, 2))
)
@pytest.mark.parametrize("x_shape", ((1,), (2,), (1, 1), (2, 2), (1, 1, 1), (2, 2, 2)))
def test_api_ratio_estimator(ratio_estimator, theta_shape, x_shape):
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a test that checks if 2D data (images) can be handled with appropriate embedding nets.

sbi/neural_nets/ratio_estimators.py Show resolved Hide resolved
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.

Thanks for pushing this along! 👏

I think it makes sense that @michaeldeistler reviews this.

@bkmi
Copy link
Contributor Author

bkmi commented Apr 22, 2024

There is a wip push, but supporting sample dim will require:

  • making warnings that it is not allowed to be used in loss function computations

@bkmi
Copy link
Contributor Author

bkmi commented Apr 25, 2024

goals:

  • no abstraction, only one RatioEstimator
  • move RatioEstimator out of neural_nets
  • primarily RatioEstimator keeps track of x and theta shapes
  • simple check shapes, i.e. (1, 1, *event_shape) for log_prob equivalent.
  • RatioEstimator can have embeddings
  • make sure that the loss is computed in the learning algorithm
  • update tests

relevant issues:
#1066
#1149

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.

None yet

4 participants