脚本专栏 
首页 > 脚本专栏 > 浏览文章

keras tensorflow 实现在python下多进程运行

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

如下所示:

 
from multiprocessing import Process
import os
 
 
def training_function(...):
 import keras # 此处需要在子进程中
 ...
 
if __name__ == '__main__':
 p = Process(target=training_function, args=(...,))
 p.start()

原文地址:https://stackoverflow.com/questions/42504669/keras-tensorflow-and-multiprocessing-in-python

1、DO NOT LOAD KERAS TO YOUR MAIN ENVIRONMENT. If you want to load Keras / Theano / TensorFlow do it only in the function environment. E.g. don't do this:

import keras
 
def training_function(...):
 ...

but do the following:

def training_function(...):
 import keras
 ...

Run work connected with each model in a separate process: I'm usually creating workers which are making the job (like e.g. training, tuning, scoring) and I'm running them in separate processes. What is nice about it that whole memory used by this process is completely freedwhen your process is done. This helps you with loads of memory problems which you usually come across when you are using multiprocessing or even running multiple models in one process. So this looks e.g. like this:

def _training_worker(train_params):
 import keras
 model = obtain_model(train_params)
 model.fit(train_params)
 send_message_to_main_process(...)
 
def train_new_model(train_params):
 training_process = multiprocessing.Process(target=_training_worker, args = train_params)
 training_process.start()
 get_message_from_training_process(...)
 training_process.join()

Different approach is simply preparing different scripts for different model actions. But this may cause memory errors especially when your models are memory consuming. NOTE that due to this reason it's better to make your execution strictly sequential.

以上这篇keras tensorflow 实现在python下多进程运行就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:tensorflow 初始化未初始化的变量实例
下一篇:python中count函数简单的实例讲解
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网