Round up half-ranks
If an answer has rank 1.5, round it up instead of down.
@@ -330,7 +330,7 @@ class EntityRankingJob(EvaluationJob):
return s_ranks, o_ranks, scores_sp, scores_po
def _get_rank(self, scores, answers):
- """Returns the rank of each answer (mean rank on ties).
+ """Returns the rank of each answer (mean rank on ties, rounded up to next integer).
`scores` is batch_size x entities matrix of scores. `answers` is a vector (of
size batch_size) holding the index of the true answer in each row of `scores`.
@@ -341,8 +341,8 @@ class EntityRankingJob(EvaluationJob):
# answer score, then sum up to get ranks.
true_scores = scores[range(answers.size(0)), answers.long()]
ranks_greater = torch.sum((scores > true_scores.view(-1, 1)).long(), dim=1)
- # -1 for the true answer
- ranks_equal = torch.sum((scores == true_scores.view(-1, 1)).long(), dim=1) - 1
+ # each element of ranks_equal is at 1 (since the true answer is in scores)
+ ranks_equal = torch.sum((scores == true_scores.view(-1, 1)).long(), dim=1)
ranks = ranks_greater + ranks_equal // 2
return ranks
