【问题标题】:How to print the confidence level of a predicted image如何打印预测图像的置信度
【发布时间】:2017-10-13 14:19:04
【问题描述】:

我在 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% 准确的?如果是这样,我一定是做错了什么。

【问题讨论】:

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


    【解决方案1】:
    1. 要打印每个图像的置信度,您必须打印“pred”,它是 logits 的 softmax。

    2. 在您的情况下,仅测量 10 个图像的准确性,并且模型在所有 10 个情况下都是正确的。所以,准确率是 1.0

    这有意义吗?如果您需要更多信息,请发表评论。

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2018-04-22
      相关资源
      最近更新 更多