如何打印预测图像的置信度

时间:2017-05-14 20:41:57

标签: python-3.x machine-learning tensorflow logistic-regression

我在 mnist 数据集上训练了一个逻辑回归模型,这些是重要的变量......

# tf Graph Input
x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes

# set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# construct model
logits = tf.matmul(x, W) + b
pred = tf.nn.softmax(logits)  # Softmax

# minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))

# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)

现在我做的是创建了一个名为adversarial的稍微改变过的图像数组,我将其反馈到模型中,以便进行预测。

如果我执行以下操作......

classification_adversarial = sess.run(tf.argmax(pred, 1), feed_dict={x:adversarial})
print(classification_adversarial)

>> [6, 6, 6, 6, 6, 6, 6, 6, 6, 6]

我得到了模型预测的输出。这是预期的输出,模型认为图像是6s。

无论如何,对于每个图像,我希望显示accuracy。因此,如果我提供一个图像,例如adversarial.reshape((1, 784)),我希望模型告诉我它的预测是多么准确,百分比。

我尝试实现以下内容以获得总体准确度......

# list of booleans to determine the correct predictions
correct_prediction = tf.equal(tf.argmax(pred, 1), np.array([6]*10))
print(correct_prediction.eval({x:adversarial}))

>> [True, True ... , True, True]

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy:", accuracy.eval({x: adversarial}))

>> Accuracy: 1.0

我的准确度为1.0。这是什么意思,我的模型100%准确?如果是这样的话,我一定是做错了。

1 个答案:

答案 0 :(得分:1)

  1. 要打印每张图片的置信度,您必须打印" pred"这是logits的softmax。

  2. 在您的情况下,仅对10张图像测量精度,并且在所有10种情况下模型都是正确的。因此,准确度为1.0

  3. 这有道理吗?如果您需要更多信息,请评论。