How to get the value from each output-node during eval MNIST testdata in TensorFlow?

。_饼干妹妹 提交于 2019-12-24 07:07:12

问题


I train a convolutional neural network (CNN) with TensorFlow. When the training is finished I calculate the accuracy with the following code:

...
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
eval_batch_size = 1
good = 0
total = 0
for i in range(int(mnist.test.num_examples/eval_batch_size)):
    testSet = mnist.test.next_batch(eval_batch_size, shuffle=False)
    good += accuracy.eval(feed_dict={ x: testSet[0], y: testSet[1]})
    total += testSet[0].shape[0]
accuracy_eval = good/total

For “good” I get the value 1.0 when the test image is correct detected and the value 0.0 if not.

I want get the values for all ten output-nodes. For example, I evaluate a test-image with a handwritten “8” so maybe the output-node for the number “8” is 0.6 and for the number “3” is 0.3 and for “5” is 0.05 and the last 0.05 spread out over the seven other output-nodes.

So how I get all this ten values for each test image in TensorFlow?


回答1:


You can do that by adding the following line:

pred=prediction.eval(feed_dict={ x: testSet[0], y: testSet[1]})

right after

testSet = mnist.test.next_batch(eval_batch_size, shuffle=False)

Then pred will be an array that contains 1 probability vector, and this is the vector you are interested in.



来源:https://stackoverflow.com/questions/44892253/how-to-get-the-value-from-each-output-node-during-eval-mnist-testdata-in-tensorf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!