KeyError:“名称'预测/准确度/平均值:0'指的是不存在的张量......”

时间:2017-11-20 03:04:37

标签: tensorflow mnist

我在ubuntu16.04上使用tensorflow1.4cpu版本。 我以前在mnist数据集上训练过一个convnet模型。

现在我想再次访问模型并预测mnist.test.images的准确性。我成功加载了模型:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
images = mnist.test.images[0:1000]
labels = mnist.test.labels[0:1000]
sess=tf.Session()

saver = tf.train.import_meta_graph('tradConv_mnistModel/tradConvMnist-
10000.meta')

saver.restore(sess,
tf.train.latest_checkpoint('./tradConv_mnistModel'))
graph = tf.get_default_graph()

在之前训练的图表中,我打印了准确度名称:

with tf.name_scope('predictions'):
  correct_prediction = tf.equal(
                       tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  with tf.name_scope('accuracy'):
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print(accuracy.name)
     "predictions/accuracy/Mean:0"

但是当我尝试用新加载的图表来评估准确性时,我得到以下错误:

tensor_name = "predictions/accuracy/Mean:0"
accuracy = graph.get_tensor_by_name(tensor_name)

print("test accuracy %g"%accuracy.eval(feed_dict={
       x: images, y_: labels, keep_prob: 1.0}))

KeyError: "The name 'predictions/accuracy/Mean:0' refers to a Tensor 
which does not exist. The operation, 'predictions/accuracy/Mean', does 
not exist in the graph."

我也在已保存的图表中打印了所有tensors_name,实际上tensor_name也没有退出。

所以我的问题是如何在重新加载后评估训练模型的测试数据的准确性?

1 个答案:

答案 0 :(得分:-1)

方法tf.train.Saver()。restore工作。但另一种方法    tf.train.import_meta_graph('tradConv_mnistModel / tradConvMnis t-10000.meta')不起作用!