首頁  >  文章  >  後端開發  >  TensorFlow入門使用 tf.train.Saver()儲存模型

TensorFlow入門使用 tf.train.Saver()儲存模型

不言
不言原創
2018-04-24 14:15:074036瀏覽

這篇文章主要介紹了TensorFlow入門使用 tf.train.Saver()保存模型,現在分享給大家,也給大家做個參考。一起來看看吧

關於模型保存的一點心得

saver = tf.train.Saver(max_to_keep=3)

在定義saver 的時候一般會定義最多保存模型的數量,一般來說,如果模型本身很大,我們需要考慮到硬碟大小。如果你需要在目前訓練好的模型的基礎上進行 fine-tune,那麼盡可能多的保存模型,後繼 fine-tune 不一定從最好的 ckpt 進行,因為有可能一下子就過擬合了。但如果保存太多,硬碟也有壓力呀。如果只想保留最好的模型,方法就是每次迭代到一定步數就在驗證集上計算一次 accuracy 或 f1 值,如果本次結果比上次好才保存新的模型,否則沒必要保存。

如果你想用不同epoch 保存下來的模型進行融合的話,3到5 個模型已經足夠了,假設這各融合的模型成為M,而最好的一個單模型稱為m_best, 這樣融合的話對於M 確實可以比m_best 更好。但如果拿這個模型和其他結構的模型再做融合的話,M 的效果並沒有 m_best 好,因為M 相當於做了平均操作,減少了該模型的「特性」。

但又有一種新的融合方式,就是利用調整學習率來獲取多個局部最優點,就是當loss 降不下了,保存一個ckpt, 然後開大學習率繼續尋找下一個局部最優點,然後用這些ckpt 來做融合,還沒試過,單模型肯定是有提高的,就是不知道還會不會出現上面再與其他模型融合就沒提高的情況。

如何使用 tf.train.Saver() 來保存模型

之前一直出錯,主要是因為坑爹的編碼問題。所以要注意文件的路徑絕對不不要出現什麼中文呀。

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

注意,在上面保存了模型之後。應該把 kernel restart 之後才能使用下面的模型導入。否則會因為兩次命名 “v1” 而導致名字錯誤。

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

在導入模型前,必須先重新定義一次變數。

但是不需要全部變數都重新定義,只定義我們需要的變數就行了。

也就是說,你所定義的變數一定要在 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]) 中可以傳入一個list,將要保存的tensors 傳入,如果沒有給定這個list保存目前所有的tensors。一般來說,tf.Saver 可以和tf.variable_scope() 巧妙搭配,可以參考: 【遷移學習】往一個已經保存好的模型添加新的變數並進行微調

相關推薦:

關於Tensorflow中的tf.train.batch函數

以上是TensorFlow入門使用 tf.train.Saver()儲存模型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn