Created
July 26, 2018 23:09
-
-
Save lioutasb/d5b29142b2cc667bc5c84fc3373e2d75 to your computer and use it in GitHub Desktop.
Tensorflow implementation of Mean Reciprocal Rank (mrr) metric compatible with tf.Estimator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the runtime complexity of this?