Conversation
…v/sbi into 1446-add-api-for-guidance
- add pytest-split plugin to dev dependencies - use split in ci workflow based on test durations in .test_durations
…v/sbi into 1446-add-api-for-guidance
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #1482 +/- ##
==========================================
- Coverage 88.54% 83.83% -4.72%
==========================================
Files 137 136 -1
Lines 11515 11797 +282
==========================================
- Hits 10196 9890 -306
- Misses 1319 1907 +588
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Update the branch
…her i.e. score -> vector field
There was a problem hiding this comment.
Great work @manuelgloeckler 👏 Great to have PriorGuide in here as well 🚀
added mostly minor formatting and edge case handling comments.
More high-level, I am concerned about the long signature sample(...) has by now. We should think about introducing config classes for the different methods (sde config, iid, guidance), or method chaining (see comment below). But this would be a larger refactoring - let's discuss.
On the test side, maybe add another test on the combination of guidance and iid settings, given that this implemented as an option?
Documentation: great to have a tutorial as part of advanced tutorial 20 already. As a follow-up, I suggest a refactoring of tutorials 19 and 20, and 1-2 how-to-guide for the VF and guidance methods.
There was a problem hiding this comment.
Great to have this tutorial. Overall, the VF and NPSE variants tutorials 19 and 20 are very long now and with overlap. I am already working on a refactoring and will take the guidance part into account as well.
| guidance_method: Method to guide the diffusion process. If None, no guidance | ||
| is used. currently we support `affine_classifier_free`, which allows to | ||
| scale and shift the "likelihood" or "prior" score contribution. This can | ||
| be used to perform "super" conditioning i.e. shring the variance of the |
| scale and shift the "likelihood" or "prior" score contribution. This can | ||
| be used to perform "super" conditioning i.e. shring the variance of the | ||
| likelihood. `Universal` can be used to guide the diffusion process with | ||
| a general guidance function. `Interval` is an isntance of that where |
| guidance_method: Optional[str] = None, | ||
| guidance_params: Optional[Dict] = None, |
There was a problem hiding this comment.
the sample method now has >20 kwargs. we could think about introducing config classes instead, e.g., sth like
from sbi.inference.posteriors import GuidanceConfig, PriorGuideCfg
guidance = GuidanceConfig(
method="prior_guide",
params=PriorGuideCfg(train_prior=..., test_prior=..., K=5)
)
samples = posterior.sample((1000,), x=x_o, guidance=guidance)
but this would be a larger, possibly follow-up refactoring. what do you think?
There was a problem hiding this comment.
or even sth like
samples = posterior.with_guidance("prior_guide", ...).sample((1000,), x=x_o)There was a problem hiding this comment.
Mhh, I think config classes would be fine (I internally use config classes anyway). But at least at the time of implementation most user API was still configured via dicts so I made this the external API.
I do actually like your suggestions with with_guidance and with_iid to make it more explicit. But yes I would make this a follow-up refactoring.
| iid_params: Additional parameters passed to the iid method. See the specific | ||
| `IIDScoreFunction` child class for details. | ||
| guidance_method: Method to guide the diffusion process. If None, no guidance | ||
| is used. currently we support `affine_classifier_free`, which allows to |
| return denoising_posterior_precision | ||
|
|
||
|
|
||
| def compute_score(p: Distribution, inputs: Tensor): |
There was a problem hiding this comment.
add docstring and return type please.
| max_log_ratio: float = 50.0, | ||
| device: Union[str, torch.device] = "cpu", | ||
| ) -> Tuple[Tensor, Tensor, Tensor]: | ||
| """Implementation for fitting a generalized GMM to the prior ratio |
There was a problem hiding this comment.
Drop "Implementation for fitting", replace by "Fit" to have it in one line
| class _HashableById: | ||
| __slots__ = ("obj", "_id") | ||
|
|
||
| def __init__(self, obj: Distribution): | ||
| """Wraps a non-hashable Distribution to make it cache-key compatible.""" | ||
| self.obj = obj | ||
| self._id = id(obj) | ||
|
|
||
| def __hash__(self) -> int: | ||
| return self._id | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| return isinstance(other, _HashableById) and self._id == other._id |
There was a problem hiding this comment.
overall great to have this here! the main purpose is to not refit the GMM on every call from the potential, right?
just wondering, this caches by object ID not distribution parameters, so if someone passes the same dist with same params but a new instance, this won't have an effect. Accordingly, when someone changes the distribution params in place this will not be noticed in the cache.
But I think both are edge cases so it's fine
There was a problem hiding this comment.
Yeah, there are definitely some edge cases where this will fail. This is why it is only used if required.
I generally a bit unhappy that the adaptor/score wrapper objects are reinitialized in each call of potential (which does require to hash as much as possible). I think it would be good the restructure the VF potential a bit that such quantities can be computed once at the beginning i.e. via an initalize_aux which needs to be called once berfore sample.
| from sbi.utils import BoxUniform, MultipleIndependent | ||
|
|
||
|
|
||
| def build_some_priors(num_dim: int): |
There was a problem hiding this comment.
rename to build_test_priors and add short docstring?
| # Bug in Independent introduced???? | ||
| priors = [prior1, prior2, prior2_2, prior3, prior4, prior5] # Something broke |
There was a problem hiding this comment.
what is this about? is there a bug?
There was a problem hiding this comment.
Good catch will check, but it includes all the priors so these tests did atleast pass. (so might have to remove the comments)
This adds an API for post-hoc modifications of the trained score estimator, allowing modifications of the likelihood or prior, the support, or other additional constraints. This addresses issue #1446 .
To does: