gpt4 book ai didi

python - 如何修复 'TypeError: hasattr(): attribute name must be string' 错误?

转载 作者:行者123 更新时间:2023-11-28 18:37:01 30 4
gpt4 key购买 nike

我有以下代码:

import pymc as pm
from matplotlib import pyplot as plt
from pymc.Matplot import plot as mcplot
import numpy as np
from matplotlib import rc

res = [18.752, 12.450, 11.832]

v = pm.Uniform('v', 0, 20)

errors = pm.Uniform('errors', 0, 100, size = 3)

taus = 1/(errors ** 2)

mydist = pm.Normal('mydist', mu = v, tau = taus, value = res, observed = True)

model=pm.Model([mydist, errors, taus, v, res])
mcmc=pm.MCMC(model) # This is line 19 where the TypeError originates
mcmc.sample(20000,10000)

mcplot(mcmc.trace('mydist'))

由于某种原因它不起作用,我收到“TypeError: hasattr(): attribute name must be string”错误,跟踪如下:

 Traceback (most recent call last):

File "<ipython-input-49-759ebaf4321c>", line 1, in <module>
runfile('C:/Users/Paul/.spyder2-py3/temp.py', wdir='C:/Users/Paul/.spyder2-py3')

File "C:\Users\Paul\Miniconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)

File "C:\Users\Paul\Miniconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "C:/Users/Paul/.spyder2-py3/temp.py", line 19, in <module>
mcmc=pm.MCMC(model)

File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\MCMC.py", line 82, in __init__
**kwds)

File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Model.py", line 197, in __init__
Model.__init__(self, input, name, verbose)

File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Model.py", line 99, in __init__
ObjectContainer.__init__(self, input)

File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Container.py", line 606, in __init__
conservative_update(self, input_to_file)

File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Container.py", line 549, in conservative_update
if not hasattr(obj, k):

TypeError: hasattr(): attribute name must be string

如何让它工作并输出“mydist”?

编辑:我一开始不小心发布了错误的踪迹。

Edit2:这一定是因为 res 没有名称,因为它是一个数组,但我不知道如何为它分配一个名称,所以它会让这个工作。

最佳答案

我必须承认我对 pymc 不熟悉,但将其更改为以下内容至少可以使应用程序运行:

mydist = pm.Normal('mydist', mu = v, tau = taus, value = res, observed = False)

mcmc=pm.MCMC([mydist, errors, taus, v, res])

这似乎是因为您将所有内容包装在 Model 中,它是 ObjectContainer 的扩展, 但既然你给它传递了一个列表,MCMC file_itemsContainer.py 中尝试分配索引 4在列表中使用 replace , 但自 Model是一个 ObjectContainer它分配了 key 4在里面 __dict__造成奇怪的TypeError你得到了。拆掉 wrapper Model造成MCMC正确使用 ListContainer相反。

现在,line 543 上的 Model.py 中可能存在错误可观察到的随机指标未存储在数据库中 - 表达式为 for object in self.stochastics | self.deterministics:但我怀疑它应该包括 self.observable_stochastics也是 - 所以我需要更改 observableFalse或者最后一行会抛出 KeyError .

我对 pymc 不够熟悉,无法确定它是实际行为、错误行为还是所需行为,所以我留给您提交 issue关于它。

关于python - 如何修复 'TypeError: hasattr(): attribute name must be string' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398767/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com