我正在尝试在文件1中训练模型,并在另一个文件(文件2)中恢复和分析权重。
在文件1中,我使用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
获取变量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:变量SMWeightMatrix不存在,或者不是用tf.get_variable()创建的。你的意思是在VarScope中设置reuse = tf.AUTO_REUSE吗?'
但是,如果我查看变量列表,SMWeightMatrix绝对存在。当我运行此代码时
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获取变量并且没有问题
with tf.Session(graph=train_graph) as sess:
with tf.variable_scope("", reuse=True):
embeddingRestored = tf.get_variable( 'SMWeightMatrix')
因此问题似乎与恢复图表和会话有关。
答案 0 :(得分:0)
你应该使用
tf.get_variable('MyVariableName')
(正如你在你的例子中所做的那样),而不是
tf.get_variable('MyVariableName:0')
这是变量运算符的输出,即它的值(以及通过调用get_variable
得到的张量的名称)。