我在mnist数据集上训练了一个逻辑回归模型,这些是重要的变量…
# tf Graph Inputx = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes# set model weightsW = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))# construct modellogits = tf.matmul(x, W) + bpred = tf.nn.softmax(logits) # Softmax# minimize error using cross entropycost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))# Gradient Descentoptimizer = 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]
我得到了模型预测的输出。这是预期的输出,模型认为这些图像是6。
无论如何,对于这些图像中的每一个,我都希望显示一个accuracy
。所以如果我输入一个图像,例如adversarial.reshape((1, 784))
,我希望模型告诉我它的预测在百分比上有多准确。
我尝试实现类似下面的代码来获取总体准确率…
# list of booleans to determine the correct predictionscorrect_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%准确的吗?如果真是这样,我一定是做错了什么。
回答:
-
要打印每张图像的置信度,你需要打印“pred”,它是logits的softmax。
-
在你的案例中,准确率仅针对10张图像进行测量,并且模型在所有10个案例中都是正确的。因此,准确率为1.0
这有意义吗?如果你需要更多信息,请评论。