Closed almoyer closed 2 weeks ago
Hi there, for SNRE you have to use ratio_estimator_based_potential
, not likelihood_estimator_based_potential
.
Changing your code to this would work:
import torch
from sbi.inference import SNRE_A, MCMCPosterior, ratio_estimator_based_potential
num_dim = 2
prior = torch.distributions.MultivariateNormal(torch.zeros(num_dim), torch.eye(num_dim))
theta = prior.sample((1000,))
x = theta + torch.randn((1000, num_dim))
x_o = torch.randn((1, num_dim))
inference = SNRE_A(prior=prior)
lkl_estimator = inference.append_simulations(theta, x).train()
potential_fn, parameter_transform = ratio_estimator_based_potential(
lkl_estimator, prior, x_o
)
posterior = MCMCPosterior(
potential_fn, proposal=prior, theta_transform=parameter_transform, warmup_steps=10
)
posterior.sample((1000,), x=x_o)
or using build_posterior
directly instead of creating the potential and transform manually:
posterior = inference.build_posterior(mcmc_parameters=dict(warmup_steps=10))
Feel free to re-open the issue if there is anything unclear.
Hi @janfb and @michaeldeistler ,
Thanks a lot for your answer, I tried running my code using the correct function and it worked! Thanks for those quick answer and the example you gave me.
Describe the bug Using SNRE_A and the sampler MCMCPosterior for a simple model I cannot sample the posterior as this error occurs :
AttributeError: 'Sequential' object has no attribute 'log_prob'
To Reproduce
`import torch from sbi.inference import SNRE_A import sbi.inference from sbi.inference import likelihood_estimator_based_potential, MCMCPosterior
num_dim = 2 prior = torch.distributions.MultivariateNormal(torch.zeros(num_dim), torch.eye(num_dim)) theta = prior.sample((1000,)) x = theta + torch.randn((1000, num_dim)) x_o = torch.randn((1, num_dim)) inference = SNRE_A(prior=prior) lkl_estimator= inference.append_simulations(theta, x).train()
potential_fn, parameter_transform = likelihood_estimator_based_potential( lkl_estimator, prior, x_o ) posterior = MCMCPosterior( potential_fn, proposal=prior, theta_transform=parameter_transform, warmup_steps=10 )
posterior.sample((1000,),x=x_o) ` Error message:
`AttributeError Traceback (most recent call last)