tensorflow saver 保存和恢復指定 tensor的實例講解
在實踐中經(jīng)常會遇到這樣的情況:
1、用簡單的模型預訓練參數(shù)
2、把預訓練的參數(shù)導入復雜的模型后訓練復雜的模型
這時就產(chǎn)生一個問題:
如何加載預訓練的參數(shù)。
下面就是我的總結。
為了方便說明,做一個假設:簡單的模型只有一個卷基層,復雜模型有兩個。
卷積層的實現(xiàn)代碼如下:
import tensorflow as tf # PS:本篇的重擔是saver,不過為了方便閱讀還是說明下參數(shù) # 參數(shù) # name:創(chuàng)建卷基層的代碼這么多,必須要函數(shù)化,而為了防止變量沖突就需要用tf.name_scope # input_data:輸入數(shù)據(jù) # width, high:卷積小窗口的寬、高 # deep_before, deep_after:卷積前后的神經(jīng)元數(shù)量 # stride:卷積小窗口的移動步長 def make_conv(name, input_data, width, high, deep_before,deep_after, stride, padding_type='SAME'): global parameters with tf.name_scope(name) asscope: weights =tf.Variable(tf.truncated_normal([width, high, deep_before, deep_after], dtype=tf.float32,stddev=0.01), trainable=True, name='weights') biases =tf.Variable(tf.constant(0.1, shape=[deep_after]), trainable=True, name='biases') conv =tf.nn.conv2d(input_data, weights, [1, stride, stride, 1], padding=padding_type) bias = tf.add(conv,biases) bias = batch_norm(bias,deep_after, 1) # batch_norm是自己寫的batchnorm函數(shù) conv =tf.maximum(0.1*bias, bias) return conv
簡單的預訓練模型就下面一句話
conv1 =make_conv('simple-conv1', images, 3, 3, 3, 32, 1)
復雜的模型是兩個卷基層,如下:
conv1 = make_conv('complex-conv1',images, 3, 3, 3, 32, 1)
pool1= make_max_pool('layer1-pool1', conv1, 2, 2)
conv2= make_conv('complex-conv2', pool1, 3, 3, 32, 64, 1)
這時簡簡單單的在預訓練模型中:
saver = tf.train.Saver() with tf.Session() as sess: saver.save(sess,'model.ckpt')
就不行了,因為:
1,如果你在預訓練模型中使用下面的話打印所有tensor
all_v =tf.global_variables() for i in all_v: print i
會發(fā)現(xiàn)tensor的名字不是weights和biases,而是'simple-conv1/weights和'simple-conv1/biases,如下:
<tf.Variable'simple-conv1/weights:0' shape=(3, 3, 3, 32) dtype=float32_ref> <tf.Variable'simple-conv1/biases:0' shape=(32,) dtype=float32_ref> <tf.Variable 'simple-conv1/Variable:0' shape=(32,)dtype=float32_ref> <tf.Variable 'simple-conv1/Variable_1:0' shape=(32,)dtype=float32_ref> <tf.Variable 'simple-conv1/Variable_2:0' shape=(32,)dtype=float32_ref> <tf.Variable 'simple-conv1/Variable_3:0' shape=(32,)dtype=float32_ref>
同理,在復雜模型中就是complex-conv1/weights和complex-conv1/biases,這是對不上號的。
2,預訓練模型中只有1個卷積層,而復雜模型中有兩個,而tensorflow默認會從模型文件('model.ckpt')中找所有的“可訓練的”tensor,找不到會報錯。
解決方法:
1,在預訓練模型中定義全局變量
parm_dict={}
并在“return conv”上面添加下面兩行
parm_dict['complex-conv1/weights']= weights parm_dict['complex-conv1/']= biases
然后在定義saver時使用下面這句話:
saver= tf.train.Saver(parm_dict)
這樣保存后的模型文件就對應到復雜模型上了。
2,在復雜模型中定義全局變量
parameters= []
并在“return conv”上面添加下面行
parameters+= [weights, biases]
然后判斷如果是第二個卷積層就不更新parameters。
接著在定義saver時使用下面這句話:
saver= tf.train.Saver(parameters)
這樣就可以告訴saver,只需要從模型文件中找weights和biases,而那些什么complex-conv1/Variable~ complex-conv1/Variable_3統(tǒng)統(tǒng)滾一邊去(上面紅色部分)。
最后使用下面的代碼加載就可以了
with tf.Session() as sess:
ckpt= tf.train.get_checkpoint_state('.')
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess,ckpt.model_checkpoint_path)
else:
print ' no saver.'
exit()
以上這篇tensorflow saver 保存和恢復指定 tensor的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解Open Folder as PyCharm Project怎么添加的方法
這篇文章主要介紹了詳解Open Folder as PyCharm Project怎么添加的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
詳解python實現(xiàn)數(shù)據(jù)歸一化處理的方式:(0,1)標準化
這篇文章主要介紹了詳解python實現(xiàn)數(shù)據(jù)歸一化處理的方式:(0,1)標準化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Python實現(xiàn)批量梯度下降法(BGD)擬合曲線
這篇文章主要介紹了Python實現(xiàn)批量梯度下降法(BGD)擬合曲線,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04

