解决Tensorflow训练速度随步数增加而变慢的问题
本文分析Tensorflow训练过程中因重复加载图结构导致速度变慢的原因,并提供两种解决方案:将模型结构载入全局或置于会话外部。通过对比测试数据,展示优化后训练时间的均衡性,帮助开发者避免图爆炸问题,提升训练效率。
解决方案概述
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种解决方案就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持本站。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













