Repository navigation
Integrate DensityEstimatorBuilder into NLE - #1904
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## gsoc-2026 #1904 +/- ##
============================================
Coverage ? 87.99%
============================================
Files ? 144
Lines ? 13499
Branches ? 0
============================================
Hits ? 11879
Misses ? 1620
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more.
|
janfb
left a comment
There was a problem hiding this comment.
Clean PR @satwiksps 👏 The one thing to fix is a side effect on MNLE: because you (correctly) added the string deprecation to the shared NLE base, MNLE's default "mnle" now raises it with wrong advice. MNPE already sidesteps this by converting its string to a callable before calling super(). You can just mirror that one line in MNLE and add a no-warning test. On the tests we can reduce the code duplication between NPE and NLE integration tests by using parametrize, see comments below.
There was a problem hiding this comment.
good to unify the NPE and NLE tests into one file. We can go even one step further and use parametrize on the NLE and NPE objects on single tests that are currently duplicated as nearly twin-tests for NPE and NLE in this file. This should work because we now have a unified API.
e.g., sth like:
TRAINERS = [(NPE_C, posterior_nn, "theta"), (NLE_A, likelihood_nn, "x")] # input_is
@pytest.mark.parametrize("trainer,factory", [(t, f) for t, f, _ in TRAINERS], ids=["npe", "nle"])
def test_string_emits_deprecation_warning(trainer, factory):
prior = MultivariateNormal(zeros(2), eye(2))
with pytest.warns(FutureWarning, match="deprecated"):
trainer(prior, density_estimator="maf", show_progress_bars=False)And similar for many of the other tests in this file.
We can even extend this to MNPE and MNLE later when they are introduced as well. What do you think?
You could do this for all twin tests. But try to find a balance with saving test code and adding test complexity. If it's hard or just adds a lot of complexity to find a general way to write the test, keep the twin tests.
| `.sample()` and must return a `ConditionalDensityEstimator`. | ||
| density_estimator: If it is a string (deprecated), use a pre-configured | ||
| network of the provided type (one of nsf, maf, mdn, made). If it is | ||
| a `DensityEstimatorBuilder``, the builder's `build()` method will be |
| The naming follows the internal convention of ``posterior_nn``: the | ||
| caller passes ``batch_theta`` as the modeled variable and ``batch_x`` | ||
| as the conditioning variable. | ||
|
|
| signature as the functions produced by `likelihood_nn`. | ||
| """ | ||
|
|
||
| def build_fn(batch_theta, batch_x): |
There was a problem hiding this comment.
add a small comment here: # NLE models p(x|θ): input=x, condition=θ
janfb
left a comment
There was a problem hiding this comment.
Looks good, thanks @satwiksps ![]()
| assert estimator.condition_shape == torch.Size([num_dim_theta]) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("trainer_cls", [MNLE, MNPE], ids=["mnle", "mnpe"]) |
* change build to neutral batch_input and batch_condition signature * update NPE wrap builder for role neutral build * wire DensityEstimatorBuilder into LikelihoodEstimatorTrainer * update NLE_A type hints and docstring fro builder support * rename and extend tests for NPE and NLE builder integration * update old batch_x batch_theta call in pr2 test file * fix typo and double blank line * convert MNLE string to callable before super to avoid spurious warning * parametrize twin tests and add MNLE MNPE no warning regression * minor ruff fixes
What does this PR do?
This PR continues the Neural Network (NN) Builder API refactor project under GSoC 2026. It does two things:
build()to a role neutral signature : the_EstimatorBuilderBase.build()andDensityEstimatorBuilder.build()methods now take(batch_input, batch_condition)instead of the NPE-specific(batch_theta, batch_x).DensityEstimatorBuilderinto the NLE trainer : similar to what PR Integrate DensityEstimatorBuilder into NPE trainers #1882 did for NPE, this wires builder support intoLikelihoodEstimatorTrainerandNLE_A. Users can now pass a builder object as thedensity_estimatorargument instead of a string or callable.Files Changed
estimator_configs.py:build()params from(batch_theta, batch_x)to(batch_input, batch_condition)on both_EstimatorBuilderBaseandDensityEstimatorBuilderand then updated docstrings.npe_base.py:build(batch_input=batch_theta, batch_condition=batch_x)nle_base.py:DensityEstimatorBuilderand_EstimatorBuilderBaseimports and changeddensity_estimatordefault from"maf"toNone_wrap_builderwith NLE role assignment:build(batch_input=batch_x, batch_condition=batch_theta)the opposite of NPE, since NLE modelsp(x|θ)notp(θ|x)nle_a.py:density_estimatortype union and default to match the base classnpe_nle_builder_integration_test.pynpe_builder_integration_test.pyand added tests for likelihood estimation in same fileAI Usage
Does this close any issues?
N/A
Any relevant code examples, logs, or error messages?
N/A