Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python KeyError:张量变量,指不存在的张量_Python_Tensorflow_Lstm - Fatal编程技术网

Python KeyError:张量变量,指不存在的张量

Python KeyError:张量变量,指不存在的张量,python,tensorflow,lstm,Python,Tensorflow,Lstm,使用LSTMCell,我训练了一个模型来生成文本。我启动了tensorflow会话,并使用tf.global\u variables\u 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(

使用LSTMCell,我训练了一个模型来生成文本。我启动了tensorflow会话,并使用tf.global\u variables\u 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))
模型得到训练并保存其所有会话。链接以查看整个代码

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

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'引用的张量不是 存在。图形中不存在“预测”操作。“

这里有完整的代码

虽然我保存了所有tensorflow变量,但预测张量并没有保存在tensorflow计算图中。我的lstm\n.py文件有什么问题


谢谢

用于
图形。通过名称(“预测:0”)获取张量来工作,您应该在创建它时将其命名。你可以这样命名

prediction = tf.nn.softmax(tf.matmul(last,weight)+bias, name="prediction")
如果您已经训练了模型,并且无法重命名张量,您仍然可以使用其默认名称获取该张量,如中所示

y_pred = graph.get_tensor_by_name("Reshape_1:0")
如果
reformate_1
不是张量的实际名称,则必须查看图表中的名称并找出它。 你可以用电脑检查一下

for op in graph.get_operations():
    print(op.name)

我也有类似的问题,但当我使用您的建议并将我的层命名为
prediction=tf.layers.dense(net,8,name='prediction\u output')时
我得到的操作名为
预测\输出/偏差预测\输出/偏差/读取预测\输出/偏差/初始值设定项/零预测\输出/偏差/分配预测\输出/内核/正则化器/l2 \正则化器/缩放
,但没有任何名称
预测\输出
,这就是我得到错误的原因
KeyError:“名称'prediction_output:0'指的是不存在的张量。图形中不存在“预测输出”操作。“
我如何解决这个问题?我的答案中的图层是
tf.nn.softmax
,它没有像偏差或权重这样的参数。另一方面,您使用的是具有这些参数的
tf.layers.dense
,因此您将以指定给层的名称命名偏差和权重。如果希望在
预测\u输出中输出,可以不命名稠密层,然后将其输出传递给具有该名称的标识函数
prediction=tf.layers.densite(净,8)final_output=tf.identity(prediction,name='prediction'u output')