繁体   English   中英

在类内的emcee库中使用多处理

[英]Using multiprocessing in emcee library inside a class

我尝试使用emcee库在一个类中实现Monte Carlo Markov Chain,并且还使多处理模块工作,但是在运行了这样的测试代码之后:

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)

我收到此错误消息:

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

我认为这与我在类中使用多重处理的方式有关,但是我无法弄清楚如何保持类的结构,同时也使用多重处理?

任何提示,我将不胜感激。

附注:如果我从上一个函数中删除threads=10 ,则代码必须能完美工作。

有许多SO问题讨论正在发生的事情:

  1. https://stackoverflow.com/a/21345273/2379433

  2. https://stackoverflow.com/a/28887474/2379433

  3. https://stackoverflow.com/a/21345308/2379433

  4. https://stackoverflow.com/a/29129084/2379433

…包括这个,这似乎是您对几乎相同的问题的回答:

  1. https://stackoverflow.com/a/25388586/2379433

但是,这里的区别在于您不是直接使用multiprocessing ,而是emcee 因此, pathos.multiprocessing解决方案(来自上面的链接)不适用于您。 由于emcee使用cPickle ,因此您必须坚持pickle知道如何序列化的内容。 您对类实例不走运。 典型的解决方法是使用copy_reg来注册要序列化的对象的类型,或者添加__reduce__方法来告诉python如何对其进行序列化。 您可以从上面的链接中看到一些答案,它们暗示了类似的事情……但是,没有一个使您能够按照编写该类的方式进行操作。

作为记录,您现在可以创建一个pathos.multiprocessing池,并使用pool参数将其传递给emcee。 但是,请注意,除非您的计算时间特别耗时,否则多处理的开销实际上会降低速度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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