在新文件中还原图表和会话后使用tf.get_variable获取变量

2024-04-20 08:17:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在文件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获取变量

^{pr2}$

但是,我得到'ValueError:变量SMWeightMatrix不存在,或者不是用tf.get_变量(). 你是想设置重用吗=自动重用在VarScope?'在

但是,如果我查看变量列表,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')

所以这个问题似乎与恢复图形和会话有关。在


Tags: refgettfembeddingvariablepowershapedtype
1条回答
网友
1楼 · 发布于 2024-04-20 08:17:47

你应该用

tf.get_variable('MyVariableName')

(就像你在你的例子中所做的那样),不是

^{pr2}$

这是变量运算符的输出,即其值(以及通过调用get_variable返回的张量的名称)。在

相关问题 更多 >