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自定义主从分布式架构实例分析
Sep 19 Python
python里使用正则的findall函数的实例详解
Oct 19 Python
Python自定义线程池实现方法分析
Feb 07 Python
理论讲解python多进程并发编程
Feb 09 Python
关于Python正则表达式 findall函数问题详解
Mar 22 Python
Python实现抓取HTML网页并以PDF文件形式保存的方法
May 08 Python
pycharm运行和调试不显示结果的解决方法
Nov 30 Python
Python子类继承父类构造函数详解
Feb 19 Python
Python中py文件转换成exe可执行文件的方法
Jun 14 Python
python中使用while循环的实例
Aug 05 Python
利用python绘制数据曲线图的实现
Apr 09 Python
django创建超级用户时指定添加其它字段方式
May 14 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 flv视频时间获取函数
2010/06/29 PHP
兼容各大浏览器带关闭按钮的漂浮多组图片广告代码
2014/06/05 PHP
PHP提示Cannot modify header information - headers already sent by解决方法
2014/09/22 PHP
PHP多进程编程总结(推荐)
2016/07/18 PHP
在js(jquery)中获得文本框焦点和失去焦点的方法
2012/12/04 Javascript
利用JavaScript检测CPU使用率自己写的
2014/03/22 Javascript
Bootstrap每天必学之进度条
2015/11/30 Javascript
JavaScript函数中关于valueOf和toString的理解
2016/06/14 Javascript
浅谈js中几种实用的跨域方法原理详解
2016/12/02 Javascript
jQuery.cookie.js实现记录最近浏览过的商品功能示例
2017/01/23 Javascript
ES6入门教程之Iterator与for...of循环详解
2017/05/17 Javascript
vue实现商城上货组件简易版
2017/11/27 Javascript
[02:11]2016国际邀请赛中国区预选赛最美TA采访现场玩家
2016/06/28 DOTA
Python3中多线程编程的队列运作示例
2015/04/16 Python
Python3通过Luhn算法快速验证信用卡卡号的方法
2015/05/14 Python
浅谈python 里面的单下划线与双下划线的区别
2017/12/01 Python
TensorFlow变量管理详解
2018/03/10 Python
numpy concatenate数组拼接方法示例介绍
2019/05/27 Python
Scrapy框架基本命令与settings.py设置
2020/02/06 Python
Python爬虫之Selenium鼠标事件的实现
2020/12/04 Python
HTML5上传文件显示进度的实现代码
2012/08/30 HTML / CSS
世界上最大的罕见唱片、CD和音乐纪念品网上商店:991.com
2018/05/03 全球购物
护理职业应聘自荐书
2013/09/29 职场文书
知识竞赛活动方案
2014/02/18 职场文书
运动会广播稿150字
2014/02/19 职场文书
《翻越远方的大山》教学反思
2014/04/13 职场文书
2014组织生活会方案
2014/05/19 职场文书
计算机软件专业求职信
2014/06/10 职场文书
2014年营销工作总结
2014/11/22 职场文书
优秀班主任申报材料
2014/12/16 职场文书
酒会开场白大全
2015/06/01 职场文书
会议营销主持词
2015/07/03 职场文书
创业项目(超低成本创业项目)
2019/08/16 职场文书
MySQL之DML语言
2021/04/05 MySQL
「约定的梦幻岛」作画发布诺曼生日新绘
2022/03/21 日漫
苹果的回收机器人可以通过拆解iPhone获取大量的金和铜并外公布了环境保护最新进展
2022/04/21 数码科技