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设计模式之抽象工厂模式
Aug 25 Python
Python使用pip安装报错:is not a supported wheel on this platform的解决方法
Jan 23 Python
Python使用 Beanstalkd 做异步任务处理的方法
Apr 24 Python
Python之批量创建文件的实例讲解
May 10 Python
利用Python如何制作好玩的GIF动图详解
Jul 11 Python
python异步存储数据详解
Mar 19 Python
Python远程视频监控程序的实例代码
May 05 Python
详解如何管理多个Python版本和虚拟环境
May 10 Python
使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解
Jan 25 Python
利用python在excel中画图的实现方法
Mar 17 Python
完美解决Pycharm中matplotlib画图中文乱码问题
Jan 11 Python
如何获取numpy array前N个最大值
May 14 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
基于文本的访客签到簿
2006/10/09 PHP
PHP 各种排序算法实现代码
2009/08/20 PHP
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
2013/07/03 PHP
php链表用法实例分析
2015/07/09 PHP
php实现生成验证码实例分享
2016/04/10 PHP
php过滤htmlspecialchars() 函数实现把预定义的字符转换为 HTML 实体用法分析
2019/06/25 PHP
js中单引号与双引号冲突问题解决方法
2013/10/04 Javascript
jquery统计复选框选中示例
2013/11/05 Javascript
JS关闭窗口与JS关闭页面的几种方法小结
2013/12/17 Javascript
js toFixed()方法的重写实现精度的统一
2014/03/06 Javascript
jQuery圆形统计图开发实例
2015/01/04 Javascript
jQuery弹出框代码封装DialogHelper
2015/01/30 Javascript
jquery通过扩展select控件实现支持enter或focus选择的方法
2015/11/19 Javascript
JavaScript ParseFloat()方法
2015/12/18 Javascript
jQuery实现弹出带遮罩层的居中浮动窗口效果
2016/09/12 Javascript
jquery自定义插件结合baiduTemplate.js实现异步刷新(附源码)
2016/12/22 Javascript
JavaScript两种计时器的实例讲解
2019/01/31 Javascript
Node.js折腾记一:读指定文件夹,输出该文件夹的文件树详解
2019/04/20 Javascript
vue-cli3 项目优化之通过 node 自动生成组件模板 generate View、Component
2019/04/30 Javascript
layui实现显示数据表格、搜索和修改功能示例
2020/06/03 Javascript
[02:27]DOTA2英雄基础教程 莱恩
2014/01/17 DOTA
基于Python的身份证号码自动生成程序
2014/08/15 Python
Python 多进程和数据传递的理解
2017/10/09 Python
Python 单例设计模式用法实例分析
2019/09/23 Python
Python IDE环境之 新版Pycharm安装详细教程
2020/03/05 Python
PyQt5事件处理之定时在控件上显示信息的代码
2020/03/25 Python
CSS3模拟动画下拉菜单效果
2017/04/12 HTML / CSS
Stutterheim瑞典:瑞典高级外套时装品牌
2019/06/24 全球购物
安德玛加拿大官网:Under Armour加拿大
2019/10/02 全球购物
医学生求职自荐信
2013/10/25 职场文书
初婚未育证明样本
2014/10/24 职场文书
2016新年年会主持词
2015/07/06 职场文书
运动会新闻报道稿
2015/07/22 职场文书
uni-app 微信小程序授权登录的实现步骤
2022/02/18 Javascript
Python列表的索引与切片
2022/04/07 Python
浅谈音视频 pts dts基本概念及理解
2022/08/05 数码科技