question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Support for Python 3.10] MCMC example in documentation does not work: `AttributeError: __enter__`

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nabriiscommented, May 24, 2022

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.

1reaction
ayaka14732commented, Mar 5, 2022

Thanks! I tested it on Python 3.8 and it works now.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found