sbi: Custom Priors for SNPE

Hello! I have a simulation that takes in two arrays of parameters both of length N, of which I would like them to be sampled from different priors. I would then like to perform SNPE as provided in the sbi tutorials.

from sbi.utils.user_input_checks import process_prior
import torch
from torch.distributions import Uniform, TransformedDistribution, ExpTransform
from sbi.inference import SNPE

def simulation(params):
    omega, gamma = params
    simulation = StasisSolver(Gamma_0=gamma, Omega_0=omega)
    return simulation.get_stasis().astype(int)

base_dist = Uniform(torch.log(torch.tensor([1e-20])), torch.log(torch.tensor([1])))
gamma_prior = TransformedDistribution(base_dist, ExpTransform())
omega_prior = Uniform(torch.tensor([0.]), torch.tensor([1.]))

joint_prior = process_prior([omega_prior, gamma_prior])

inference = SNPE(joint_prior[0])
# proposal = joint_prior[0]
 for _ in range(rounds):
     theta = proposal.sample((num_sims,))
     x = simulator(theta)
#     _ = inference.append_simulations(theta, x, proposal=proposal).train()
#     posterior = inference.build_posterior().set_default_x(x_o)
#     proposal = posterior

The output of joint_prior is (MultipleIndependent(), 2, False) and the type is <class 'tuple'>. In my code, you can see that my two priors, omega_prior and gamma_prior are drawn from a Uniform and custom LogUniform distributions defined in PyTorch. I construct the joint prior using sbi.utils.user_input_checks.process_prior(). My question is how to properly unpack and sample from the prior distribution in my simulation function, given that the inputs are indeed drawn from 2 different prior distributions.

      6 def simulation(params):
----> 7     omega, gamma = params
      8     simulation = StasisSolver(Gamma_0=gamma, Omega_0=omega)
      9     return simulation.get_stasis().astype(int)

ValueError: too many values to unpack (expected 2)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Yes! thanks so much