在新文件中恢复图形和会话后,使用 tf.get_variable 获取变量 [英] Getting a variable using tf.get_variable after graph and session is restored in new file
问题描述
我正在尝试在文件 1 中训练模型,并在另一个文件(文件 2)中恢复和分析权重.
I am trying to train a model in file 1, and restore and analyze the weights in another file (file 2).
在文件 1 中,我使用 get_variable 创建了一个变量
In file 1, I have created a variable using get_variable
with train_graph.as_default():
softmax_wInit = tf.truncated_normal((n_vocab, n_embedding))
softmax_w = tf.get_variable('SMWeightMatrix', initializer = softmax_wInit)
在文件 2 中,我恢复了图形和会话检查点,并尝试使用 get_variable 获取变量
In file 2, I have restored the graph and session checkpoint, and attempted to get the variable using get_variable
with tf.Session() as sess:
saver = tf.train.import_meta_graph('./MODEL4/text8.ckpt.meta')
saver.restore(sess, './MODEL4/text8.ckpt' )
with tf.variable_scope('', reuse=True):
embeddingRestored = tf.get_variable( 'SMWeightMatrix')
但是,我得到ValueError: Variable SMWeightMatrix 不存在,或者不是用 tf.get_variable() 创建的".您的意思是在 VarScope 中设置重用=tf.AUTO_REUSE 吗?'
However, I get 'ValueError: Variable SMWeightMatrix does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?'
但是,如果我查看变量列表,SMWeightMatrix 肯定存在.当我运行此代码时
However, if I look into the list of variables, SMWeightMatrix is definitely there. When I run this code
with tf.Session() as sess:
saver = tf.train.import_meta_graph('./MODEL4/text8.ckpt.meta')
saver.restore(sess, './MODEL4/text8.ckpt' )
for v in tf.get_default_graph().get_collection("variables"):
print(v)
for v in tf.get_default_graph().get_collection("trainable_variables"):
print(v)
这是输出
INFO:tensorflow:Restoring parameters from ./MODEL4/text8.ckpt
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'embedding/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix/Adam_1:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
<tf.Variable 'embedding:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'SMWeightMatrix:0' shape=(63641, 128) dtype=float32_ref>
<tf.Variable 'softmax_bias:0' shape=(63641,) dtype=float32_ref>
我已经尝试在文件 1 中使用类似的代码来使用 get_variable 获取变量并且没有问题
I have tried using similar code in file 1 to get the variable using get_variable and have no problems
with tf.Session(graph=train_graph) as sess:
with tf.variable_scope("", reuse=True):
embeddingRestored = tf.get_variable( 'SMWeightMatrix')
所以这个问题似乎与图形和会话的恢复有关.
So the issue seems related to the restoring of the graph and the session.
推荐答案
你应该使用
tf.get_variable('MyVariableName')
(就像你在你的例子中所做的那样),而不是
(as you did in your example that works), not
tf.get_variable('MyVariableName:0')
这是变量运算符的输出,即它的值(以及您将通过调用 get_variable
返回的张量的名称).
which is the output of the variable operator, i.e its value (and the name of the tensor you will get back by calling get_variable
).
这篇关于在新文件中恢复图形和会话后,使用 tf.get_variable 获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!