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通过matplotlib绘制动画简单实例
Dec 13 Python
python表格存取的方法
Mar 07 Python
Python判断字符串是否为字母或者数字(浮点数)的多种方法
Aug 03 Python
Python 实现某个功能每隔一段时间被执行一次的功能方法
Oct 14 Python
Python打开文件,将list、numpy数组内容写入txt文件中的方法
Oct 26 Python
Pycharm取消py脚本中SQL识别的方法
Nov 29 Python
python面试题小结附答案实例代码
Apr 11 Python
django框架基于模板 生成 excel(xls) 文件操作示例
Jun 19 Python
python+tkinter实现学生管理系统
Aug 20 Python
Python3查找列表中重复元素的个数的3种方法详解
Feb 13 Python
浅析Python __name__ 是什么
Jul 07 Python
python绘图模块之利用turtle画图
Feb 12 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
CI使用Tank Auth转移数据库导致密码用户错误的解决办法
2014/06/12 PHP
让codeigniter与swfupload整合的最佳解决方案
2014/06/12 PHP
php使用curl实现简单模拟提交表单功能
2017/05/15 PHP
php单元测试phpunit入门实例教程
2017/11/17 PHP
PHP开发中解决并发问题的几种实现方法分析
2017/11/13 PHP
javascript 特殊字符串
2009/02/25 Javascript
慎用 somefunction.prototype 分析
2009/06/02 Javascript
Javascript 日期处理之时区问题
2009/10/08 Javascript
JavaScript小技巧 2.5 则
2010/09/12 Javascript
Javascript基础 函数“重载” 详细介绍
2013/10/25 Javascript
简体中文转换繁体中文(实现代码)
2013/12/25 Javascript
js动态移动滚动条至底部示例代码
2014/04/24 Javascript
javascript与css3动画结合使用小结
2015/03/11 Javascript
jquery实现的点击翻书效果代码
2015/11/04 Javascript
Bootstrap学习笔记之进度条、媒体对象实例详解
2017/03/09 Javascript
AngularJS封装$http.post()实例详解
2017/05/06 Javascript
Angular2 之 路由与导航详细介绍
2017/05/26 Javascript
AngularJS路由Ui-router模块用法示例
2017/05/29 Javascript
Vue官网todoMVC示例代码
2018/01/29 Javascript
详解vue之自行实现派发与广播(dispatch与broadcast)
2021/01/19 Vue.js
python中 ? : 三元表达式的使用介绍
2013/10/09 Python
Python实现把数字转换成中文
2015/06/29 Python
将Emacs打造成强大的Python代码编辑工具
2015/11/20 Python
详解python上传文件和字符到PHP服务器
2017/11/24 Python
Python3.6基于正则实现的计算器示例【无优化简单注释版】
2018/06/14 Python
Python3调用百度AI识别图片中的文字功能示例【测试可用】
2019/03/13 Python
Python转换时间的图文方法
2019/07/01 Python
Python实现最常见加密方式详解
2019/07/13 Python
Django如何使用jwt获取用户信息
2020/04/21 Python
python属于解释语言吗
2020/06/11 Python
python实现图片,视频人脸识别(opencv版)
2020/11/18 Python
Python实现王者荣耀自动刷金币的完整步骤
2021/01/22 Python
Osklen官方在线商店:巴西服装品牌
2019/04/25 全球购物
空字符串(“”)和null的区别
2012/11/13 面试题
就业导师推荐信范文
2015/03/27 职场文书
李强优秀员工观后感
2015/06/16 职场文书