【问题标题】:KeyError : The tensor variable , Refer to the tensor which does not existsKeyError:张量变量,指不存在的张量
【发布时间】:2018-03-08 06:33:18
【问题描述】:

使用 LSTMCell 我训练了一个模型来生成文本。我启动了 tensorflow 会话并使用 tf.global_variables_initializer() 保存了所有 tensorflow 变量。

import tensorflow as tf
sess = tf.Session()
//code blocks
run_init_op = tf.global_variables_intializer()
sess.run(run_init_op)
saver = tf.train.Saver()
#varible that makes prediction
prediction = tf.nn.softmax(tf.matmul(last,weight)+bias)
#feed the inputdata into model and trained
#saved the model
#save the tensorflow model
save_path= saver.save(sess,'/tmp/text_generate_trained_model.ckpt')
print("Model saved in the path : {}".format(save_path))

模型得到训练并保存其所有会话。链接查看整个代码lstm_rnn.py

现在我加载了存储的模型并尝试为文档生成文本。所以,我用下面的代码恢复了模型

tf.reset_default_graph()
imported_data = tf.train.import_meta_graph('text_generate_trained_model.ckpt.meta')
with tf.Session() as sess:
    imported_meta.restore(sess,tf.train.latest_checkpoint('./'))

    #accessing the default graph which we restored
    graph = tf.get_default_graph()

    #op that we can be processed to get the output
    #last is the tensor that is the prediction of the network
    y_pred = graph.get_tensor_by_name("prediction:0")
    #generate characters
    for i in range(500):
        x = np.reshape(pattern,(1,len(pattern),1))
        x = x / float(n_vocab)
        prediction = sess.run(y_pred,feed_dict=x)
        index = np.argmax(prediction)
        result = int_to_char[index]
        seq_in = [int_to_char[value] for value in pattern]
        sys.stdout.write(result)
        patter.append(index)
        pattern = pattern[1:len(pattern)]

    print("\n Done...!")
sess.close()

我知道预测变量在图中不存在。

KeyError: "名称 'prediction:0' 指的是一个不 存在。图中不存在“预测”操作。”

完整代码在这里text_generation.py

虽然我保存了所有张量流变量,但预测张量并未保存在张量流计算图中。我的 lstm_rnn.py 文件有什么问题。

谢谢!

【问题讨论】:

    标签: python tensorflow lstm


    【解决方案1】:

    为了使graph.get_tensor_by_name("prediction:0") 工作,您应该在创建它时命名它。可以这样命名

    prediction = tf.nn.softmax(tf.matmul(last,weight)+bias, name="prediction")
    

    如果您已经训练了模型并且无法重命名张量,您仍然可以使用默认名称获取该张量,如下所示,

    y_pred = graph.get_tensor_by_name("Reshape_1:0")
    

    如果Reshape_1 不是张量的实际名称,则必须查看图中的名称并弄清楚。 您可以使用

    进行检查
    for op in graph.get_operations():
        print(op.name)
    

    【讨论】:

    • 我有类似的问题,但是当我使用您的建议并将我的层命名为 prediction = tf.layers.dense(net, 8, name='prediction_output') 时,我得到的操作命名为 prediction_output/bias prediction_output/bias/read prediction_output/bias/Initializer/zeros prediction_output/bias/Assign prediction_output/kernel/Regularizer/l2_regularizer/scale 但没有任何名称 prediction_output,这就是我收到错误的原因KeyError: "The name 'prediction_output:0' refers to a Tensor which does not exist. The operation, 'prediction_output', does not exist in the graph." 我该如何解决这个问题?
    • 我的答案中的层是tf.nn.softmax,它没有偏差或权重等参数。另一方面,您正在使用具有这些参数的tf.layers.dense,因此您将拥有以您分配给图层的名称命名的偏差和权重。如果你希望你的输出在prediction_output,你可以不命名密集层,然后将其输出传递给具有该名称的标识函数。 prediction = tf.layers.dense(net, 8) final_output = tf.identity(prediction, name='prediction_output')
    猜你喜欢
    • 2018-05-03
    • 2021-10-11
    • 2016-11-17
    • 2017-12-01
    • 2019-05-30
    • 2021-07-03
    • 2018-05-21
    • 1970-01-01
    • 2019-03-18
    相关资源
    最近更新 更多