#NEWLINE# def top_k_op( predictions , targets , k = 1 ) : #NEWLINE# #INDENT# ' top_k_op.\n\n    An op that calculates top-k mean accuracy.\n\n    Examples:\n        ```python\n        input_data = placeholder(shape=[None, 784])\n        y_pred = my_network(input_data) # Apply some ops\n        y_true = placeholder(shape=[None, 10]) # Labels\n        top3_op = top_k_op(y_pred, y_true, 3)\n\n        # Calculate Top-3 accuracy by feeding data X and labels Y\n        top3_accuracy = sess.run(top3_op, feed_dict={input_data: X, y_true: Y})\n        ```\n\n    Arguments:\n        predictions: `Tensor`.\n        targets: `Tensor`.\n        k: `int`. Number of top elements to look at for computing precision.\n\n    Returns:\n        `Float`. The top-k mean accuracy.\n\n    ' #NEWLINE# with tf . name_scope ( ( 'Top_' + str ( k ) ) ) : #NEWLINE# #INDENT# targets = tf . cast ( targets , tf . int32 ) #NEWLINE# correct_pred = tf . nn . in_top_k ( predictions , tf . argmax ( correct_pred , 1 ) , k ) #NEWLINE# acc = tf . reduce_mean ( tf . cast ( correct_pred , tf . float32 ) ) #NEWLINE# #UNINDENT# return acc