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 实现堆排序算法代码
Jun 05 Python
详解Python编程中time模块的使用
Nov 20 Python
Python创建对称矩阵的方法示例【基于numpy模块】
Oct 12 Python
windows下Virtualenvwrapper安装教程
Dec 13 Python
pycharm使用matplotlib.pyplot不显示图形的解决方法
Oct 28 Python
Django之创建引擎索引报错及解决详解
Jul 17 Python
Django 多表关联 存储 使用方法详解 ManyToManyField save
Aug 09 Python
基于python中__add__函数的用法
Nov 25 Python
在Python中用GDAL实现矢量对栅格的切割实例
Mar 11 Python
Python3 Tensorlfow:增加或者减小矩阵维度的实现
May 22 Python
Python新手如何进行闭包时绑定变量操作
May 29 Python
教你怎么用Python操作MySql数据库
May 31 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
JS 网站性能优化笔记
2011/05/24 PHP
IE8 兼容性问题(属性名区分大小写)
2009/06/04 Javascript
20个非常有用的PHP类库 加速php开发
2010/01/15 Javascript
设置iframe的document.designMode后仅Firefox中其body.innerHTML为br
2012/02/27 Javascript
关于图片的预加载过程中隐藏未知的
2012/12/19 Javascript
JavaScript字符串常用类使用方法汇总
2015/04/14 Javascript
jfinal与bootstrap的登录跳转实战演习
2015/09/22 Javascript
javascript实现标签切换代码示例
2016/05/22 Javascript
浅谈js的html元素的父节点,子节点
2016/08/06 Javascript
D3.js封装文本实现自动换行和旋转平移等功能
2016/10/14 Javascript
Vue.js实战之Vuex的入门教程
2017/04/01 Javascript
Nodejs中使用phantom将html转为pdf或图片格式的方法
2017/09/18 NodeJs
Vue实现简单分页器
2018/12/29 Javascript
JS基于开关思想实现的数组去重功能【案例】
2019/02/18 Javascript
vite2.0+vue3移动端项目实战详解
2021/03/03 Vue.js
简单介绍Python中的readline()方法的使用
2015/05/24 Python
Python解析树及树的遍历
2016/02/03 Python
Python使用MD5加密算法对字符串进行加密操作示例
2018/03/30 Python
Python3 jupyter notebook 服务器搭建过程
2018/11/30 Python
对Python3 goto 语句的使用方法详解
2019/02/16 Python
python标记语句块使用方法总结
2019/08/05 Python
python实现梯度下降法
2020/03/24 Python
python中读入二维csv格式的表格方法详解(以元组/列表形式表示)
2020/04/24 Python
Pyinstaller 打包发布经验总结
2020/06/02 Python
python raise的基本使用
2020/09/10 Python
Django生成数据库及添加用户报错解决方案
2020/10/09 Python
小学生打架检讨书
2014/01/26 职场文书
网络技术专业求职信
2014/02/18 职场文书
建议书的格式
2014/05/12 职场文书
开工仪式策划方案
2014/05/23 职场文书
客房领班岗位职责
2015/02/11 职场文书
公积金贷款承诺书
2015/04/30 职场文书
运动会开幕式新闻稿
2015/07/17 职场文书
为什么说餐饮很难做,是因为你不了解这些新规则
2019/08/20 职场文书
oracle索引总结
2021/09/25 Oracle
零基础学java之带参数以及返回值的方法
2022/04/10 Java/Android