对于一些优化问题,我使用scipys优化工具箱中的差分进化。 我想使用几个CPU来加速这个过程,但我想把几个额外的参数传递给目标函数。然而,这些不仅仅是一些标量,而是优化评估模型所需的一些数据集
当我试图以通常的方式将参数直接传递给目标函数时,python抱怨目标函数不可拾取。当我将数据放入字典并将其传递给目标函数时,python抱怨 “文件“/usr/lib64/python3.6/multiprocessing/connection.py”,第393行,以字节为单位 header=struct.pack(“!i”,n) struct.error:“i”格式需要-2147483648<;=数字<;=2147483647 "
当使用多个worker时,如何将非平凡数据传递给差分进化的目标函数?我还没有找到办法
差不多
par02 = {'a':2,'b':3, "data":train_data}
# Define optimization bounds.
bounds = [(0, 10), (0, 10)]
# Attempt to optimize in series.
# series_result = differential_evolution(rosenbrock, bounds, args=(par02,))
# print(series_result.x)
# Attempt to optimize in parallel.
parallel_result = differential_evolution(rosenbrock, bounds, args=(par02,),
updating='deferred', workers=-1)
例如,它不起作用
有人有主意吗?还是每次调用目标函数时都必须从磁盘加载数据?我相信这会大大降低优化速度
如果提供了MWE,它总是有帮助的。对于要使用的并行处理,目标函数和参数需要是可拾取的
以下说明了问题:
基本上,您不能使用在main上下文(即CLI)中定义的类/函数,它们必须可由main导入
https://docs.python.org/3/library/multiprocessing.html#multiprocessing-programming
相关问题 更多 >
编程相关推荐