如何解决使用 Scipy Optimize 进行多处理冻结
编辑:错误不在于多处理,而在于另一个库。我会结束这个问题,但@Lukasz Tracewski 使用 joblib 的观点可能会帮助其他人。
我尝试在 Windows 上并行化的任务遇到问题。
它似乎工作了一段时间,然后在特定实例上突然停止(我可以通过获取代码来打印它正在进行哪个迭代)。我在任务管理器中注意到,对于 Python 进程来说,cpu 使用率(例如大约 50%)是最小的。然后,当我在 cmd 提示符下执行键盘中断时,这会突然向前发射许多实例,并且活动会恢复一小会儿,然后又会再次下降。
我已经包含了我认为相关的代码部分。我知道它可以工作(我不认为它被困在问题上)并且它何时冻结似乎有一定程度的随机性。
我正在使用带有 max_iter
集的 COBYLA 求解器。我不确定它是否相关,但是当我尝试使用 BFGS 时,我遇到了冻结问题。
def get_optimal_angles(G,p,coeff,objective_function,initial_gamma_range,initial_beta_range,seed):
'''
This performs the classical-quantum interchange,improving the values of beta and gamma by reducing the value of
< beta,gamma | C | beta,gamma >. Returns the best angles found and the objective value this refers to. Bounds on the minimiser are set as the starting points
'''
initial_starting_points = random_intial_values((np.array(initial_gamma_range)),(np.array(initial_beta_range)),seed)
optimiser_function = minimize(objective_function,initial_starting_points,method='COBYLA',options={'maxiter':1500})
best_angles = optimiser_function.x
objective_value = optimiser_function.fun
return best_angles,objective_value
def find_best_angles_for_graph_6(graph_seed):
print("6:On graph",graph_seed)
#graph = gp.unweighted_erdos_graph(10,0.4,graph_seed)
graph = gp.unweighted_erdos_graph(6,graph_seed)
graph_coefficients = quantum_operator_z_coefficients(graph,'Yo')
exact_energy =get_exact_energy(graph)
angle_starting_seed = np.arange(1,number_of_angle_points,1)
objective_function= get_black_Box_objective_sv(graph,graph_coefficients)
list_of_results = []
for angle_seed in angle_starting_seed:
print("On Angle Seed",angle_seed)
best_angles_for_seed,best_objective_value_for_seed = get_optimal_angles(graph,graph_coefficients,[0,np.pi],angle_seed)
success_prob = calculate_energy_success_prob(graph,exact_energy,best_angles_for_seed,angle_seed)
angle_seed_data_list = [angle_seed,best_objective_value_for_seed,success_prob,best_angles_for_seed]
list_of_results.append(angle_seed_data_list)
list_of_best = get_best_results(list_of_results)
full_results = [graph_seed,list_of_best,list_of_results]
return full_results
import multiprocessing as mp
def main():
physical_cores = 5
pool = mp.Pool(physical_cores)
list_of_results_every_graph_6 = []
list_of_all_graph_results_6 = pool.map(find_best_angles_for_graph_6,range(1,number_of_graphs+1))
print(list_of_all_graph_results_6)
file_name_6 = 'unweighted_erdos_graph_N_6_p_8.pkl'
pickle_6 = open((file_name_6),'wb')
pickle.dump(list_of_all_graph_results_6,pickle_6)
pickle_6.close()
list_of_results_every_graph_10 = []
list_of_all_graph_results_10 = pool.map(find_best_angles_for_graph_10,number_of_graphs+1))
print(list_of_all_graph_results_10)
file_name_10 = 'unweighted_erdos_graph_N_9_p_8.pkl'
pickle_10 = open((file_name_10),'wb')
pickle.dump(list_of_all_graph_results_10,pickle_10)
pickle_10.close()
if __name__ == "__main__":
main()
编辑:这是 Jupyter 笔记本的完整代码。 https://www.dropbox.com/sh/6xb7setjsn1c1o3/AAANfH7mEmhuuf9cxh5QWsRQa?dl=0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。