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

 更新时间:2020年02月06日 09:39:14   作者:gukanqi8252  
今天小编就为大家分享一篇keras tensorflow 实现在python下多进程运行,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

 
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下多进程运行就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 一篇文章告诉你如何用Python控制Excel实现自动化办公

    一篇文章告诉你如何用Python控制Excel实现自动化办公

    这篇文章主要介绍了教你怎么用Python处理excel实现自动化办公,文中有非常详细的代码示例,对正在学习python的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-08-08
  • 用python写一个福字(附完整代码)

    用python写一个福字(附完整代码)

    大家好,本篇文章主要讲的是用python写一个福字(附完整代码),感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Python I/O与进程的详细讲解

    Python I/O与进程的详细讲解

    今天小编就为大家分享一篇关于Python I/O与进程的详细讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • python爬虫系列Selenium定向爬取虎扑篮球图片详解

    python爬虫系列Selenium定向爬取虎扑篮球图片详解

    这篇文章主要介绍了python爬虫系列Selenium定向爬取虎扑篮球图片详解,具有一定参考价值,喜欢的朋友可以了解下。
    2017-11-11
  • 最新Python idle下载、安装与使用教程图文详解

    最新Python idle下载、安装与使用教程图文详解

    这篇文章主要介绍了最新Python idle下载、安装与使用教程图文详解,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • python实现简单中文词频统计示例

    python实现简单中文词频统计示例

    本篇文章主要介绍了python实现简单中文词频统计示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • PyQt5 实现字体大小自适应分辨率的方法

    PyQt5 实现字体大小自适应分辨率的方法

    今天小编就为大家分享一篇PyQt5 实现字体大小自适应分辨率的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • 2020版Python学习路线图(附学习资料)

    2020版Python学习路线图(附学习资料)

    这篇文章主要介绍了Python学习路线图(2020最新版),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2020-09-09
  • Python OpenCV的基本使用及相关函数

    Python OpenCV的基本使用及相关函数

    这篇文章主要介绍了Python-OpenCV的基本使用和相关函数介绍,主要包括图像的读取保存图像展示问题,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • Django项目创建及管理实现流程详解

    Django项目创建及管理实现流程详解

    这篇文章主要介绍了Django项目创建及管理实现流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10

最新评论