Q:

how can I calculate the multi-label top k precisions with tensorflow?

My task is to predict the five most probable tags in a sentence. And now I've got unscaled logits from the output(dense connect) layer:
with tf.name_scope("output"):
    scores = tf.nn.xw_plus_b(self.h_drop, W,b, name="scores")
    predictions = tf.nn.top_k(self.scores, 5) # should be the k highest score
with tf.name_scope("accuracy"):
    labels = input_y  # its shape is (batch_size, num_classes)
    # calculate the top k accuracy

now predictions are just like [3,1,2,50,12] (3,1... are indexes of the highest scores), while labels are in "multi-hot" form: [0,1,0,1,1,0...].
 In python, i can simply write
correct_preds = [input_y[i]==1 for i in predictions]
weighted = np.dot(correct_preds, [5,4,3,2,1]) # weighted by rank 
recall  = sum(correct_preds) /sum(input_y)
precision =sum(correct_preds)/len(correct_preds)

but in tensorflow, what form shoud I use to complete this task?

A:

Solution
I've coded up an example of how to do the calculations.  All of the inputs in this example are coded as tf.constant but of course you can substitute your variables.
The main trick is the matrix multiplications.  First is input_y reshaped to be 2d times a [1x5] ones matrix called to_top5.  The second is correct_preds by the weighted_matrix.
Code
import tensorflow as tf

input_y = tf.constant( [5,2,9,1] , dtype=tf.int32 )

predictions = tf.constant( [[9,3,5,2,1],[8,9,0,6,5],[1,9,3,4,5],[1,2,3,4,5]])

to_top5       = tf.constant( [[1,1,1,1,1]] , dtype=tf.int32 )
input_y_for_top5 = tf.matmul( tf.reshape(input_y,[-1,1]) , to_top5 )

correct_preds = tf.cast( tf.equal( input_y_for_top5 , predictions ) , dtype=tf.float16 )

weighted_matrix = tf.constant( [[5.],[4.],[3.],[2.],[1.]] , dtype=tf.float16 )

weighted = tf.matmul(correct_preds,weighted_matrix)

recall = tf.reduce_sum(correct_preds) / tf.cast( tf.reduce_sum(input_y) , tf.float16)
precision = tf.reduce_sum(correct_preds) / tf.constant(5.0,dtype=tf.float16)

## training
# Run tensorflow and print the result
with tf.Session() as sess:
  print "\n\n=============\n\n"
  print "\ninput_y_for_top5"
  print sess.run(input_y_for_top5)
  print "\ncorrect_preds"
  print sess.run(correct_preds)
  print "\nweighted"
  print sess.run(weighted)
  print "\nrecall"
  print sess.run(recall)
  print "\nprecision"
  print sess.run(precision)
  print "\n\n=============\n\n"

Output
=============

input_y_for_top5
[[5 5 5 5 5]
 [2 2 2 2 2]
 [9 9 9 9 9]
 [1 1 1 1 1]]

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

weighted
[[ 3.]
 [ 0.]
 [ 4.]
 [ 5.]]

recall
0.17651

precision
0.6001

=============

Summary
The above examples shows a batch size of 4.
The first batch has a y_label of 5, which means that the element with an index of 5 is the correct label for the first batch.  Furthermore, the prediction for the first batch is [9,3,5,2,1] which means that the prediction function thinks that the 9th element is the most likely, then element 3 is the next most likely and so on.
Let's say we want an example of a batch size of 3, then use the following code
input_y = tf.constant( [5,2,9] , dtype=tf.int32 )
predictions = tf.constant( [[9,3,5,2,1],[8,9,0,6,5],[1,9,3,4,5]])

If we substitute in the above lines to the program we can see that indeed it calculates everything for a batch size of 3 correctly.

