我使用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}))