AttributeError: __enter__ for mcmc.run()

Hello all,

I am trying to run a regression model with the following code:

def model(X, obs=None):

# Prior for the bias/intercept
alpha = pyro.sample('alpha', dist.Normal(0., 1.))

# Priors for the regression coeffcients
beta  = pyro.sample('beta', dist.Normal(torch.zeros(X.shape[1]), 
                                        1.*torch.ones(X.shape[1]))) 
# Priors for the error term
epsilon = pyro.sample('epsilon', dist.HalfCauchy(5.))  

with pyro.plate('data'):
    y = pyro.sample('y', dist.Normal(alpha + X.matmul(beta), epsilon), obs=obs)
    
return y

X_train = torch.tensor(X).float()
y_train = torch.tensor(y).float()

nuts_kernel = NUTS(model)
mcmc = MCMC(nuts_kernel, num_samples=3000, warmup_steps=200, num_chains=2)
mcmc.run(model, X_train, y_train)

Both X_train and y_train are tensors. However, it returns the following error message. And a few month ago, the same code worked. :confused: Does anyone know how to resolve this? All tips are appreciated!

AttributeError Traceback (most recent call last)
Input In [12], in <cell line: 4>()
2 nuts_kernel = NUTS(model)
3 mcmc = MCMC(nuts_kernel, num_samples=3000, warmup_steps=200, num_chains=2)
----> 4 mcmc.run(model, X_train, y_train)

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyro/poutine/messenger.py:11, in _context_wrap(context, fn, *args, **kwargs)
10 def _context_wrap(context, fn, *args, **kwargs):
—> 11 with context:
12 return fn(*args, **kwargs)

AttributeError: enter

hello i think you’ll need to provide a complete runnable script that triggers the error if you would like help identifying the possible source of the problem,

1 Like

Hi!

I think I have figured it out - not the solution but the cause. I tried to run the code from a Jupyter notebook on a MacBook Pro with M1 chip, it returned the above message. And the same script works perfectly fine on an older MacBook Pro and a window machine I borrowed.

It might also be that this error is produced because of Python version 3.10 since Pyro is not being tested on Python 3.10 as far as I know.

2 Likes

Thanks! I think I can try to go back to Python 3.9 on my macbook M1 and test this theory. :slight_smile:

There is apparently an issue opened for this already on github: [Support for Python 3.10] MCMC example in documentation does not work: AttributeError: __enter__

1 Like