TypeError:hasattr():属性名必须是pymc中的字符串(TypeError: hasattr(): attribute name must be string in pymc)

编程入门 行业动态 更新时间:2024-10-25 16:28:49
TypeError:hasattr():属性名必须是pymc中的字符串(TypeError: hasattr(): attribute name must be string in pymc)

我查看了以下链接,但没有一个提供我正在寻找的解决方案

https://github.com/pymc-devs/pymc/issues/125

PyMC错误:hasattr():属性名必须是字符串

我必须编写一个函数,它给出了先验(和其他数据等东西)返回一个pymc模型。 例如

m = pym.Model([fittable_params.values(), rv]) return m

而在调用函数中,当我执行mcmc = pymc.MCMC(model)会出现很长的错误

Traceback (most recent call last): File "model_constructor.py", line 81, in <module> mcmc = pm.MCMC(model) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/MCMC.py", line 81, in __init__ **kwds) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Model.py", line 195, in __init__ Model.__init__(self, input, name, verbose) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Model.py", line 98, in __init__ ObjectContainer.__init__(self, input) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Container.py", line 605, in __init__ conservative_update(self, input_to_file) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Container.py", line 548, in conservative_update if not hasattr(obj, k): TypeError: hasattr(): attribute name must be string

另一方面,如果在函数(返回模型)中,如果我这样做

m = pm.MCMC([fittable_params.values(), rv])

它运行正常, 但该函数应返回一个模型,以便用户可以在代码的其他部分用模型做任何他想做的事。

I have looked at the following links but none of them provide the solution I am looking for

https://github.com/pymc-devs/pymc/issues/125

PyMC error : hasattr(): attribute name must be string

I have to write a function which given the priors (and other stuff like data etc) returns a pymc model. eg

m = pym.Model([fittable_params.values(), rv]) return m

and the in the calling function, when I do mcmc = pymc.MCMC(model) It gives a long error

Traceback (most recent call last): File "model_constructor.py", line 81, in <module> mcmc = pm.MCMC(model) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/MCMC.py", line 81, in __init__ **kwds) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Model.py", line 195, in __init__ Model.__init__(self, input, name, verbose) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Model.py", line 98, in __init__ ObjectContainer.__init__(self, input) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Container.py", line 605, in __init__ conservative_update(self, input_to_file) File "/usr/local/lib/python2.7/dist-packages/pymc-2.3.2-py2.7-linux-i686.egg/pymc/Container.py", line 548, in conservative_update if not hasattr(obj, k): TypeError: hasattr(): attribute name must be string

On the other hand , if in the function (which returns a model), if I do

m = pm.MCMC([fittable_params.values(), rv])

it is running fine, but the function should return a model so that the user can do whatever he wants with the model in other parts of code.

最满意答案

如果链接的解决方案不适合您,那么作为最后的手段,您可以从模型中删除非字符串属性,因为它们似乎无论如何都不会被使用。

for key in m.__dict__.keys(): if not isinstance(key, basestring): del m.__dict__[key]

If the linked solutions don't work for you then as a last resort you can just delete the non-string attributes from the model, since they don't seem to be used anyway.

for key in m.__dict__.keys(): if not isinstance(key, basestring): del m.__dict__[key]

更多推荐

本文发布于:2023-07-04 10:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   属性   TypeError   hasattr   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!