SciPy中的并行优化 [英] Parallel optimizations in SciPy
问题描述
我有一个简单的功能
def square(x, a=1):
return [x**2 + a, 2*x]
对于几个参数a
,我想通过x
将其最小化.从本质上讲,我目前有一些循环,可以执行以下操作:
I want to minimize it over x
, for several parameters a
. I currently have loops that, in spirit, do something like this:
In [89]: from scipy import optimize
In [90]: res = optimize.minimize(square, 25, method='BFGS', jac=True)
In [91]: [res.x, res.fun]
Out[91]: [array([ 0.]), 1.0]
In [92]: l = lambda x: square(x, 2)
In [93]: res = optimize.minimize(l, 25, method='BFGS', jac=True)
In [94]: [res.x, res.fun]
Out[94]: [array([ 0.]), 2.0]
现在,该函数已被矢量化
Now, the function is already vectorized
In [98]: square(array([2,3]))
Out[98]: [array([ 5, 10]), array([4, 6])]
In [99]: square(array([2,3]), array([2,3]))
Out[99]: [array([ 6, 12]), array([4, 6])]
这意味着并行运行所有优化而不是循环运行可能会快得多.使用SciPy可以轻松做到这一点吗?还是其他任何第三方工具?
Which means it would probably be much faster to run all the optimizations in parallel rather than looping. Is that something that's easily do-able with SciPy? Or any other 3rd party tool?
推荐答案
这是另一种尝试,它基于我的原始答案以及随后的讨论.
Here's another try, based on my original answer and the discussion that followed.
据我所知, scipy.optimize 模块是用于具有标量或矢量输入以及标量输出或成本"的函数.
As far as I know, the scipy.optimize module is for functions with scalar or vector inputs and a scalar output, or "cost".
由于您将每个方程式视为独立于其他方程式,所以我最好的主意是使用多处理模块并行进行工作.如果您要最小化的功能与问题中的功能一样简单,那么我认为这是不值得的.
Since you're treating each equation as independent of the others, my best idea is to use the multiprocessing module to do the work in parallel. If the functions you're minimizing are as simple as the ones in your question, I'd say it's not worth the effort.
如果功能更复杂,并且您希望将工作分成几个部分,请尝试以下操作:
If the functions are more complex, and you'd like to divide the work up, try something like:
import numpy as np
from scipy import optimize
from multiprocessing import Pool
def square(x, a=1):
return [np.sum(x**2 + a), 2*x]
def minimize(args):
f,x,a = args
res = optimize.minimize(f, x, method = 'BFGS', jac = True, args = [a])
return res.x
# your a values
a = np.arange(1,11)
# initial guess for all the x values
x = np.empty(len(a))
x[:] = 25
args = [(square,a[i],x[i]) for i in range(10)]
p = Pool(4)
print p.map(minimize,args)
这篇关于SciPy中的并行优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!