简体   繁体   中英

AttributeError: __exit__ with pymc

I am using anaconda python on Ubuntu 12.04 When I try to execute following python code

import pymc as pm
import numpy as np

trace = None
with pm.Model() as model:
    alpha = pm.Normal('alpha', mu=0, sd=20)
    beta = pm.Normal('beta', mu=0, sd=20)
    sigma = pm.Uniform('sigma', lower=0, upper=20)

    y_est = alpha + beta * x

    likelihood = pm.Normal('y', mu=y_est, sd=sigma, observed=y)

    start = pm.find_MAP()
    step = pm.NUTS(state=start)
    trace = pm.sample(2000, step, start=start, progressbar=False)

    pm.traceplot(trace);

I get following error

AttributeError                            Traceback (most recent call last)
<ipython-input-7-0d27d14303ac> in <module>()
      3 
      4 trace = None
----> 5 with pm.Model() as model:
      6     alpha = pm.Normal('alpha', mu=0, sd=20)
      7     beta = pm.Normal('beta', mu=0, sd=20)

AttributeError: __exit__

How can I fix it ? What is the problem here?

pm.Model() is not a context manager; it doesn't implement the requisite methods. You probably are running a version of pymc where that functionality has not yet been added.

Just assign it to a variable and use directly, after creating the elements:

alpha = pm.Normal('alpha', mu=0, sd=20)
beta = pm.Normal('beta', mu=0, sd=20)
sigma = pm.Uniform('sigma', lower=0, upper=20)
# etc.
model = pm.Model([alpha, beta, sigma, ...])

The model fitting tutorial uses a function to produce the inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM