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连接mysql数据库示例(做增删改操作)
Dec 31 Python
scrapy-redis的安装部署步骤讲解
Feb 27 Python
python3.6根据m3u8下载mp4视频
Jun 17 Python
详解如何从TensorFlow的mnist数据集导出手写体数字图片
Aug 05 Python
python 数据提取及拆分的实现代码
Aug 26 Python
Django异步任务线程池实现原理
Dec 17 Python
Python批量启动多线程代码实例
Feb 18 Python
python tqdm 实现滚动条不上下滚动代码(保持一行内滚动)
Feb 19 Python
python使用梯度下降和牛顿法寻找Rosenbrock函数最小值实例
Apr 02 Python
Python爬虫之Selenium鼠标事件的实现
Dec 04 Python
python中threading和queue库实现多线程编程
Feb 06 Python
python tkinter实现定时关机
Apr 21 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
十大使用PHP框架的理由
2015/09/26 PHP
phpcms中的评论样式修改方法
2016/10/21 PHP
AutoSave/自动存储功能实现
2007/03/24 Javascript
使用IE6看老赵的博客 jQuery初探
2010/01/17 Javascript
js将long日期格式转换为标准日期格式实现思路
2013/04/07 Javascript
laytpl 精致巧妙的JavaScript模板引擎
2014/08/29 Javascript
通过JS动态创建一个html DOM元素并显示
2014/10/15 Javascript
localStorage的黑科技-js和css缓存机制
2017/02/06 Javascript
JavaScript箭头(arrow)函数详解
2017/06/04 Javascript
windows下更新npm和node的方法
2017/11/30 Javascript
vue组件挂载到全局方法的示例代码
2018/08/02 Javascript
Node.js模拟发起http请求从异步转同步的5种用法
2018/09/26 Javascript
移动端底部导航固定配合vue-router实现组件切换功能
2019/06/13 Javascript
WEB前端性能优化的7大手段详解
2020/02/04 Javascript
vue实现div可拖动位置也可改变盒子大小的原理
2020/09/16 Javascript
vue自定义树状结构图的实现方法
2020/10/18 Javascript
[56:20]LGD vs VP Supermajor 败者组决赛 BO3 第三场 6.10
2018/07/04 DOTA
Flask框架WTForm表单用法示例
2018/07/20 Python
python一键去抖音视频水印工具
2018/09/14 Python
python实现简单登陆系统
2018/10/18 Python
python2.7 安装pip的方法步骤(管用)
2019/05/05 Python
Python生成指定数量的优惠码实操内容
2019/06/18 Python
python调用接口的4种方式代码实例
2019/11/19 Python
利用Python的turtle库绘制玫瑰教程
2019/11/23 Python
python去除删除数据中\u0000\u0001等unicode字符串的代码
2020/03/06 Python
python实现scrapy爬虫每天定时抓取数据的示例代码
2021/01/27 Python
使用css3制作登录表单的步骤
2014/04/07 HTML / CSS
加拿大领先的时尚和体育零售商:Sporting Life
2019/12/15 全球购物
Brasty罗马尼亚:购买手表、香水、化妆品、珠宝
2020/04/21 全球购物
大学生实习鉴定评语
2014/04/25 职场文书
建筑专业毕业生求职信
2014/09/30 职场文书
现役军人家属慰问信
2015/03/24 职场文书
银行稽核岗位职责
2015/04/13 职场文书
Redis如何实现分布式锁
2021/08/23 Redis
彩虹社八名人气艺人全新周边限时推出,性转女装男装一次拥有!
2022/04/01 日漫
如何通过cmd 连接阿里云服务器
2022/04/18 Servers