title: Get top-k predictions from tensorflow
body: <p>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, </p>
  
  <blockquote>
    <p>Is it possible to get top k classes along with their probabilities using tensorflow instead of just a single prediction?</p>
  </blockquote>
  
  <p>If it is possible how can this be done? Thanks for your guidance.</p>
  
  <p><strong>Update 01:</strong>
  I am adding code of what I am doing. So I build a neural network with 3 layers having tanh, sigmoid, &amp; sigmoid respectively as activation functions for the hidden layers and softmax for output layer. The code for training and prediction is as follows:</p>
  
  <pre><code>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})
  </code></pre>
  
  <p>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.</p>