在TensorFlow的MNIST教程中,最后一步是使用以下代码输出模型的测试准确率:
# Test trained modelcorrect_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
然而,我想知道如何修改这段代码,以便输出测试集的预测值(标签),而不仅仅是打印准确率?
这里是教程的链接: https://www.tensorflow.org/tutorials/mnist/beginners/
回答:
类似这样的代码应该可以工作
print(sess.run(tf.argmax(y, 1), feed_dict={x: mnist.test.images}))
因为教程中的y
是一个张量,其中索引为j
的列描述了第i
行的图像是数字j
的可能性,所以tf.argmax
只是返回每一行中概率最高的列的索引。
PS 抱歉我的英语不好