-
Notifications
You must be signed in to change notification settings - Fork 129
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
custom prior #581
Comments
Hi there! It's a bit unclear to me what your prior is doing. This line seems to have a bug: return = a_0 * np.log(tau) - b_0 * tau - sp.gammaln(a_0) - tau # see the "=" after "return" Anyways: from sbi.inference import SNPE
inference = SNPE()
_ = inference.append_simulations(theta, x).train() # theta are samples from your custom prior
posterior = inference.build_posterior() If you are using SNLE or SNRE, you need to specify a prior. The prior must have a Does this answer you question? |
Apologies for the typo, and thanks for the reply.
I need to use SNLE. If I understood correctly, I could just write a class called prior, and define a method called log_prob in it. An instance of the class would then be passed to SBI, is this correct?
…________________________________
Da: Michael Deistler ***@***.***>
Inviato: Monday, January 17, 2022 11:05:07 PM
A: mackelab/sbi ***@***.***>
Cc: Spurio Mancini, Alessio ***@***.***>; Author ***@***.***>
Oggetto: Re: [mackelab/sbi] custom prior (Issue #581)
⚠ Caution: External sender
Hi there! It's a bit unclear to me what your prior is doing. This line seems to have a bug:
return = a_0 * np.log(tau) - b_0 * tau - sp.gammaln(a_0) - tau # see the "=" after "return"
Anyways:
If you are using SNPE, you can simply not pass any prior in the flexible interface<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.mackelab.org%2Fsbi%2Ftutorial%2F02_flexible_interface%2F&data=04%7C01%7Ca.spuriomancini%40ucl.ac.uk%7C97210d86be2c450f9dfa08d9da056d49%7C1faf88fea9984c5b93c9210a11d9a5c2%7C0%7C0%7C637780539114574555%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=fiCMVO5vc6dEL%2Bl77BlHeh1lLQBL4S8tFLQF%2FmG3N7M%3D&reserved=0>:
from sbi.inference import SNPE
inference = SNPE()
_ = inference.append_simulations(theta, x).train() # theta are samples from your custom prior
posterior = inference.build_posterior()
If you are using SNLE or SNRE, you need to specify a prior. The prior must have a .log_prob() method.
Does this answer you question?
—
Reply to this email directly, view it on GitHub<#581 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AD3IGQKUCA5L62KXFZKV7QTUWSHBHANCNFSM5MFTMZ6Q>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Hi @alessiospuriomancini and thanks for bringing this up! Yes, if you pass a class that mimics the behaviour of a torch distributions, then Essentially, the class needs
I think you are the first person we know about using this feature, that's great! Let us know if there are any problems. |
A comment from the side lines: if @alessiospuriomancini sees that this works, it might be an idea to contribute a unit test for exactly this feature to |
Thanks for bringing this up @psteinb but I think we even have a test for this: But it would be great to create an FAQ entry from this issue. |
Thanks everyone for your help. I can confirm that:
works smoothly with the SNLE method. |
Unfortunately I have to reopen this issue. In my last post I incorrectly said that writing the When running with SN L E, instead, with the same prior setup previously described, I get the following error: Traceback (most recent call last):
File "examples/sbi_examples.py", line 46, in <module>
samples = posterior.sample((1000,), x=observed_data).numpy()
File "/home/alessio/anaconda2/envs/sbi-env/lib/python3.8/site-packages/sbi/inference/posteriors/likelihood_based_posterior.py", line 187, in sample
transform = mcmc_transform(
File "/home/alessio/anaconda2/envs/sbi-env/lib/python3.8/site-packages/sbi/utils/sbiutils.py", line 918, in mcmc_transform
if hasattr(prior.support, "base_constraint") and hasattr(
File "/home/alessio/anaconda2/envs/sbi-env/lib/python3.8/site-packages/torch/distributions/distribution.py", line 107, in support
raise NotImplementedError
NotImplementedError The call to SBI in my script from sbi import utils as utils
from sbi.inference.base import infer
def simulator(parameters):
return experiment.simulator(parameters)
posterior = infer(simulator, prior, method='SNLE', num_simulations=1000)
samples = posterior.sample((1000,), x=observed_data).numpy()
log_probability = posterior.log_prob(samples, x=observed_data).numpy()
|
thanks for the good error documentation! Alright, this is probably due to the new support constraints since pytorch 1.5 or so. We will have to look into this in more detail adapt the prior wrapper class accordingly. Mostly likely we will have the time for that next week already, we will let you know. |
This is a bug in our code, I recently encountered it as well. It is fixed on the current main branch via this fix, but we have not made a release yet. |
A simple fix might be to add a class MyPrior:
....
....
def support():
pass (I did not test this though, no guarantees) |
First of all congratulations for this amazing software!
My issue: I have a custom prior that I would like to sample from
Is there a way to write this in a way that can be read by
sbi
, without using Torch distributions? ThanksThe text was updated successfully, but these errors were encountered: