多线程调用scipy.optimize.leastsq的目标函数 [英] Multithreaded calls to the objective function of scipy.optimize.leastsq
问题描述
我将scipy.optimize.leastsq
与模拟器结合使用. leastsq
调用用户定义的目标函数并将输入向量传递给它.反过来,目标函数返回误差向量. leastsq
以使误差矢量的平方和最小的方式优化输入矢量.
I'm using scipy.optimize.leastsq
in conjunction with a simulator. leastsq
calls a user-defined objective function and passes an input vector to it. In turn, the objective function returns an error vector. leastsq
optimizes the input vector in such a way that the sum of the squares of the error vector is minimized.
在我的情况下,目标函数每次调用都会运行整个模拟.所使用的模拟器是单线程的,每次运行需要几分钟.因此,我想一次运行模拟器的多个实例.但是,对目标函数的调用是串行执行的.
In my case the objective function will run a whole simulation each time it is called. The employed simulator is single-threaded and needs several minutes for each run. I'd therefore like to run multiple instances of the simulator at once. However, calls to the objective function are performed serially.
如何获取leastsq
一次执行对目标函数的多次调用?
How can I get leastsq
to perform multiple calls to the objective function at once?
推荐答案
如果您有多个参数,可以通过提供自己的函数来计算导数(Dfun
参数)来加快leastsq
的机会.如果未提供此功能,则leastsq
会遍历每个参数来每次计算导数,这很耗时.这似乎需要花费大部分时间.
There's a good opportunity to speed up leastsq
by supplying your own function to calculate the derivatives (the Dfun
parameter), providing you have several parameters. If this function is not supplied, leastsq
iterates over each of the parameters to calculate the derivative each time, which is time consuming. This appears to take the majority of the time in the fitting.
您可以使用自己的Dfun
函数,该函数使用multiprocessing.Pool
计算每个参数的导数以完成工作.这些导数可以独立计算,应该平凡并行化.
You can use your own Dfun
function which calculates the derivatives for each parameter using a multiprocessing.Pool
to do the work. These derivatives can be calculated independently and should be trivially parallelised.
这是一个粗略的示例,展示了如何执行此操作:
Here is a rough example, showing how to do this:
import numpy as np
import multiprocessing
import scipy.optimize
def calcmod(params):
"""Return the model."""
return func(params)
def delta(params):
"""Difference between model and data."""
return calcmod(params) - y
pool = multiprocessing.Pool(4)
def Dfun(params):
"""Calculate derivatives for each parameter using pool."""
zeropred = calcmod(params)
derivparams = []
delta = 1e-4
for i in range(len(params)):
copy = np.array(params)
copy[i] += delta
derivparams.append(copy)
results = pool.map(calcmod, derivparams)
derivs = [ (r - zeropred)/delta for r in results ]
return derivs
retn = scipy.optimize.leastsq(leastfuncall, inputparams, gtol=0.01,
Dfun=Dfun, col_deriv=1)
这篇关于多线程调用scipy.optimize.leastsq的目标函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!