Search code examples
machine-learningtensorflowmnist

Apply one input to tensorflow session and get output


I am running the sample MNIST handwritten numbers tutorial.

How can I activate the session with the first test MNIST image and hopefully get the expected output?

When I do this:

print(sess.run(tf.convert_to_tensor(mnist.test.images[0])))

I get a 784-long tensor with weights(guessing):

...
...
...
0.94901967  0.99607849  0.99607849  0.20392159  0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.
0.47450984  0.99607849  0.99607849  0.8588236   0.15686275  0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.47450984  0.99607849  0.81176478  0.07058824  0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
0.          0.          0.          0.          0.          0.          0.
...
...
...

My expected output would be 7 in this format:

[ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]

Solution

  • Well, the images (mnist.train.images) are 784 = 28 x 28 dimensional vectors. What you are looking for are labels (mnist.test.labels), which are indeed 10-dimensional:

    In [29]: mnist.test.labels[0]
    Out[29]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.])
    

    By the way, these arrays are just regular numpy arrays, you don't need to use tf session to print them.