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 Mysql自动备份脚本
Jul 14 Python
python生成器的使用方法
Nov 21 Python
使用pdb模块调试Python程序实例
Jun 02 Python
Python实现的计数排序算法示例
Nov 29 Python
Python定时发送消息的脚本:每天跟你女朋友说晚安
Oct 21 Python
python生成带有表格的图片实例
Feb 03 Python
Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】
Jun 18 Python
python开启debug模式的方法
Jun 27 Python
完美解决python3.7 pip升级 拒绝访问问题
Jul 12 Python
Python 合并多个TXT文件并统计词频的实现
Aug 23 Python
django框架F&Q 聚合与分组操作示例
Dec 12 Python
Python3 利用face_recognition实现人脸识别的方法
Mar 13 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
PHP备份/还原MySQL数据库的代码
2011/01/06 PHP
php数字游戏 计算24算法
2012/06/10 PHP
php多文件上传功能实现原理及代码
2013/04/18 PHP
php获取访问者IP地址汇总
2015/04/24 PHP
Docker 如何布置PHP开发环境
2016/06/21 PHP
PHP正则判断一个变量是否为正整数的方法
2019/02/27 PHP
关于文本限制字数的js代码
2007/04/02 Javascript
javascript firefox兼容ie的dom方法脚本
2008/05/18 Javascript
番茄的表单验证类代码修改版
2008/07/18 Javascript
JavaScript中使用构造函数实现继承的代码
2010/08/12 Javascript
jquery跨域请求示例分享(jquery发送ajax请求)
2014/03/25 Javascript
在微信、支付宝、百度钱包实现点击返回按钮关闭当前页面和窗口的方法
2016/08/05 Javascript
React实现点击删除列表中对应项
2017/01/10 Javascript
JS排序之选择排序详解
2017/04/08 Javascript
js定义类的方法示例【ES5与ES6】
2019/07/30 Javascript
vue 实现滚动到底部翻页效果(pc端)
2019/07/31 Javascript
scrapyd schedule.json setting 传入多个值问题
2019/08/07 Javascript
Python网络爬虫出现乱码问题的解决方法
2017/01/05 Python
Django学习笔记之Class-Based-View
2017/02/15 Python
python算法演练_One Rule 算法(详解)
2017/05/17 Python
Python类的继承和多态代码详解
2017/12/27 Python
django框架之cookie/session的使用示例(小结)
2018/10/15 Python
python 美化输出信息的实例
2018/10/15 Python
解决Django中多条件查询的问题
2019/07/18 Python
python3获取文件中url内容并下载代码实例
2019/12/27 Python
TensorFlow实现指数衰减学习率的方法
2020/02/05 Python
解决Jupyter NoteBook输出的图表太小看不清问题
2020/04/16 Python
Django form表单与请求的生命周期步骤详解
2020/06/07 Python
您附近的水疗和健康场所:Spafinder(美国)
2019/07/05 全球购物
意大利网上书店:LaFeltrinelli
2020/06/12 全球购物
中药学专业求职信
2014/05/31 职场文书
营销总经理岗位职责范本
2014/09/02 职场文书
小学生田径运动会广播稿
2014/09/11 职场文书
2015年酒店客房部工作总结
2015/04/25 职场文书
如何拟写通知正文?
2019/04/02 职场文书
python字符串常规操作大全
2021/05/02 Python