use*_*729 4 python-3.x differential-evolution
我正在尝试使用 scipy.optimize 中的different_evolution 找到函数的全局最小值。如 scipy 参考指南中所述,我应该在选项中设置:update='deferred',workers=number of cores
但是,当我运行代码时,它会冻结并且什么也不做。我该如何解决这个问题,或者有没有更好的方法来并行化全局优化器?
以下是我的代码:
scipy.optimize.differential_evolution(objective, bnds, args=(),
strategy='best1bin', maxiter=1e6,
popsize=15, tol=0.01, mutation=(0.5, 1),
recombination=0.7, seed=None,
callback=None, disp=False, polish=True,
init='latinhypercube', atol=0,
updating='deferred',workers=2)
Run Code Online (Sandbox Code Playgroud)
小智 6
我自己也遇到了同样的问题。scipy.optimize.differential_evolution
在版本中添加了对并行性的支持,1.2.0
而我的版本太旧了。在查找文档时,最上面的结果还引用了旧版本。更新的文档可以在https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html找到。
我使用 virtualenvironment 和pip进行包管理,要升级到最新版本的scipy我只需要运行pip install --upgrade scipy
. 如果使用anaconda,您可能需要执行例如conda install scipy=1.4.1
.
为了激活并行性,将workers
标志设置> 1
为特定数量的内核或workers=-1
使用所有可用内核。
一个警告:不要和我犯同样的错误,尝试在 Windows 上的 Python 脚本的顶层直接运行差异进化,因为它不会运行。这是由于如何multiprocessing.Pool
运作。具体来说,而不是以下内容:
import scipy.optimize
def minimize_me(x, *args):
... # Your code
return result
# DO NOT DO LIKE THIS
... # Prepare all the arguments
# This will give errors
result = scipy.optimize.differential_evolution(minimize_me, bounds=function_bounds, args=extraargs,
disp=True, polish=False, updating='deferred', workers=-1)
print(result)
Run Code Online (Sandbox Code Playgroud)
使用下面的代码:
import scipy.optimize
def minimize_me(x, *args):
... # Your code
return result
# DO LIKE THIS
if __name__ == "__main__":
... # Prepare all the arguments
result = scipy.optimize.differential_evolution(minimize_me, bounds=function_bounds, args=extraargs,
disp=True, polish=False, updating='deferred', workers=-1)
print(result)
Run Code Online (Sandbox Code Playgroud)
有关 Windows 上并行执行的更多信息,请参阅此帖子:使用多处理时在 Windows 中强制使用 if __name__=="__main__"
请注意,即使不在 Windows 上,使用if __name__ == "__main__":
.
归档时间: |
|
查看次数: |
2662 次 |
最近记录: |