我查看了以下链接,但没有一个提供我正在寻找的解决方案
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 mand 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 stringOn 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]更多推荐
发布评论