2017-09-13 181 views
1

我用這個救了我的神經網絡參數的模型:保存和恢復使用tensorflow

parameters = { 
    'w_h1': w_h1, 
    'b_h1': b_h1, 
    'w_h2': w_h2, 
    'b_h2': b_h2, 
    'w_h3': w_h3, 
    'b_h3': b_h3, 
    'w_o': w_o, 
    'b_o': b_o 
} 

saver = tf.train.Saver(parameters) 

saver.save(sess, 'my-model', global_step=epoch) 

現在,我已經在我的硬盤這3個文件:

checkpoint 

my-model-114000 

my-model-114000.meta 

我想是這樣的:

with tf.Session() as sess: 
    new_saver = tf.train.import_meta_graph('my-model-114000.meta') 
    new_saver.restore(sess, 'my-model-114000') 

我接收到的消息:

INFO:tensorflow:Restoring parameters from my-model-114000 

但是,我無法恢復原始參數。我試圖像這樣(內部的與tf.Session()作爲SESS)

w_h1 = tf.get_default_graph()get_tensor_by_name( 「w_h1:0」)。

但我收到消息

KeyError: "The name 'w_h1:0' refers to a Tensor which does not exist. The operation, 'w_h1', does not exist in the graph." 

但是,我無法恢復權重。我怎樣才能做到這一點?

我用

for var in tf.all_variables(): 
     print str(var) 

知道已經保存,我意識到,這節省了很多東西(以下只是一個樣本),但是我雖然我已保存的只有少數的重要參數:

<tf.Variable 'Variable_21/Adam_3:0' shape=(50,) dtype=float32_ref> 
<tf.Variable 'Variable_24/Adam_2:0' shape=(50, 50) dtype=float32_ref> 
<tf.Variable 'Variable_24/Adam_3:0' shape=(50, 50) dtype=float32_ref> 
<tf.Variable 'Variable_25/Adam_2:0' shape=(50,) dtype=float32_ref> 
<tf.Variable 'Variable_25/Adam_3:0' shape=(50,) dtype=float32_ref> 
<tf.Variable 'Variable_28/Adam_2:0' shape=(50, 1) dtype=float32_ref> 
<tf.Variable 'Variable_28/Adam_3:0' shape=(50, 1) dtype=float32_ref> 
<tf.Variable 'Variable_29/Adam_2:0' shape=(1,) dtype=float32_ref> 
<tf.Variable 'Variable_29/Adam_3:0' shape=(1,) dtype=float32_ref> 
>>> 
+0

確保選中初始化'張量流程圖「與您在保存期間所做的相同的圖形體系結構。 – Ultraviolet

+0

@Ultraviolet:我該怎麼做? – DanielTheRocketMan

+0

此外,即使我定義要保存的參數tensorflow保存所有變量? – DanielTheRocketMan

回答

1

的名字,像'Variable_21/Adam_3:0'是你的變量名和"w_h1"不,你應該得到這個張量w_h1 = tf.get_default_graph().get_tensor_by_name("Variable_21/Adam_3:0")

+0

它可能是w_h1,w_h2或w_h3。但是,我怎麼知道哪一個?我以爲我應該通過名稱「w_h1:0」恢復變量。 – DanielTheRocketMan

+1

你可以用'tf.Variable(name =「w_h1」)''來創建變量,並將其命名爲變量。 –

+0

恢復權重的問題是我沒有命名這些變量。現在我做到了! – DanielTheRocketMan