How to get predicted class labels in TensorFlow's MNIST example?

£可爱£侵袭症+ 提交于 2019-11-29 23:25:13

问题


I am new to Neural Networks and went through the MNIST example for beginners.

I am currently trying to use this example on another dataset from Kaggle that does not have test labels.

If I run the model on the test data set without corresponding labels and therefore unable to compute the accuracy like in the MNIST example, I would like to be able to see the predictions. Is it possible to access observations and their predicted labels somehow and print them out nicely?


回答1:


I think you just need to evaluate your output-tensor as stated in the tutorial:

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

To get the output of a tensor see the docs:

After the graph has been launched in a session, the value of the Tensor can be computed by passing it to Session.run(). t.eval() is a shortcut for calling tf.get_default_session().run(t).

If you want to get predictions rather than accuracy, you need to evaluate your ouput tensor y in the same way:

print(sess.run(y, feed_dict={x: mnist.test.images}))



回答2:


prediction=tf.argmax(y,1)
print prediction.eval(feed_dict={x: mnist.test.images}).

For more details, check out https://github.com/tensorflow/tensorflow/issues/97



来源:https://stackoverflow.com/questions/34060332/how-to-get-predicted-class-labels-in-tensorflows-mnist-example

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