Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Parts of GAP metric for MultiLevel LTR #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tensorflow_ranking/python/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class RankingMetricKey(object):
# Ordered Pair Accuracy.
ORDERED_PAIR_ACCURACY = 'ordered_pair_accuracy'

# Global Average Precision (GAP)
GAP = 'gap'


def compute_mean(metric_key,
labels,
Expand Down Expand Up @@ -89,6 +92,7 @@ def compute_mean(metric_key,
RankingMetricKey.PRECISION: metrics_impl.PrecisionMetric(name, topn),
RankingMetricKey.MAP: metrics_impl.MeanAveragePrecisionMetric(name, topn),
RankingMetricKey.ORDERED_PAIR_ACCURACY: metrics_impl.OPAMetric(name),
RankingMetricKey.GAP: metrics_impl.GlobalAveragePrecisionMetric(name),
}
assert metric_key in metric_dict, ('metric_key %s not supported.' %
metric_key)
Expand Down Expand Up @@ -199,6 +203,15 @@ def _ordered_pair_accuracy_fn(labels, predictions, features):
return ordered_pair_accuracy(
labels, predictions, weights=_get_weights(features), name=name)

def _global_average_precision_fn(labels, predictions, features):
"""Returns mean average precision as the metric."""
return global_average_precision(
labels,
predictions,
weights=_get_weights(features),
topn=topn,
name=name)

metric_fn_dict = {
RankingMetricKey.ARP: _average_relevance_position_fn,
RankingMetricKey.MRR: _mean_reciprocal_rank_fn,
Expand All @@ -207,6 +220,7 @@ def _ordered_pair_accuracy_fn(labels, predictions, features):
RankingMetricKey.PRECISION: _precision_fn,
RankingMetricKey.MAP: _mean_average_precision_fn,
RankingMetricKey.ORDERED_PAIR_ACCURACY: _ordered_pair_accuracy_fn,
RankingMetricKey.GAP: _global_average_precision_fn,
}
assert metric_key in metric_fn_dict, ('metric_key %s not supported.' %
metric_key)
Expand Down Expand Up @@ -415,6 +429,37 @@ def ordered_pair_accuracy(labels, predictions, weights=None, name=None):
correct_pairs, pair_weights = metric.compute(labels, predictions, weights)
return tf.compat.v1.metrics.mean(correct_pairs, pair_weights)

def global_average_precision(labels,
predictions,
weights=None,
topn=None,
name=None):
"""Computes global average precision (MAP).

The implementation of GAP is based on Equation (1.7) in the following:
Kaggle Evaluation Metric page, found at
https://www.kaggle.com/c/youtube8m/overview/evaluation

Args:
labels: A `Tensor` of the same shape as `predictions`. A value >= 1 means a
relevant example.
predictions: A `Tensor` with shape [batch_size, list_size]. Each value is
the ranking score of the corresponding example.
weights: A `Tensor` of the same shape of predictions or [batch_size, 1]. The
former case is per-example and the latter case is per-list.
topn: A cutoff for how many examples to consider for this metric.
name: A string used as the name for this metric.

Returns:
A metric for the global average precision.
"""
metric = metrics_impl.GlobalAveragePrecisionMetric(name, topn)
with tf.compat.v1.name_scope(metric.name, 'global_average_precision',
(labels, predictions, weights)):
per_list_map, per_list_weights = metric.compute(labels, predictions,
weights)
return tf.compat.v1.metrics.mean(per_list_map, per_list_weights)


def eval_metric(metric_fn, **kwargs):
"""A stand-alone method to evaluate metrics on ranked results.
Expand Down
14 changes: 14 additions & 0 deletions tensorflow_ranking/python/metrics_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,17 @@ def compute(self, labels, predictions, weights):
weights, 2) * tf.cast(
valid_pair, dtype=tf.float32)
return correct_pairs, pair_weights

class GlobalAveragePrecisionMetric(_RankingMetric):
"""Implements Global Average Precesion (GAP)."""
def __init__(self, name):
"""Constructor."""
self._name = name

@property
def name(self):
"""The metric name."""
return self._name

def compute(self, labels, predictions, weights):
pass
19 changes: 19 additions & 0 deletions tensorflow_ranking/python/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,25 @@ def test_compute_mean(self):
key, labels, scores, weights, 2, name=key))
self.assertGreater(value, 0.)

def test_global_average_precision(self):
with tf.Graph().as_default():
scores = [[1., 3., 2.], [1., 2., 3.]]
# Note that scores are ranked in descending order, so the ranks are
# [[3, 1, 2], [3, 2, 1]]
labels = [[0., 0., 1.], [0., 1., 2.]]
rels = [[0, 0, 1], [0, 1, 1]]
m = metrics_lib.global_average_precision
# self._check_metrics([
# (m([labels[0]], [scores[0]]), _ap(rels[0], scores[0])),
# (m([labels[0]], [scores[0]], topn=1), _ap(rels[0], scores[0],
# topn=1)),
# (m([labels[0]], [scores[0]], topn=2), _ap(rels[0], scores[0],
# topn=2)),
# (m(labels,
# scores), sum(_ap(rels[i], scores[i]) for i in range(2)) / 2.),
# (m(labels, scores, topn=1),
# sum(_ap(rels[i], scores[i], topn=1) for i in range(2)) / 2.),
# ])

if __name__ == '__main__':
tf.compat.v1.enable_v2_behavior()
Expand Down