Tensorflow训练模型越来越慢的2种解决方案


Posted in Python onFebruary 07, 2020

1 解决方案

【方案一】

载入模型结构放在全局,即tensorflow会话外层。

'''载入模型结构:最关键的一步'''
saver = tf.train.Saver()
'''建立会话'''
with tf.Session() as sess:
 for i in range(STEPS):
 '''开始训练'''
 _, loss_1, acc, summary = sess.run([train_op_1, train_loss, train_acc, summary_op], feed_dict=feed_dict)
 '''保存模型'''
 saver.save(sess, save_path="./model/path", i)

【方案二】

在方案一的基础上,将模型结构放在图会话的外部。

'''预测值'''
train_logits= network_model.inference(inputs, keep_prob)
'''损失值'''
train_loss = network_model.losses(train_logits)
'''优化'''
train_op = network_model.train(train_loss, learning_rate)
'''准确率'''
train_acc = network_model.evaluation(train_logits, labels)
'''模型输入'''
feed_dict = {inputs: x_batch, labels: y_batch, keep_prob: 0.5}
'''载入模型结构'''
saver = tf.train.Saver()
'''建立会话'''
with tf.Session() as sess:
 for i in range(STEPS):
 '''开始训练'''
 _, loss_1, acc, summary = sess.run([train_op_1, train_loss, train_acc, summary_op], feed_dict=feed_dict)
 '''保存模型'''
 saver.save(sess, save_path="./model/path", i)

2 时间测试

通过不同方法测试训练程序,得到不同的训练时间,每执行一次训练都重新载入图结构,会使每一步的训练时间逐次增加,如果训练步数越大,后面训练速度越来越慢,最终可导致图爆炸,而终止训练。

【时间累加】

2019-05-15 10:55:29.009205: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
step: 0, time cost: 1.8800880908966064
step: 1, time cost: 1.592250108718872
step: 2, time cost: 1.553826093673706
step: 3, time cost: 1.5687050819396973
step: 4, time cost: 1.5777575969696045
step: 5, time cost: 1.5908267498016357
step: 6, time cost: 1.5989274978637695
step: 7, time cost: 1.6078357696533203
step: 8, time cost: 1.6087186336517334
step: 9, time cost: 1.6123006343841553
step: 10, time cost: 1.6320762634277344
step: 11, time cost: 1.6317598819732666
step: 12, time cost: 1.6570467948913574
step: 13, time cost: 1.6584930419921875
step: 14, time cost: 1.6765813827514648
step: 15, time cost: 1.6751370429992676
step: 16, time cost: 1.7304580211639404
step: 17, time cost: 1.7583982944488525

【时间均衡】

2019-05-15 13:03:49.394354: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 7048 MB memory) -> physical GPU (device: 1, name: Tesla P4, pci bus id: 0000:00:0d.0, compute capability: 6.1)
step: 0, time cost: 1.9781079292297363
loss1:6.78, loss2:5.47, loss3:5.27, loss4:7.31, loss5:5.44, loss6:6.87, loss7: 6.84
Total loss: 43.98, accuracy: 0.04, steps: 0, time cost: 1.9781079292297363
step: 1, time cost: 0.09688425064086914
step: 2, time cost: 0.09693264961242676
step: 3, time cost: 0.09671926498413086
step: 4, time cost: 0.09688210487365723
step: 5, time cost: 0.09646058082580566
step: 6, time cost: 0.09669041633605957
step: 7, time cost: 0.09666872024536133
step: 8, time cost: 0.09651994705200195
step: 9, time cost: 0.09705543518066406
step: 10, time cost: 0.09690332412719727

3 原因分析

(1) Tensorflow使用图结构构建系统,图结构中有节点(node)和边(operation),每次进行计算时会向图中添加边和节点进行计算或者读取已存在的图结构;

(2) 使用图结构也是一把双刃之剑,可以加快计算和提高设计效率,但是,程序设计不合理会导向负面,使训练越来约慢;

(3) 训练越来越慢是因为运行一次sess.run,向图中添加一次节点或者重新载入一次图结构,导致图中节点和边越来越多,计算参数也成倍增长;

(4) tf.train.Saver()就是载入图结构的类,因此设计训练程序时,若每执行一次跟新就使用该类载入图结构,自然会增加参数数量,必然导致训练变慢;

(5) 因此,将载入图结构的类放在全局,即只载入一次图结构,其他时间只训练图结构中的参数,可保持原有的训练速度;

4 总结

(1) 设计训练网络,只载入一次图结构即可;

(2) tf.train.Saver()就是载入图结构的类,将该类的实例化放在全局,即会话外部,解决训练越来越慢。

以上这篇Tensorflow训练模型越来越慢的2种解决方案就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python中使用pngquant压缩png图片的教程
Apr 09 Python
利用Python生成文件md5校验值函数的方法
Jan 10 Python
python中Matplotlib实现绘制3D图的示例代码
Sep 04 Python
python中正则表达式的使用方法
Feb 25 Python
PyQt5每天必学之事件与信号
Apr 20 Python
详解python-图像处理(映射变换)
Mar 22 Python
Python 使用指定的网卡发送HTTP请求的实例
Aug 21 Python
python修改文件内容的3种方法详解
Nov 15 Python
pytorch GAN伪造手写体mnist数据集方式
Jan 10 Python
Python函数的迭代器与生成器的示例代码
Jun 18 Python
python flask开发的简单基金查询工具
Jun 02 Python
详解python itertools功能
Feb 07 #Python
Python中itertools的用法详解
Feb 07 #Python
Python转换itertools.chain对象为数组的方法
Feb 07 #Python
已安装tensorflow-gpu,但keras无法使用GPU加速的解决
Feb 07 #Python
python十进制转二进制的详解
Feb 07 #Python
基于Tensorflow使用CPU而不用GPU问题的解决
Feb 07 #Python
python实现ip地址的包含关系判断
Feb 07 #Python
You might like
ThinkPHP调试模式与日志记录概述
2014/08/22 PHP
EarthLiveSharp中cloudinary的CDN图片缓存自动清理python脚本
2017/04/04 PHP
Yii框架批量插入数据扩展类的简单实现方法
2017/05/23 PHP
实例讲解php实现多线程
2019/01/27 PHP
Nigma vs Liquid BO3 第二场2.14
2021/03/10 DOTA
几款极品的javascript压缩混淆工具
2007/05/16 Javascript
js以对象为索引的关联数组
2010/07/04 Javascript
js focus不起作用的解决方法(主要是因为dom元素是否加载完成)
2010/11/05 Javascript
jquery的ajax跨域请求原理和示例
2014/05/08 Javascript
AngularJS + Node.js + MongoDB开发的基于高德地图位置的通讯录
2015/01/02 Javascript
鼠标经过子元素触发mouseout,mouseover事件的解决方案
2015/07/26 Javascript
轻松实现js弹框显示选项
2016/09/13 Javascript
Angular在一个页面中使用两个ng-app的方法
2017/02/20 Javascript
vue.js 使用v-if v-else发现没有执行解决办法
2017/05/15 Javascript
Angular实现的自定义模糊查询、排序及三角箭头标注功能示例
2017/12/28 Javascript
js推箱子小游戏步骤代码解析
2018/01/10 Javascript
使用Vue.js和Flask来构建一个单页的App的示例
2018/03/21 Javascript
jQuery实现的淡入淡出图片轮播效果示例
2018/08/29 jQuery
[04:53]DOTA2英雄基础教程 祈求者
2014/01/03 DOTA
Python中列表的一些基本操作知识汇总
2015/05/20 Python
Python中unittest模块做UT(单元测试)使用实例
2015/06/12 Python
Python 读写文件和file对象的方法(推荐)
2016/09/12 Python
Python基于OpenCV实现视频的人脸检测
2018/01/23 Python
python实现汉诺塔算法
2021/03/01 Python
对Python的zip函数妙用,旋转矩阵详解
2018/12/13 Python
pymongo中聚合查询的使用方法
2019/03/22 Python
纽约服装和生活方式品牌:Saturdays NYC
2017/08/13 全球购物
Shopty西班牙:缝纫机在线销售
2018/01/26 全球购物
亚洲领先的旅游体验市场:Voyagin
2019/11/23 全球购物
客服端调用EJB对象的几个基本步骤
2012/01/15 面试题
什么是GWT的Entry Point
2013/08/16 面试题
市场营销专业个人求职信范文
2013/12/14 职场文书
2015年驾驶员工作总结
2015/04/29 职场文书
解决Pytorch修改预训练模型时遇到key不匹配的情况
2021/06/05 Python
mongodb清除连接和日志的正确方法分享
2021/09/15 MongoDB
在 HTML 页面中使用 React的场景分析
2022/01/18 Javascript