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


Posted in Python onFebruary 06, 2020

如下所示:

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 相关文章推荐
复制粘贴功能的Python程序
Apr 04 Python
浅谈django开发者模式中的autoreload是如何实现的
Aug 18 Python
Python元组及文件核心对象类型详解
Feb 11 Python
对Python中for复合语句的使用示例讲解
Nov 01 Python
在python中利用KNN实现对iris进行分类的方法
Dec 11 Python
python实现字符串加密 生成唯一固定长度字符串
Mar 22 Python
对python中的float除法和整除法的实例详解
Jul 20 Python
pytorch 实现cross entropy损失函数计算方式
Jan 02 Python
使用Keras画神经网络准确性图教程
Jun 15 Python
Flask处理Web表单的实现方法
Jan 31 Python
Matplotlib绘制混淆矩阵的实现
May 27 Python
移除Selenium中window.navigator.webdriver值
Jun 10 Python
Scrapy框架实现的登录网站操作示例
Feb 06 #Python
Tensorflow 多线程设置方式
Feb 06 #Python
Scrapy框架基本命令与settings.py设置
Feb 06 #Python
python opencv圆、椭圆与任意多边形的绘制实例详解
Feb 06 #Python
Python输出指定字符串的方法
Feb 06 #Python
python实现简单飞行棋
Feb 06 #Python
python实现飞行棋游戏
Feb 05 #Python
You might like
安装APACHE
2007/01/15 PHP
PHP超级全局变量数组小结
2012/10/04 PHP
探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解
2013/06/20 PHP
javascript encodeURI和encodeURIComponent的比较
2010/04/03 Javascript
cookie 最近浏览记录(中文escape转码)具体实现
2013/06/08 Javascript
Javascript改变CSS样式(局部和全局)
2013/12/18 Javascript
Js实现滚动变色的文字效果
2014/06/16 Javascript
JS文字球状放大效果代码分享
2015/08/19 Javascript
详解js中构造流程图的核心技术JsPlumb(2)
2015/12/08 Javascript
javascript 数组去重复(在线去重工具)
2016/12/17 Javascript
vue-dialog的弹出层组件
2020/05/25 Javascript
JQuery实现文字无缝滚动效果示例代码(Marquee插件)
2017/03/07 Javascript
mac中利用NVM管理不同node版本的方法详解
2017/11/08 Javascript
简易Vue评论框架的实现(父组件的实现)
2018/01/08 Javascript
详解微信小程序文件下载--视频和图片
2019/04/24 Javascript
通过实例了解js函数中参数的传递
2019/06/15 Javascript
JavaScript 截取字符串代码实例
2019/09/05 Javascript
Vue中图片Src使用变量的方法
2019/10/30 Javascript
nuxt 自定义 auth 中间件实现令牌的持久化操作
2020/11/05 Javascript
jquery实现点击左右按钮切换图片
2021/01/27 jQuery
[01:09]DOTA2次级职业联赛 - 99战队宣传片
2014/12/01 DOTA
详细分析python3的reduce函数
2017/12/05 Python
对numpy中二进制格式的数据存储与读取方法详解
2018/11/01 Python
详解Python循环作用域与闭包
2019/03/21 Python
python 使用plt画图,去除图片四周的白边方法
2019/07/09 Python
python类中super() 的使用解析
2019/12/19 Python
3D动画《斗罗大陆》上线当日播放过亿
2021/03/16 国漫
Lookfantastic澳大利亚官网:英国知名美妆购物网站
2021/01/07 全球购物
建筑行业的大学生自我评价
2013/12/08 职场文书
行政办公员自我评价分享
2013/12/14 职场文书
荷叶圆圆教学反思
2014/02/01 职场文书
开业主持词
2014/03/21 职场文书
群众路线剖析材料
2014/09/30 职场文书
考试作弊检讨书怎么写?
2014/12/21 职场文书
北京大学中文系教授推荐的10本小说
2019/08/08 职场文书
Python控制台输出俄罗斯方块的方法实例
2021/04/17 Python