MCMC throwin an error when I try to run it

Hello,

I’m using the tutorial of using a self-written guide for a bayesian regression, (http://pyro.ai/examples/bayesian_regression_ii.html) adapted to my data. Essentially, it’s a simple

When I run nuts_kernel = NUTS(model_g) I get the following error:

Traceback (most recent call last):

  File "<ipython-input-20-f21e6b5ce4f8>", line 1, in <module>
    nuts_kernel = NUTS(model_g)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\pyro\infer\mcmc\nuts.py", line 148, in __init__
    init_strategy=init_strategy)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\pyro\infer\mcmc\hmc.py", line 112, in __init__
    raise ValueError("Only one of `model` or `potential_fn` must be specified.")

ValueError: Only one of `model` or `potential_fn` must be specified.

I thought i may have a model on CPU and GPU and that may be throwing the error so i restarted my kernel and got the same thing. Is this error indicative of some other error? Model code below:

def model(x, y):
    a = pyro.sample("mean", dist.Normal(0., 10.))
    b = pyro.sample("edit_coeff", dist.Normal(0., 1.))
    sigma = pyro.sample("sigma", dist.Uniform(0., 10.))
    mean = a + b * x
    rate = mean.exp()
    with pyro.plate("data", len(x)):
        pyro.sample("obs", dist.Poisson(rate, sigma), obs=y)    
        
def guide(x, y):
    a_loc = pyro.param('a_mean', torch.tensor(0.))
    a_scale = pyro.param('a_std', torch.tensor(1.),
                         constraint=constraints.positive)
    sigma_loc = pyro.param('sigma_loc', torch.tensor(1.),
                             constraint=constraints.positive)
    weights_loc = pyro.param('weights_loc', torch.randn(3))
    weights_scale = pyro.param('weights_scale', torch.ones(3),
                               constraint=constraints.positive)
    a = pyro.sample("a", dist.Normal(a_loc, a_scale))
    b = pyro.sample("edit_coeff", dist.Normal(weights_loc[0], weights_scale[0]))
    sigma = pyro.sample("sigma", dist.Normal(sigma_loc, torch.tensor(0.05)))
    mean =  a + b * x     
    
def summary(samples):
    site_stats = {}
    for site_name, values in samples.items():
        marginal_site = pd.DataFrame(values)
        describe = marginal_site.describe(percentiles=[.05, 0.25, 0.5, 0.75, 0.95]).transpose()
        site_stats[site_name] = describe[["mean", "std", "5%", "25%", "50%", "75%", "95%"]]
    return site_stats

model_g = model(x.cuda(), y.cuda())

nuts_kernel = NUTS(model_g) #model throws error here

mcmc = MCMC(nuts_kernel, num_samples = 1500, warmup_steps = 500)
mcmc.run(x.cuda(), y.cuda())

hmc_samples = {k: v.detach().cpu().numpy() for k, v in mcmc.get_samples().items()}

Hi @jordan.howell2, could you check if model_g is None? NUTS requires either model or potential_fn specified, if you provide None input, it will throw that error. I think what you need is nuts_kernel = NUTS(model).

Got it! thank you.