usamazf
usamazf

Reputation: 3215

Get top-k predictions from tensorflow

I am relatively new in machine learning especially when it comes to implementing algorithms. I am using python and tensorflow library to implement a neural network to train on a dataset which has about 20 classes. I am able to train and get predictions successfully but I have a question,

Is it possible to get top k classes along with their probabilities using tensorflow instead of just a single prediction?

If it is possible how can this be done? Thanks for your guidance.

Update 01: I am adding code of what I am doing. So I build a neural network with 3 layers having tanh, sigmoid, & sigmoid respectively as activation functions for the hidden layers and softmax for output layer. The code for training and prediction is as follows:

y_pred = None
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(training_epochs):            
            # running the training_epoch numbered epoch
            _,cost = sess.run([optimizer,cost_function],feed_dict={X:tr_features,Y:tr_labels})
            cost_history = np.append(cost_history,cost)
        # predict results based on the trained model
        y_pred = sess.run(tf.argmax(y_,1),feed_dict={X: ts_features})

Right now y_pred is a list of class labels for each test example of ts_features. But instead of getting 1 single class label for each test example I am hoping to get top-k predictions for each example each of the k-predictions accompanied by some kind of probability.

Upvotes: 3

Views: 3573

Answers (1)

benjaminplanche
benjaminplanche

Reputation: 15119

Using tf.nn.top_k():

top_k_values, top_k_indices = tf.nn.top_k(predictions, k=k)

If predictions is a vector of probabilities per class (i.e. predictions[i] = prediction probability for class i), then top_k_values will contain the k highest probabilities in predictions, and top_k_indices will contain the indices of these probabilities, i.e. the corresponding classes.


Supposing that in your code, y_ is the vector of predicted probabilities per class:

k = 3  # replace with your value
# Instead of `y_pred`:
y_k_probs, y_k_pred = sess.run(
    tf.nn.top_k(y_, k=k), feed_dict={X: ts_features})

Upvotes: 9

Related Questions