【发布时间】:2018-05-03 05:29:59
【问题描述】:
我在我的 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."
我还打印了保存图表中的所有 tensor_name 并且确实该 tensor_name 没有退出。
所以我的问题是如何在重新加载后评估训练模型和测试数据的准确性?
【问题讨论】:
-
猜测:saver/meta-graph 是否在添加准确率操作之前保存?创建 MetaGraph 时,您能否检查该操作是否存在于具有该名称的图中?
-
您好,我已经解决了这个问题。精度在保存器之前定义并保存在图表中,但它不存在于图元文件中。我不知道graph和methgraph之间的区别。我也不知道如何检查名称是否是在元图中创建的。经过几次尝试,我发现这种方法 tf.train.import_meta_graph('tradConv_mnistModel/tradConvMnist- 10000.meta') 根本不起作用。但是 tf.train.Saver().restore 有效!并使用它解决了这个问题。
标签: tensorflow mnist