TensorFlow入門使用 tf.train.Saver()保存模型
關(guān)于模型保存的一點(diǎn)心得
saver = tf.train.Saver(max_to_keep=3)
在定義 saver 的時(shí)候一般會(huì)定義最多保存模型的數(shù)量,一般來說,如果模型本身很大,我們需要考慮到硬盤大小。如果你需要在當(dāng)前訓(xùn)練好的模型的基礎(chǔ)上進(jìn)行 fine-tune,那么盡可能多的保存模型,后繼 fine-tune 不一定從最好的 ckpt 進(jìn)行,因?yàn)橛锌赡芤幌伦泳瓦^擬合了。但是如果保存太多,硬盤也有壓力呀。如果只想保留最好的模型,方法就是每次迭代到一定步數(shù)就在驗(yàn)證集上計(jì)算一次 accuracy 或者 f1 值,如果本次結(jié)果比上次好才保存新的模型,否則沒必要保存。
如果你想用不同 epoch 保存下來的模型進(jìn)行融合的話,3到5 個(gè)模型已經(jīng)足夠了,假設(shè)這各融合的模型成為 M,而最好的一個(gè)單模型稱為 m_best, 這樣融合的話對(duì)于M 確實(shí)可以比 m_best 更好。但是如果拿這個(gè)模型和其他結(jié)構(gòu)的模型再做融合的話,M 的效果并沒有 m_best 好,因?yàn)镸 相當(dāng)于做了平均操作,減少了該模型的“特性”。
但是又有一種新的融合方式,就是利用調(diào)整學(xué)習(xí)率來獲取多個(gè)局部最優(yōu)點(diǎn),就是當(dāng) loss 降不下了,保存一個(gè) ckpt, 然后開大學(xué)習(xí)率繼續(xù)尋找下一個(gè)局部最優(yōu)點(diǎn),然后用這些 ckpt 來做融合,還沒試過,單模型肯定是有提高的,就是不知道還會(huì)不會(huì)出現(xiàn)上面再與其他模型融合就沒提高的情況。
如何使用 tf.train.Saver() 來保存模型
之前一直出錯(cuò),主要是因?yàn)榭拥木幋a問題。所以要注意文件的路徑絕對(duì)不不要出現(xiàn)什么中文呀。
import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) # Create some variables. v1 = tf.Variable([1.0, 2.3], name="v1") v2 = tf.Variable(55.5, name="v2") # Add an op to initialize the variables. init_op = tf.global_variables_initializer() # Add ops to save and restore all the variables. saver = tf.train.Saver() ckpt_path = './ckpt/test-model.ckpt' # Later, launch the model, initialize the variables, do some work, save the # variables to disk. sess.run(init_op) save_path = saver.save(sess, ckpt_path, global_step=1) print("Model saved in file: %s" % save_path)
Model saved in file: ./ckpt/test-model.ckpt-1
注意,在上面保存完了模型之后。應(yīng)該把 kernel restart 之后才能使用下面的模型導(dǎo)入。否則會(huì)因?yàn)閮纱蚊?“v1” 而導(dǎo)致名字錯(cuò)誤。
import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) # Create some variables. v1 = tf.Variable([11.0, 16.3], name="v1") v2 = tf.Variable(33.5, name="v2") # Add ops to save and restore all the variables. saver = tf.train.Saver() # Later, launch the model, use the saver to restore variables from disk, and # do some work with the model. # Restore variables from disk. ckpt_path = './ckpt/test-model.ckpt' saver.restore(sess, ckpt_path + '-'+ str(1)) print("Model restored.") print sess.run(v1) print sess.run(v2)
INFO:tensorflow:Restoring parameters from ./ckpt/test-model.ckpt-1
Model restored.
[ 1. 2.29999995]
55.5
導(dǎo)入模型之前,必須重新再定義一遍變量。
但是并不需要全部變量都重新進(jìn)行定義,只定義我們需要的變量就行了。
也就是說,你所定義的變量一定要在 checkpoint 中存在;但不是所有在checkpoint中的變量,你都要重新定義。
import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) # Create some variables. v1 = tf.Variable([11.0, 16.3], name="v1") # Add ops to save and restore all the variables. saver = tf.train.Saver() # Later, launch the model, use the saver to restore variables from disk, and # do some work with the model. # Restore variables from disk. ckpt_path = './ckpt/test-model.ckpt' saver.restore(sess, ckpt_path + '-'+ str(1)) print("Model restored.") print sess.run(v1)
INFO:tensorflow:Restoring parameters from ./ckpt/test-model.ckpt-1
Model restored.
[ 1. 2.29999995]
tf.Saver([tensors_to_be_saved]) 中可以傳入一個(gè) list,把要保存的 tensors 傳入,如果沒有給定這個(gè)list的話,他會(huì)默認(rèn)保存當(dāng)前所有的 tensors。一般來說,tf.Saver 可以和 tf.variable_scope() 巧妙搭配,可以參考: 【遷移學(xué)習(xí)】往一個(gè)已經(jīng)保存好的模型添加新的變量并進(jìn)行微調(diào)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ollama搭建本地ai大模型并應(yīng)用調(diào)用的操作方法
- 大語言模型的開發(fā)利器langchainan安裝使用快速入門學(xué)習(xí)
- langchain Prompt大語言模型使用技巧詳解
- Docker?AIGC等大模型深度學(xué)習(xí)環(huán)境搭建步驟最新詳細(xì)版
- 前端AI機(jī)器學(xué)習(xí)在瀏覽器中訓(xùn)練模型
- AI:如何訓(xùn)練機(jī)器學(xué)習(xí)的模型
- django數(shù)據(jù)模型on_delete, db_constraint的使用詳解
- Python從零開始訓(xùn)練AI模型的實(shí)用教程
相關(guān)文章
python 線性回歸分析模型檢驗(yàn)標(biāo)準(zhǔn)--擬合優(yōu)度詳解
今天小編就為大家分享一篇python 線性回歸分析模型檢驗(yàn)標(biāo)準(zhǔn)--擬合優(yōu)度詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python實(shí)現(xiàn)線程狀態(tài)監(jiān)測(cè)簡(jiǎn)單示例
這篇文章主要介紹了Python實(shí)現(xiàn)線程狀態(tài)監(jiān)測(cè),結(jié)合簡(jiǎn)單實(shí)例形式分析了Python線程start啟動(dòng)、sleep推遲運(yùn)行、isAlive判斷等方法使用技巧,需要的朋友可以參考下2018-03-03python將YUV420P文件轉(zhuǎn)PNG圖片格式的兩種方法
這篇文章主要介紹了python將YUV420P文件轉(zhuǎn)PNG圖片格式的兩種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01python中關(guān)于range()函數(shù)反向遍歷的幾種表達(dá)
這篇文章主要介紹了python中關(guān)于range()函數(shù)反向遍歷的幾種表達(dá),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05