I’ve implemented a model (shown below) where the control flow through the program depends on a draw from a Bernoulli random variable. For some reason, when I attempt to do inference using MCMC with a NUTS kernel I get the following runtime error.
RuntimeError: bool value of Tensor with more than one value is ambiguous
Trace Shapes:
Param Sites:
Sample Sites:
edge dist |
value 2 1 |
slope dist |
value |
n dist |
value 1000 |
Here is the code that generated the error.
import pyro
import pyro.distributions as dist
from pyro import sample
from pyro.infer.mcmc import MCMC, NUTS
def model(n):
theta_edge = 0.5
X_mean = 0
X_var = 1
Y_mean = 0
Y_var = 1
# edge == 1 means Y is a linear function of X with additive gaussian noise.
edge = sample('edge', dist.Bernoulli(theta_edge))
slope = sample('slope', dist.Normal(0,1))
with pyro.plate('n', n):
if edge:
X = sample('X', dist.Normal(X_mean, X_var))
Y = sample('Y', dist.Normal(Y_mean + X * slope, Y_var))
else:
Y = sample('Y', dist.Normal(Y_mean, Y_var))
X = sample('X', dist.Normal(X_mean + Y * slope, X_var))
return X, Y, edge
n = 1000
n_steps = 5000
X, Y, edge = model(n)
conditioned_model = pyro.condition(model, data={'X':X, 'Y':Y})
nuts_kernel = NUTS(conditioned_model, adapt_step_size=True)
posterior = MCMC(nuts_kernel, num_samples=n_steps, warmup_steps=200).run(n)
The inference works fine when I use ImportanceSampling. I suspect that this has something to do with the fact that MCMC is expressing edge
as a tensor, while importance sampling samples from the prior program without modifying their underlying data type. Is there a way to get NUTS (or any other MCMC method) to work with this kind of program?
I’m happy to additional detail from the error message if that would be helpful.
Thanks!