1

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?

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
Milan
  • 256
  • 2
  • 17

1 Answers1

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.

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
  • Thank you @MiriamFarbe! That is what I was looking for. I am little confused about the values in this probability vector, I expected numbers between 0.0 and 1.0. But for example I get this: “-45049.1328125 6181.90087891 -26765.70117188 -40108.9609375 44725.2578125 60644.22265625 72028.3515625 -38615.09375 23696.48046875 -9353.94628906”. Do you have some keywords or links I can look up to understand this? – Milan Jul 03 '17 at 19:18
  • 1
    @Freundlicher it sounds that you did not apply softmax (https://en.wikipedia.org/wiki/Softmax_function) on your output. softmax function transforms a vector into probability vector. By the definition of softmax, both argmax(prediction, 1) and argmax(softmax(prediction), 1) will be equal. Yet, the latter one is easier to interpret, and often used in the evaluation of the loss function. – Miriam Farber Jul 03 '17 at 19:23
  • @Freundlicher in addition to my previous comment, you could see here that softmax is applied internally in the model: https://www.tensorflow.org/get_started/mnist/pros#predicted_class_and_loss_function, while here https://www.tensorflow.org/get_started/mnist/beginners#implementing_the_regression it is applied explicitly (in the row y=...) – Miriam Farber Jul 03 '17 at 19:29
  • and I have to apply it explicitly? Because at the moment I use at the beginning of training: prediction = convolutional_neural_network(x) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits = prediction, labels = y) ) – Milan Jul 03 '17 at 19:47
  • You don't have to, as the tf.nn.softmax_cross_entropy_with_logits does that internally. Yet if you want to look at the propability vector yourself you'll need to do that. Here is a toy example: import tensorflow as tf a=tf.nn.softmax([1.0,2.0]) sess=tf.InteractiveSession() a.eval() – Miriam Farber Jul 03 '17 at 19:55
  • Now I get this: [[ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]] ... should I open a new Question and link it here? – Milan Jul 03 '17 at 20:09
  • @Freundlicher yes it would be easier this way to see what happens. – Miriam Farber Jul 03 '17 at 20:21
  • i try the something but dont work, so I open a new question here: https://stackoverflow.com/questions/44893846/how-to-apply-softmax-on-an-array-vector-with-huge-positive-and-negative-values-i – Milan Jul 03 '17 at 21:05