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字符串替换的2种方法
Nov 30 Python
Python使用xlrd读取Excel格式文件的方法
Mar 10 Python
利用Python循环(包括while&for)各种打印九九乘法表的实例
Nov 06 Python
浅述python中深浅拷贝原理
Sep 18 Python
Python UnboundLocalError和NameError错误根源案例解析
Oct 31 Python
python字典的遍历3种方法详解
Aug 10 Python
在Python中使用filter去除列表中值为假及空字符串的例子
Nov 18 Python
Python yield生成器和return对比代码实例
Apr 20 Python
pycharm设置默认的UTF-8编码模式的方法详解
Jun 01 Python
python和node.js生成当前时间戳的示例
Sep 29 Python
python 基于selenium实现鼠标拖拽功能
Dec 24 Python
如何用python反转图片,视频
Apr 24 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 编程的 5个良好习惯
2009/02/20 PHP
PHP学习笔记之二 php入门知识
2011/01/12 PHP
简单的PHP多图上传小程序代码
2011/07/17 PHP
在PHP上显示JFreechart画的统计图方法
2013/11/03 PHP
php根据isbn书号查询amazon网站上的图书信息的示例
2014/02/13 PHP
PHP生成迅雷、快车、旋风等软件的下载链接代码实例
2014/05/12 PHP
php生成随机颜色方法汇总
2014/12/03 PHP
因str_replace导致的注入问题总结
2019/08/08 PHP
PHP如何将图片文件上传到另外一台服务器上
2019/08/26 PHP
PHP网站常见安全漏洞,及相应防范措施总结
2021/03/01 PHP
js获取div高度的代码
2008/08/09 Javascript
设置下载不需要倒计时cookie(倒计时代码)
2008/11/19 Javascript
js下通过getList函数实现分页效果的代码
2010/09/17 Javascript
框架页面高度自动刷新的Javascript脚本
2013/11/01 Javascript
JQuery基础语法小结
2015/02/27 Javascript
BootStrap Datetimepicker 汉化的实现代码
2017/02/10 Javascript
js中document.write和document.writeln的区别
2018/03/11 Javascript
node前端开发模板引擎Jade的入门
2018/05/11 Javascript
JavaScript实现捕获鼠标坐标
2020/04/12 Javascript
python字典排序实例详解
2015/05/20 Python
python动态参数用法实例分析
2015/05/25 Python
举例讲解Python中的迭代器、生成器与列表解析用法
2016/03/20 Python
python实现对求解最长回文子串的动态规划算法
2018/06/02 Python
用python写一个定时提醒程序的实现代码
2019/07/22 Python
详解Python time库的使用
2019/10/10 Python
Python爬虫防封ip的一些技巧
2020/08/06 Python
在vscode中启动conda虚拟环境的思路详解
2020/12/25 Python
突袭HTML5之Javascript API扩展1—Web Worker异步执行及相关概述
2013/01/31 HTML / CSS
Yahoo-PHP面试题2
2014/12/06 面试题
团员的自我评价
2013/12/01 职场文书
职业技术学校毕业生推荐信
2013/12/03 职场文书
十岁生日同学答谢词
2014/01/19 职场文书
售票员岗位职责
2015/02/15 职场文书
MySQL kill不掉线程的原因
2021/05/07 MySQL
mysql聚集索引、辅助索引、覆盖索引、联合索引的使用
2022/02/12 MySQL
Java由浅入深通关抽象类与接口(下篇)
2022/04/26 Java/Android