我用这个保存了神经网络的参数:
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)
现在我的磁盘上有这三个文件:
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 :(得分:1)
'Variable_21/Adam_3:0'
之类的名称是您的变量名称而"w_h1"
不是,您应该使用w_h1 = tf.get_default_graph().get_tensor_by_name("Variable_21/Adam_3:0")
获得此张量