Tensorflow训练模型越来越慢的2种解决方案
作者:xdq101 时间:2021-06-04 20:55:53
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()就是载入图结构的类,将该类的实例化放在全局,即会话外部,解决训练越来越慢。
来源:https://blog.csdn.net/Xin_101/article/details/90232659
标签:Tensorflow,训练,模型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Pandas知识点之缺失值处理详解
2023-09-29 20:23:16
![](https://img.aspxhome.com/file/2023/0/63220_0s.png)
Python利用memory_profiler查看内存占用情况
2022-05-24 08:55:29
![](https://img.aspxhome.com/file/2023/0/81810_0s.png)
Python实现合并excel表格的方法分析
2022-04-24 21:30:22
500行代码使用python写个微信小游戏飞机大战游戏
2023-01-16 06:58:09
![](https://img.aspxhome.com/file/2023/3/76703_0s.gif)
jupyter notebook清除输出方式
2021-05-08 02:51:13
![](https://img.aspxhome.com/file/2023/3/87993_0s.jpg)
matplotlib设置legend图例代码示例
2023-04-18 13:20:12
![](https://img.aspxhome.com/file/2023/5/78515_0s.jpg)
如何使用python实现模拟鼠标点击
2022-07-07 21:46:57
Python解析多帧dicom数据详解
2022-08-13 21:16:13
Python实现自动化处理Word文档的方法详解
2022-05-24 00:33:59
![](https://img.aspxhome.com/file/2023/2/72782_0s.jpg)
python arcpy练习之面要素重叠拓扑检查
2021-12-24 12:41:42
![](https://img.aspxhome.com/file/2023/7/81797_0s.jpg)
JS分割字符串并放入数组的函数
2023-08-05 15:31:03
我的css样式写法总结
2009-01-18 13:04:00
CSS用relative和absolute实现的图片定位效果
2008-10-06 21:37:00
![](https://img.aspxhome.com/file/UploadPic/200810/6/2008106214346662s.gif)
Python3.10耙梳加密算法Encryption种类及开发场景
2021-07-19 00:46:55
![](https://img.aspxhome.com/file/2023/7/89167_0s.png)
python 基于selenium实现鼠标拖拽功能
2022-10-30 17:03:07
Microsoft VBScript 运行时错误 错误 800a0005 无效的过程调用或参数: chr
2011-03-09 11:03:00
用Asp+XmlHttp实现RssReader功能
2008-07-09 12:20:00
JSP安全开发之XSS漏洞详解
2023-06-13 13:07:24
![](https://img.aspxhome.com/file/2023/1/62781_0s.png)
python遗传算法之单/多目标规划问题
2021-09-09 20:27:24
![](https://img.aspxhome.com/file/2023/8/76878_0s.png)
python 函数的缺省参数使用注意事项分析
2021-08-23 05:09:02