Generate a Prediction List

Viewed 242

With Tensorflow, is there a go to way to output the networks predictions?

My Outputs have been using a One Hot Representation for 12 classes

[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0]
etc...

When trying to get predictions out of my model for a given input, I ran the following code

    prediction=tf.argmax(y,1)
    best = sess.run([prediction],feed_dict={x: batch_x, y: batch_y,
                                  seqlen: batch_seqlen})
    print("Prediction: ")
    print(best)

My output when I ran this code and printed the prediction was:

[array([1, 5, 7, 7, 7, 4, 7, 9, 4, 4, 9, 6, 7, 8, 3, 2], dtype=int64)]

my batch size that I input was 16, so it did make sense to have 16 outputs. However, none of these are One Hot representation (Not sure if tensorflow's output is intended to be interperated as index's, so the 1 would actually be some form of onehot

Is there a way for each particular X to create a ranked list of predictions, what the model finds the most likely given that X?

Does this make sense?

1 Answers

You're taking the tf.argmax of 1-hot vectors, so that's why you're seeing indices instead of the 1-hot vectors you expected.

To get a sorted list of class predictions you could take your prediction vector and apply values, indices = tf.nn.top_k(prediction) the values will be your predictions sorted in DESCENDING order and indices will be those sorted values' indices.

Related