Here is a simple code that runs well. Even if the function minimize wraps the scipy.optimize.minimize it does not complain about pickling
import numpy as np
from scipy import optimize
from multiprocessing import Pool
def square(x):
return np.sum(x**2+ 2*x)
def minimize(args):
f,x = args
res = optimize.minimize(f, x, method = 'L-BFGS-B')
return res.x
x = np.random.rand(8,10)
args = [(square,x[i]) for i in range(8)]
p = Pool(8)
p.map(minimize,args)
However, if try the following it fails with the pickling error
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
def run():
def square(x):
return np.sum(x**2+ 2*x)
def minimize(args):
f,x = args
res = optimize.minimize(f, x, method = 'L-BFGS-B')
return res.x
x = np.random.rand(8,10)
args = [(square,x[i]) for i in range(8)]
p = Pool(8)
p.map(minimize,args)
run()
I want to make a module to use scipy minimize in parallel with many population of initial guesses. However, as shown in the example, when I make it a module, it fails.