2017-10-12 18 views
0

我正在訓練Tensorflow中的卷積神經網絡。我的代碼運行完成沒有錯誤。這就是說,我很難理解我如何保存神經網絡學習的權重和偏見(當我在服務器上訓練時,這很重要,並且希望在本地執行更簡單的可視化工作)。關於在Tensorflow中保存/恢復訓練過的權重和偏差感到困惑

我初始化我的重量和偏見正是如此:

weights = { 
'wConv1': tf.Variable(tf.random_normal([5, 5, 1, 3],0,0.25), name='wC1'), 
'wConv2': tf.Variable(tf.random_normal([5, 5, 3, 32],0,0.25), name='wC2'), 
'wConv3': tf.Variable(tf.random_normal([5, 5, 32, 64],0,0.25), name='wC3'), 
'wConv4': tf.Variable(tf.random_normal([5, 5, 64, 128],0,0.25), name='wC4'), 
'wConv5': tf.Variable(tf.random_normal([5, 5, 128, 64],0,0.25), name='wC5'), 
'wConv6': tf.Variable(tf.random_normal([5, 5, 64, 32],0,0.25), name='wC6'), 
'wConv7': tf.Variable(tf.random_normal([5, 5, 32, 16],0,0.25), name='wC7'), 
'wOUT' : tf.Variable(tf.random_normal([5, 5, 16, 1],0,0.25),   name='wCOUT') 
} 

biases = { 
'bConv1': tf.Variable(tf.random_normal([3]), name='bC1'), 
'bConv2': tf.Variable(tf.random_normal([32]), name='bC2'), 
'bConv3': tf.Variable(tf.random_normal([64]), name='bC3'), 
'bConv4': tf.Variable(tf.random_normal([128]), name='bC4'), 
'bConv5': tf.Variable(tf.random_normal([64]), name='bC5'), 
'bConv6': tf.Variable(tf.random_normal([32]), name='bC6'), 
'bConv7': tf.Variable(tf.random_normal([16]), name='bC7'), 
'bOUT': tf.Variable(tf.random_normal([1]),  name='bCOUT') 
} 

然後,一旦然而,許多時期我運行完成後,我使用下面的保存一切:

saver = tf.train.Saver({"weights": weights, "biases": biases}) 
save_path = saver.save(sess, "./output/trained.ckpt")  

現在,我自己機器我有一個評估腳本,其中我嘗試加載的權重:

with sess.as_default(): 
      saver = tf.train.import_meta_graph('output.ckpt.meta') 
      saver.restore(sess,tf.train.latest_checkpoint('./')) 
      a= tf.all_variables() 
      sess.run(tf.global_variables_initializer()) 
      b=sess.run(pred,feed_dict={x: input[:,:,:,30,:]}) 

現在,這個問題是,當我在加載「一」我得到一個爛攤子,什麼似乎是我的偏見和體重變量的多個副本:

<tf.Variable 'wC1:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC2:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC3:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC4:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC5:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC6:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC7:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wCOUT:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'bC1:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC2:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC3:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC4:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC5:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC6:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC7:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bCOUT:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>, 
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>, 
<tf.Variable 'wC1/Adam:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC1/Adam_1:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC2/Adam:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC2/Adam_1:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC3/Adam:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC3/Adam_1:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC4/Adam:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC4/Adam_1:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC5/Adam:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC5/Adam_1:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC6/Adam:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC6/Adam_1:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC7/Adam:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wC7/Adam_1:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wCOUT/Adam:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'wCOUT/Adam_1:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'bC1/Adam:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC1/Adam_1:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC2/Adam:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC2/Adam_1:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC3/Adam:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC3/Adam_1:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC4/Adam:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC4/Adam_1:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC5/Adam:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC5/Adam_1:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC6/Adam:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC6/Adam_1:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC7/Adam:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bC7/Adam_1:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bCOUT/Adam:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'bCOUT/Adam_1:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'wC1:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC2:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC3:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC4:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC5:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC6:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC7:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wCOUT:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'bC1:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC2:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC3:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC4:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC5:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC6:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC7:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bCOUT:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>, 
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>, 
<tf.Variable 'wC1/Adam:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC1/Adam_1:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC2/Adam:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC2/Adam_1:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC3/Adam:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC3/Adam_1:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC4/Adam:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC4/Adam_1:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC5/Adam:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC5/Adam_1:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC6/Adam:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC6/Adam_1:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC7/Adam:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wC7/Adam_1:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wCOUT/Adam:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'wCOUT/Adam_1:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'bC1/Adam:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC1/Adam_1:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC2/Adam:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC2/Adam_1:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC3/Adam:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC3/Adam_1:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC4/Adam:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC4/Adam_1:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC5/Adam:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC5/Adam_1:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC6/Adam:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC6/Adam_1:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC7/Adam:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bC7/Adam_1:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bCOUT/Adam:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'bCOUT/Adam_1:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'wC1:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC2:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC3:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC4:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC5:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC6:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC7:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wCOUT:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'bC1:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC2:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC3:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC4:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC5:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC6:0' shape=(32,) dtype=float32_ref>, 


<tf.Variable 'bC7:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bCOUT:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>, 
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>, 
<tf.Variable 'wC1/Adam:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC1/Adam_1:0' shape=(5, 5, 1, 3) dtype=float32_ref>, 
<tf.Variable 'wC2/Adam:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC2/Adam_1:0' shape=(5, 5, 3, 32) dtype=float32_ref>, 
<tf.Variable 'wC3/Adam:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC3/Adam_1:0' shape=(5, 5, 32, 64) dtype=float32_ref>, 
<tf.Variable 'wC4/Adam:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC4/Adam_1:0' shape=(5, 5, 64, 128) dtype=float32_ref>, 
<tf.Variable 'wC5/Adam:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC5/Adam_1:0' shape=(5, 5, 128, 64) dtype=float32_ref>, 
<tf.Variable 'wC6/Adam:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC6/Adam_1:0' shape=(5, 5, 64, 32) dtype=float32_ref>, 
<tf.Variable 'wC7/Adam:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wC7/Adam_1:0' shape=(5, 5, 32, 16) dtype=float32_ref>, 
<tf.Variable 'wCOUT/Adam:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'wCOUT/Adam_1:0' shape=(5, 5, 16, 1) dtype=float32_ref>, 
<tf.Variable 'bC1/Adam:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC1/Adam_1:0' shape=(3,) dtype=float32_ref>, 
<tf.Variable 'bC2/Adam:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC2/Adam_1:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC3/Adam:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC3/Adam_1:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC4/Adam:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC4/Adam_1:0' shape=(128,) dtype=float32_ref>, 
<tf.Variable 'bC5/Adam:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC5/Adam_1:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'bC6/Adam:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC6/Adam_1:0' shape=(32,) dtype=float32_ref>, 
<tf.Variable 'bC7/Adam:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bC7/Adam_1:0' shape=(16,) dtype=float32_ref>, 
<tf.Variable 'bCOUT/Adam:0' shape=(1,) dtype=float32_ref>, 
<tf.Variable 'bCOUT/Adam_1:0' shape=(1,) dtype=float32_ref>] 

我的問題是,我怎麼能只保存訓練的權重和偏見Tensorflow,然後加載它們以用於測試目的?

+0

看看這個:https://www.tensorflow。org/api_guides/python/meta_graph – BlooB

回答

0

之前回答確切的問題,讓我先解決您的問題:

的問題是,當我加載在「一」我得到一個爛攤子,有什麼似乎是 我的偏見的許多副本和重量變量

在你評估你的腳本加載你的訓練元圖:

saver = tf.train.import_meta_graph('output.ckpt.meta') 

內部的圖形,d除了你定義的顯式權重和偏差變量之外,還有與優化過程相關的變量(即帶有後綴adam或beta1_power的變量)。執行上面指定的行,它們在您的評估腳本中再次定義,但可能不一定需要進行推斷。

另一種方法是定義要進行推理的確切圖形,這可能與訓練有點不同。在你的情況 - 只是沒有定義優化器。

我們解決您的問題:

我的問題是,我怎麼能只保存在 Tensorflow的訓練的權重和偏見,然後加載它們後來用於測試目的?

從你的代碼看來,你的本質上就是這樣做的。你看到的其他變量源於上述。

有一點需要指出 - 確保在恢復變量後不要初始化變量。如果你留在當前的代碼,首先進行初始化,然後恢復。如果您打算更改推理圖並且不包含優化器,則不需要初始化任何變量。

+0

我遇到的問題是我需要將權重和偏差定義爲tf.placeholder()的字典,以便定義預測操作。然後,當我運行Session塊時,我收到一個錯誤消息,說這些佔位符沒有值。 – Karl

+0

我不明白,將不得不看到確切的代碼.. – amirbar

+0

我是這個網站的新手,它看起來像代碼太長,無法發表評論。還有另一種分享方式嗎? – Karl

相關問題