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语言中的按位运算符
Nov 26 Python
Python与R语言的简要对比
Nov 14 Python
python 实现在Excel末尾增加新行
May 02 Python
python实现贪吃蛇游戏
Mar 21 Python
Linux上使用Python统计每天的键盘输入次数
Apr 17 Python
Python使用sklearn库实现的各种分类算法简单应用小结
Jul 04 Python
Python 分发包中添加额外文件的方法
Aug 16 Python
python编写猜数字小游戏
Oct 06 Python
Python HTMLTestRunner库安装过程解析
May 25 Python
k-means 聚类算法与Python实现代码
Jun 01 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
Nov 24 Python
Python pandas读取CSV文件的注意事项(适合新手)
Jun 20 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 和 COM
2006/10/09 PHP
php explode函数实例代码
2012/02/27 PHP
joomla组件开发入门教程
2016/05/04 PHP
php微信支付接口开发程序
2016/08/02 PHP
PHP中使用mpdf 导出PDF文件的实现方法
2018/10/22 PHP
TP5框架实现的数据库备份功能示例
2020/04/05 PHP
ExtJS 2.0实用简明教程 之Border区域布局
2009/04/29 Javascript
微信小程序自定义组件
2017/08/16 Javascript
js时间戳与日期格式之间转换详解
2017/12/11 Javascript
Vue.js 2.0和Cordova开发webApp环境搭建方法
2018/02/26 Javascript
对Vue- 动态元素属性及v-bind和v-model的区别详解
2018/08/27 Javascript
vue实现路由监听和参数监听
2019/10/29 Javascript
Nuxt的动态路由和参数校验操作
2020/11/09 Javascript
[01:22]DOTA2神秘商店携大量周边降临完美大师赛
2017/11/07 DOTA
[36:05]DOTA2亚洲邀请赛 3.31 小组赛 A组 Liquid vs Optic
2018/04/01 DOTA
在Linux下调试Python代码的各种方法
2015/04/17 Python
python 读写txt文件 json文件的实现方法
2016/10/22 Python
Python传递参数的多种方式(小结)
2019/09/18 Python
Python文件操作模拟用户登陆代码实例
2020/06/09 Python
python爬虫使用requests发送post请求示例详解
2020/08/05 Python
python 将列表里的字典元素合并为一个字典实例
2020/09/01 Python
Python通过字典映射函数实现switch
2020/11/06 Python
CSS 3.0文字悬停跳动特效代码
2020/10/26 HTML / CSS
HTML5添加鼠标悬浮音响效果不使用FLASH
2014/04/23 HTML / CSS
canvas与html5实现视频截图功能示例
2016/12/15 HTML / CSS
Marc Jacobs官方网站:美国奢侈品牌
2017/08/29 全球购物
北大青鸟学生求职信
2013/09/24 职场文书
《庐山的云雾》教学反思
2014/04/22 职场文书
导游词怎么写
2015/02/04 职场文书
学校实习推荐信
2015/03/27 职场文书
Nginx解决前端访问资源跨域问题的方法详解
2021/03/31 Servers
Python实现文本文件拆分写入到多个文本文件的方法
2021/04/18 Python
Python爬虫框架之Scrapy中Spider的用法
2021/06/28 Python
MySQL学习之基础命令实操总结
2022/03/19 MySQL
GoFrame框架数据校验之校验结果Error接口对象
2022/06/21 Golang
使用CSS实现按钮边缘跑马灯动画
2023/05/07 HTML / CSS