【问题标题】:How to properly paralleize a blackbox likelihood in emcee如何在 emcee 中正确并行化黑盒可能性
【发布时间】:2020-07-14 21:15:10
【问题描述】:

我目前正在使用 emcee 包获取 MCMC 示例,使用教程 https://emcee.readthedocs.io/en/stable/tutorials/parallel/。代码的串行版本可以工作,但速度很慢,所以我想使用并行化技术来加快处理速度。

我的代码目前如下(提供伪代码):

def loglike(theta):
    cosmo = blackbox_function_from_another_package(theta)
    
    model = calculate_model(cosmo)
    
    diff = data - model
    
    return -0.5 * (diff).T @ ivar @ diff

def mcmc_parallel(loglikelihood, init_pos, nsamples):

    nwalkers = init_pos.shape[0]
    ndim = init_pos.shape[1]
    
    with Pool() as pool:
        sampler = emcee.EnsembleSampler(
            nwalkers, ndim, loglikelihood, pool=pool)
        sampler.run_mcmc(p0, nsamples, progress=True);

#Main code

from multiprocessing import Pool
sampler = mcmc_parallel(loglikelihood=loglike, init_pos=p0, nsamples=5)

dataivar 是用于酸洗目的的全局变量,如教程中所述。每当我尝试运行此代码时,它都会无限期地执行,当我中断执行时,我会收到以下回调:

    294         try:    # restore state no matter what (e.g., KeyboardInterrupt)
    295             if timeout is None:
--> 296                 waiter.acquire()
    297                 gotit = True
    298             else:

我不完全确定发生了什么以及代码冻结的原因。如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

    标签: python parallel-processing python-multithreading mcmc emcee


    【解决方案1】:

    我遇到了同样的问题,发现这个解决方案对我有用:

    import multiprocessing as mp
    
    Pool = mp.get_context('fork').Pool
    

    【讨论】:

      猜你喜欢
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多