TensorFlow 获得单个预测的准确性

TensorFlow get accuracy of a single prediction

我使用 TensorFlow 和 mnist 数据集实现了逻辑回归模型。我想出了如何使用以下代码获得学习算法的总准确度...

correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

这很好用,打印准确率为 91%。现在我正在恢复模型并将单个图像传递到模型中以进行预测。我传递了一张数字 7 的图片,mnist.test.images[0],它预测正确 -> [7]...

with tf.Session() as sess:
    saver.restore(sess, "/tmp/model.ckpt")
    x_in = np.expand_dims(mnist.test.images[0], axis=0)
    classification = sess.run(tf.argmax(pred, 1), feed_dict={x:x_in})
    print(classification)

现在我想获得与模型相关的此预测的准确性,但我不确定如何进行,我尝试了以下显然不起作用的方法...

 accuracy = tf.reduce_mean(tf.cast(classification, tf.float32))
 print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

这个输出是Accuracy: 7.0

如果无法给出直接的答案,我希望能采取一些步骤来实现我想要的结果。

单个预测的准确性没有多大意义。

您将获得 0% 或 100%。

但您仍然可以使用您在图表中创建的精度操作:

with tf.Session() as sess:
    saver.restore(sess, "/tmp/model.ckpt")
    x_in = np.expand_dims(mnist.test.images[0], axis=0)
    y_in = np.expand_dims(mnist.test.labels[0], axis=0)
    print("Accuracy:", accuracy.eval({x: x_in , y: y_in}))