sbi icon indicating copy to clipboard operation
sbi copied to clipboard

Replace `prepare_for_sbi` with separate calls to for prior and simulator in all tutorials

Open jecampagne opened this issue 3 years ago • 8 comments

Sorry to reopen on a different issue

If have 3 independents priors

prior = process_prior([prior_oc, prior_s8, prior_w0] )

leading to (MultipleIndependent(), 3, False) , what about this error when I try to use the usual prepare_for_sbi

#adapt/check the prior & simulator for SBI
simulator, prior = prepare_for_sbi(simulator, prior)
AssertionError: Nesting of combined distributions is not possible.

how should I proceed? Thanks

jecampagne avatar Oct 21 '22 12:10 jecampagne

Hi, no need to run prepare_for_sbi after you have run process_prior. You can simply pass a dummy to prepare_for_sbi if you only want to prepare your simulator:

from sbi.utils import BoxUniform
dummy_prior = BoxUniform(torch.zeros(3), torch.ones(3))
simulator, dumm_prior = prepare_for_sbi(simulator, dummy_prior)

michaeldeistler avatar Oct 23 '22 19:10 michaeldeistler

I agree that this is not elegant and we should probably split prepare_for_sbi into separate functions for the prior and the simulator, so let's keep this issue open until this is implemented

michaeldeistler avatar Oct 23 '22 19:10 michaeldeistler

Hey! Regarding the API it should be possible to directly use prepare_for_sbi:

simulator, prior, = prepare_for_sbi(simulator, [prior1, prior2, ...])

Regarding the error: it seems one of your priors was already processed to be a MultipleIndependent. The priors passed as list should all the native torch.distributions, e.g., Uniform, MultivariateNormal etc.

Regarding splitting prepare_for_sbi: What Michael said :) prepare_for_sbi is just a wrapper that calls process_prior and process_simulator. process_simulator checks the return type and ensures that the simulator returns a batch dimension. process_prior returns a tuple (processed_prior, num_parametes, whether_the_prior_returns_numpy). Thus, when using it, make sure to pass on only the processed prior, not the entire tuple.

janfb avatar Oct 24 '22 07:10 janfb

Hi, is this solved?

janfb avatar Oct 28 '22 11:10 janfb

I would keep it open because I think we should really split prepare_for_sbi.

michaeldeistler avatar Oct 28 '22 11:10 michaeldeistler

as discussed, we will promote using process_prior and process_simulator separately instead of using prepare_for_sbi, e.g., by changing it in all the tutorials:

replace simulator, prior = prepare_for_sbi(custom_simulator, custom_prior) by

prior, num_parameters, prior_returns_numpy = process_prior(custom_prior)

simulator = process_simulator(simulator, prior, prior_returns_numpy)

janfb avatar Nov 09 '22 09:11 janfb

TODO for the hackathon:

As described by @janfb above, this issue is not concerned with the core codebase, but only with the tutorial notebooks.

  • [ ] Go through all notebooks in the tutorial folder and indentify the relevant ones
  • [ ] Replace simulator, prior = prepare_for_sbi(custom_simulator, custom_prior) by
prior, num_parameters, prior_returns_numpy = process_prior(custom_prior)

simulator = process_simulator(simulator, prior, prior_returns_numpy)

jsvetter avatar Mar 05 '24 12:03 jsvetter

Happy to take care of it.

zinaStef avatar Mar 14 '24 09:03 zinaStef