Skip to content

Instantly share code, notes, and snippets.

@lioutasb
Created July 26, 2018 23:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lioutasb/d5b29142b2cc667bc5c84fc3373e2d75 to your computer and use it in GitHub Desktop.
Save lioutasb/d5b29142b2cc667bc5c84fc3373e2d75 to your computer and use it in GitHub Desktop.
Tensorflow implementation of Mean Reciprocal Rank (mrr) metric compatible with tf.Estimator
import tensorflow as tf
def mrr_metric(labels, predictions, weights=None,
metrics_collections=None,
updates_collections=None,
name=None):
with tf.name_scope(name, 'mrr_metric', [predictions, labels, weights]) as scope:
k = predictions.get_shape().as_list()[-1]
_, r = tf.nn.top_k(predictions, k)
get_ranked_indicies = tf.expand_dims(tf.where(tf.equal(tf.cast(r,tf.int64),labels))[:,1],1)
rr = 1/(get_ranked_indicies+1)
m_rr, update_mrr_op = tf.metrics.mean(rr, weights=weights, name=name)
if metrics_collections:
tf.add_to_collection(metrics_collections, m_rr)
if updates_collections:
tf.add_to_collections(updates_collections, update_mrr_op)
return m_rr, update_mrr_op
@eggie5
Copy link

eggie5 commented Oct 15, 2018

What's the runtime complexity of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment