[Support for Python 3.10] MCMC example in documentation does not work: `AttributeError: __enter__`
See original GitHub issueI am following the MCMC example in the documentation: https://docs.pyro.ai/en/stable/mcmc.html#nuts
import pyro
import pyro.distributions as dist
from pyro.infer import MCMC, NUTS
import torch
true_coefs = torch.tensor([1., 2., 3.])
data = torch.randn(2000, 3)
dim = 3
labels = dist.Bernoulli(logits=(true_coefs * data).sum(-1)).sample()
def model(data):
coefs_mean = torch.zeros(dim)
coefs = pyro.sample('beta', dist.Normal(coefs_mean, torch.ones(3)))
y = pyro.sample('y', dist.Bernoulli(logits=(coefs * data).sum(-1)), obs=labels)
return y
nuts_kernel = NUTS(model, adapt_step_size=True)
mcmc = MCMC(nuts_kernel, num_samples=500, warmup_steps=300)
mcmc.run(data)
But it raises an error:
Traceback (most recent call last):
File "/home/ayaka/Projects/test/main.py", line 19, in <module>
mcmc.run(data)
File "/home/ayaka/venv/lib/python3.10/site-packages/pyro/poutine/messenger.py", line 11, in _context_wrap
with context:
AttributeError: __enter__
Pyro version: 1.8.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
AttributeError: __enter__ for mcmc.run() - Misc.
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 ......
Read more >The AttributeError: __enter__ Python Error Solved
A Python error object (or exception) in is a problem in the code that causes the program to not work as intended and...
Read more >Python Error: "AttributeError: __enter__" - Stack Overflow
This results in AttributeError: __enter__ import os with os.open('out.txt', os.CREAT) as f: f.write('hello world'). This does not
Read more >PyMC: Bayesian Stochastic Modelling in Python - PMC - NCBI
Purpose. PyMC is a python module that implements Bayesian statistical models and fitting algorithms, including Markov chain Monte Carlo.
Read more >Dive Into Python
ActivePython is freely downloadable, although it is not open source. ... sure to type python2.3 to get into the interactive shell. ... Example...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For me on windows pytorch now installs through conda in python 3.10 @fehiepsi!
I could not for the life of me figure out why after installing pytorch using the default conda install and then pyro from pip I was getting this error. I guess pyro does not check the python version when installing through pip.
Thanks! I tested it on Python 3.8 and it works now.