Tensorflow訓(xùn)練模型越來(lái)越慢的2種解決方案
1 解決方案
【方案一】
載入模型結(jié)構(gòu)放在全局,即tensorflow會(huì)話外層。
'''載入模型結(jié)構(gòu):最關(guān)鍵的一步''' saver = tf.train.Saver() '''建立會(huì)話''' with tf.Session() as sess: for i in range(STEPS): '''開始訓(xùn)練''' _, 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)
【方案二】
在方案一的基礎(chǔ)上,將模型結(jié)構(gòu)放在圖會(huì)話的外部。
'''預(yù)測(cè)值'''
train_logits= network_model.inference(inputs, keep_prob)
'''損失值'''
train_loss = network_model.losses(train_logits)
'''優(yōu)化'''
train_op = network_model.train(train_loss, learning_rate)
'''準(zhǔn)確率'''
train_acc = network_model.evaluation(train_logits, labels)
'''模型輸入'''
feed_dict = {inputs: x_batch, labels: y_batch, keep_prob: 0.5}
'''載入模型結(jié)構(gòu)'''
saver = tf.train.Saver()
'''建立會(huì)話'''
with tf.Session() as sess:
for i in range(STEPS):
'''開始訓(xùn)練'''
_, 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 時(shí)間測(cè)試
通過不同方法測(cè)試訓(xùn)練程序,得到不同的訓(xùn)練時(shí)間,每執(zhí)行一次訓(xùn)練都重新載入圖結(jié)構(gòu),會(huì)使每一步的訓(xùn)練時(shí)間逐次增加,如果訓(xùn)練步數(shù)越大,后面訓(xùn)練速度越來(lái)越慢,最終可導(dǎo)致圖爆炸,而終止訓(xùn)練。
【時(shí)間累加】
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
【時(shí)間均衡】
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使用圖結(jié)構(gòu)構(gòu)建系統(tǒng),圖結(jié)構(gòu)中有節(jié)點(diǎn)(node)和邊(operation),每次進(jìn)行計(jì)算時(shí)會(huì)向圖中添加邊和節(jié)點(diǎn)進(jìn)行計(jì)算或者讀取已存在的圖結(jié)構(gòu);
(2) 使用圖結(jié)構(gòu)也是一把雙刃之劍,可以加快計(jì)算和提高設(shè)計(jì)效率,但是,程序設(shè)計(jì)不合理會(huì)導(dǎo)向負(fù)面,使訓(xùn)練越來(lái)約慢;
(3) 訓(xùn)練越來(lái)越慢是因?yàn)檫\(yùn)行一次sess.run,向圖中添加一次節(jié)點(diǎn)或者重新載入一次圖結(jié)構(gòu),導(dǎo)致圖中節(jié)點(diǎn)和邊越來(lái)越多,計(jì)算參數(shù)也成倍增長(zhǎng);
(4) tf.train.Saver()就是載入圖結(jié)構(gòu)的類,因此設(shè)計(jì)訓(xùn)練程序時(shí),若每執(zhí)行一次跟新就使用該類載入圖結(jié)構(gòu),自然會(huì)增加參數(shù)數(shù)量,必然導(dǎo)致訓(xùn)練變慢;
(5) 因此,將載入圖結(jié)構(gòu)的類放在全局,即只載入一次圖結(jié)構(gòu),其他時(shí)間只訓(xùn)練圖結(jié)構(gòu)中的參數(shù),可保持原有的訓(xùn)練速度;
4 總結(jié)
(1) 設(shè)計(jì)訓(xùn)練網(wǎng)絡(luò),只載入一次圖結(jié)構(gòu)即可;
(2) tf.train.Saver()就是載入圖結(jié)構(gòu)的類,將該類的實(shí)例化放在全局,即會(huì)話外部,解決訓(xùn)練越來(lái)越慢。
以上這篇Tensorflow訓(xùn)練模型越來(lái)越慢的2種解決方案就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python OpenCV基于霍夫圈變換算法檢測(cè)圖像中的圓形
這篇文章主要介紹了通過霍夫圈變換算法檢測(cè)圖像中的圓形,文中用到的函數(shù)為cv2.HoughCircles(),該函數(shù)可以很好地檢測(cè)圓心。感興趣的小伙伴可以了解一下2021-12-12
matplotlib交互式數(shù)據(jù)光標(biāo)實(shí)現(xiàn)(mplcursors)
這篇文章主要介紹了matplotlib交互式數(shù)據(jù)光標(biāo)實(shí)現(xiàn)(mplcursors),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)
這篇文章主要介紹了python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-03-03
Python實(shí)現(xiàn)腳本轉(zhuǎn)換為命令行程序
使用Python中的scaffold和click庫(kù),你可以將一個(gè)簡(jiǎn)單的實(shí)用程序升級(jí)為一個(gè)成熟的命令行界面工具,本文就來(lái)帶你看看具體實(shí)現(xiàn)方法,感興趣的可以了解下2022-09-09
Python基礎(chǔ)教程之tcp socket編程詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Python基礎(chǔ)教程之tcp socket編程詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
如何用tempfile庫(kù)創(chuàng)建python進(jìn)程中的臨時(shí)文件
這篇文章主要介紹了如何用tempfile庫(kù)創(chuàng)建python進(jìn)程中的臨時(shí)文件,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
python調(diào)用可執(zhí)行文件.exe的2種實(shí)現(xiàn)方法
Python是一種流行的編程語(yǔ)言,可以輕松地通過腳本調(diào)用各種應(yīng)用程序,本文就詳細(xì)的介紹了python調(diào)用可執(zhí)行文件.exe的2種實(shí)現(xiàn)方法,感興趣的可以了解一下2023-08-08

