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 文件重命名工具代码
Jul 26 Python
Python中django学习心得
Dec 06 Python
python面向对象实现名片管理系统文件版
Apr 26 Python
python可视化爬虫界面之天气查询
Jul 03 Python
python爬虫豆瓣网的模拟登录实现
Aug 21 Python
30秒学会30个超实用Python代码片段【收藏版】
Oct 15 Python
Win下PyInstaller 安装和使用教程
Dec 25 Python
python sorted函数原理解析及练习
Feb 10 Python
django 解决model中类写不到数据库中,数据库无此字段的问题
May 20 Python
python开发入门——set的使用
Sep 03 Python
Python中Selenium模块的使用详解
Oct 09 Python
PO模式在selenium自动化测试框架的优势
Mar 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
一个简单的域名注册情况查询程序
2006/10/09 PHP
用PHP连接MySQL代码的参数说明
2008/06/07 PHP
php中的观察者模式简单实例
2015/01/20 PHP
laravel中短信发送验证码的实现方法
2018/04/25 PHP
PHP针对redis常用操作实例详解
2019/08/17 PHP
关于js数组去重的问题小结
2014/01/24 Javascript
jquery实现仿Flash的横向滑动菜单效果代码
2015/09/17 Javascript
js判断是否为空和typeof的用法(详解)
2016/10/07 Javascript
js html css实现复选框全选与反选
2016/10/09 Javascript
利用PM2部署node.js项目的方法教程
2017/05/10 Javascript
JS实现闭包中的沙箱模式示例
2017/09/07 Javascript
js实现二级菜单点击显示当前内容效果
2018/04/28 Javascript
Django+Vue跨域环境配置详解
2018/07/06 Javascript
深入理解 Koa 框架中间件原理
2018/10/18 Javascript
Python处理XML格式数据的方法详解
2017/03/21 Python
Python把csv数据写入list和字典类型的变量脚本方法
2018/06/15 Python
CentOS 7 安装python3.7.1的方法及注意事项
2018/11/01 Python
详解Python中is和==的区别
2019/03/21 Python
Python日志无延迟实时写入的示例
2019/07/11 Python
opencv python Canny边缘提取实现过程解析
2020/02/03 Python
Python实现ATM系统
2020/02/17 Python
哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程
2020/05/07 Python
Django DRF认证组件流程实现原理详解
2020/08/17 Python
python代码实现图书管理系统
2020/11/30 Python
eBay奥地利站:eBay.at
2019/07/24 全球购物
IGK Hair官网:喷雾、洗发水、护发素等
2020/11/03 全球购物
Linux开机引导的步骤是什么
2015/10/19 面试题
同居协议书范本
2014/04/23 职场文书
体育课外活动总结
2014/07/08 职场文书
设备收款委托书范本
2014/10/02 职场文书
2015年营销工作总结范文
2015/04/23 职场文书
left join、inner join、right join的区别
2021/04/05 MySQL
python 爬取华为应用市场评论
2021/05/29 Python
springboot使用Redis作缓存使用入门教程
2021/07/25 Redis
【海涛教你打DOTA】剑圣第一人称视角解说
2022/04/01 DOTA
MySQL 表锁定 LOCK和UNLOCK TABLES的 SQL语法
2022/04/18 MySQL