Python 在类内使用emcee库中的多处理
我曾尝试使用库在类内实现蒙特卡罗马尔可夫链,并使多处理模块工作,但在运行此类测试代码后:Python 在类内使用emcee库中的多处理,python,multiprocessing,pickle,emcee,Python,Multiprocessing,Pickle,Emcee,我曾尝试使用库在类内实现蒙特卡罗马尔可夫链,并使多处理模块工作,但在运行此类测试代码后: import numpy as np import emcee import scipy.optimize as op # Choose the "true" parameters. m_true = -0.9594 b_true = 4.294 f_true = 0.534 # Generate some synthetic data from the model. N = 50 x = np.sort
import numpy as np
import emcee
import scipy.optimize as op
# Choose the "true" parameters.
m_true = -0.9594
b_true = 4.294
f_true = 0.534
# Generate some synthetic data from the model.
N = 50
x = np.sort(10*np.random.rand(N))
yerr = 0.1+0.5*np.random.rand(N)
y = m_true*x+b_true
y += np.abs(f_true*y) * np.random.randn(N)
y += yerr * np.random.randn(N)
class modelfit():
def __init__(self):
self.x=x
self.y=y
self.yerr=yerr
self.m=-0.6
self.b=2.0
self.f=0.9
def get_results(self):
def func(a):
model=a[0]*self.x+a[1]
inv_sigma2 = 1.0/(self.yerr**2 + model**2*np.exp(2*a[2]))
return 0.5*(np.sum((self.y-model)**2*inv_sigma2 + np.log(inv_sigma2)))
result = op.minimize(func, [self.m, self.b, np.log(self.f)],options={'gtol': 1e-6, 'disp': True})
m_ml, b_ml, lnf_ml = result["x"]
return result["x"]
def lnprior(self,theta):
m, b, lnf = theta
if -5.0 < m < 0.5 and 0.0 < b < 10.0 and -10.0 < lnf < 1.0:
return 0.0
return -np.inf
def lnprob(self,theta):
lp = self.lnprior(theta)
likelihood=self.lnlike(theta)
if not np.isfinite(lp):
return -np.inf
return lp + likelihood
def lnlike(self,theta):
m, b, lnf = theta
model = m * self.x + b
inv_sigma2 = 1.0/(self.yerr**2 + model**2*np.exp(2*lnf))
return -0.5*(np.sum((self.y-model)**2*inv_sigma2 - np.log(inv_sigma2)))
def run_mcmc(self,nstep):
ndim, nwalkers = 3, 100
pos = [self.get_results() + 1e-4*np.random.randn(ndim) for i in range(nwalkers)]
self.sampler = emcee.EnsembleSampler(nwalkers, ndim, self.lnprob,threads=10)
self.sampler.run_mcmc(pos, nstep)
test=modelfit()
test.x=x
test.y=y
test.yerr=yerr
test.get_results()
test.run_mcmc(5000)
将numpy导入为np
进口主持
导入scipy.optimize作为op
#选择“true”参数。
m_真=-0.9594
b_真=4.294
f_真=0.534
#从模型生成一些合成数据。
N=50
x=np.sort(10*np.rand.rand(N))
yerr=0.1+0.5*np.random.rand(N)
y=m_真*x+b_真
y+=np.abs(f_真*y)*np.random.randn(N)
y+=yerr*np.random.randn(N)
类modelfit():
定义初始化(自):
self.x=x
self.y=y
self.yerr=yerr
self.m=-0.6
self.b=2.0
自相关系数f=0.9
def get_结果(自我):
def func(a):
模型=a[0]*self.x+a[1]
inv_sigma2=1.0/(self.yerr**2+模型**2*np.exp(2*a[2]))
返回0.5*(np.sum((self.y-model)**2*inv\u sigma2+np.log(inv\u sigma2)))
结果=op.minimize(func[self.m,self.b,np.log(self.f)],选项={'gtol':1e-6,'disp':True})
m_ml,b_ml,lnf_ml=结果[“x”]
返回结果[“x”]
def lnprior(自身,θ):
m、 b,lnf=θ
如果-5.0
我收到了以下错误消息:
File "MCMC_model.py", line 157, in run_mcmc
self.sampler.run_mcmc(theta0, nstep)
File "build/bdist.linux-x86_64/egg/emcee/sampler.py", line 157, in run_mcmc
File "build/bdist.linux-x86_64/egg/emcee/ensemble.py", line 198, in sample
File "build/bdist.linux-x86_64/egg/emcee/ensemble.py", line 382, in _get_lnprob
File "build/bdist.linux-x86_64/egg/emcee/interruptible_pool.py", line 94, in map
File "/vol/aibn84/data2/zahra/anaconda/lib/python2.7/multiprocessing/pool.py", line 558, in get
raise self._value
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
文件“MCMC_model.py”,第157行,在run_MCMC中
自动取样器运行mcmc(θ0,nstep)
文件“build/bdist.linux-x86_64/egg/emcee/sampler.py”,第157行,在run_mcmc中
示例中的第198行文件“build/bdist.linux-x86_64/egg/emcee/emessemble.py”
文件“build/bdist.linux-x86_64/egg/emcee/essemble.py”,第382行,在
文件“build/bdist.linux-x86_64/egg/emcee/interruptable_pool.py”,第94行,在map中
get中的文件“/vol/aibn84/data2/zahra/anaconda/lib/python2.7/multiprocessing/pool.py”,第558行
提升自我价值
cPickle.PicklingError:无法pickle:属性查找\uuuu内置\uuuuu。instancemethod失败
我认为这与我如何在类中使用多处理有关,但我不知道如何保持类的结构,同时也使用多处理
谢谢你给我任何提示
另外,如果我从上一个函数中删除
threads=10
,那么代码就可以完美地工作。有很多问题可以讨论发生了什么:
…包括这一个,这似乎是你对几乎相同问题的回答:
然而,这里的不同之处在于,您没有直接使用
多处理
,而是emcee
。因此,pathos.multiprocessing
解决方案(来自上面的链接)不适用于您。由于emcee
使用cPickle
,您必须坚持使用pickle
知道如何序列化的东西。对于类实例,您运气不佳。典型的解决方法是使用copy\u reg
来注册要序列化的对象的类型,或者添加\uuuuuuu reduce\uuuu
方法来告诉python如何序列化它。你可以从上面的链接中看到一些答案,它们提出了类似的建议……但没有一个能让你保持课堂写作的方式。有一些问题讨论了正在发生的事情:
…包括这一个,这似乎是你对几乎相同问题的回答:
然而,这里的不同之处在于,您没有直接使用
多处理
,而是emcee
。因此,pathos.multiprocessing
解决方案(来自上面的链接)不适用于您。由于emcee
使用cPickle
,您必须坚持使用pickle
知道如何序列化的东西。对于类实例,您运气不佳。典型的解决方法是使用copy\u reg
来注册要序列化的对象的类型,或者添加\uuuuuuu reduce\uuuu
方法来告诉python如何序列化它。你可以从上面的链接中看到一些答案,它们提出了类似的建议……但没有一个能让你保持课堂写作的方式。有一些问题讨论了正在发生的事情:
…包括这一个,这似乎是你对几乎相同问题的回答:
然而,这里的不同之处在于,您没有直接使用多处理
,而是emcee
。因此,pathos.multiprocessing
解决方案(来自上面的链接)不适用于您。由于emcee
使用cPickle
,您必须坚持使用pickle
知道如何序列化的东西。对于类实例,您运气不佳。典型的解决方法是使用copy\u reg
注册对象y的类型