python - 在 python 中使用多处理运行多个 ML 模型
问题描述
我在 python 中使用 Multiprocessing 并行运行两个模型,代码如下:
def pro(process):
#print(process)
os.system('python {}'.format(process))
def run_model_multiprocessing(ml_model1,ml_model2):
processes = (ml_model1,ml_model2)
pool = Pool(processes=7)
start = datetime.datetime.now()
print('Start:',start)
pool.map(defs.pro, processes)
end = datetime.datetime.now()
print('End :',end)
total = end-start
print('Total :', total)
但是我的模型都返回一个输出文件,但我无法在上述多处理过程中获取该输出文件。我已经使用下面的代码从两个模型中获取返回输出,但它对我不起作用。
def run_model_multiprocessing(ml_model1,ml_model2):
processes = (ml_model1,ml_model2)
pool = Pool(processes=7)
start = datetime.datetime.now()
print('Start:',start)
outdf1,outdf2 = pool.map(defs.pro, processes)
end = datetime.datetime.now()
print('End :',end)
total = end-start
print('Total :', total)
return outdf1, outdf2
虽然程序运行成功,但是outdf1
and里面什么都没有outdf2
解决方案
如果我正确理解了您的查询,您的目标是并行运行两个机器学习模型 (ml_model1,ml_model2) 进行预测。
进程池仅提供数据并行性(通过并行处理数据的不同进程分发数据。)
如果您的目标是并行运行两个机器学习模型进行预测,那么您应该使用基于进程的并行性或基于线程的并行性。
请参考以下链接。
推荐阅读
- ios - 关于为什么我的谓词不断返回 nil 并使我的应用程序崩溃的任何想法
- arrays - 在 VBA Access 中循环遍历二维数组时出错
- r - 我在 R 中的 if else 语句中需要帮助,直到满足某个条件
- javascript - 按值对 JSON 对象进行排序的函数
- python-3.x - 使用 sklearn/Scikit-Learn 执行自定义 GLM
- r - 如何在 R Studio 中测试经过训练的 knn 模型的准确性?
- python - 将字符串列表的某些部分过滤到另一个列表中
- iis - 为什么我的本地网站无法在 IIS 中运行
- nlp - 对齐没有书面形式的语言的平行句子
- python - Python-turtle:来自数学库:dist() 导致循环退出/冻结