def update(self, logits: torch.Tensor, targets: torch.Tensor) -> List[float]: """ Update metric value with map for new data and return intermediate metrics values. Args: logits (torch.Tensor): tensor of logits targets (torch.Tensor): tensor of targets Returns: list of map@k values """ values = mrr(logits, targets, topk=self.topk_args) values = [v.item() for v in values] for value, metric in zip(values, self.additive_metrics): metric.update(value, len(targets)) return values
def test_mrr(): """ Test mrr """ y_pred1 = [4.0, 2.0, 3.0, 1.0] y_pred2 = [1.0, 2.0, 3.0, 4.0] y_true1 = [0, 0, 1.0, 1.0] y_true2 = [0, 0, 1.0, 1.0] k_list = [1, 3] y_pred_torch = torch.Tensor([y_pred1, y_pred2]) y_true_torch = torch.Tensor([y_true1, y_true2]) mrr_results = mrr(y_pred_torch, y_true_torch, k_list) mrr_at1 = mrr_results[0] mrr_at3 = mrr_results[1] assert mrr_at1 == 0.5 assert mrr_at3 == 0.75