コード例 #1
0
ファイル: test_ndcg.py プロジェクト: rongyinet/catalyst
def test_sample_ndcg():
    """
    Tests for catalyst.ndcg metric.
    """
    y_pred = [0.5, 0.2, 0.1]
    y_true = [1.0, 0.0, 1.0]

    outputs = torch.Tensor([y_pred])
    targets = torch.Tensor([y_true])

    true_ndcg_at2 = 1.0 / (1.0 + 1 / math.log2(3))
    comp_ndcg_at2 = ndcg(outputs, targets, topk=[2])[0]

    assert np.isclose(true_ndcg_at2, comp_ndcg_at2)

    y_pred1 = [0.5, 0.2, 0.1]
    y_pred2 = [0.5, 0.2, 0.1]
    y_true1 = [1.0, 0.0, 1.0]
    y_true2 = [1.0, 0.0, 1.0]

    outputs = torch.Tensor([y_pred1, y_pred2])
    targets = torch.Tensor([y_true1, y_true2])

    true_ndcg_at2 = 1.0 / (1.0 + 1 / math.log2(3))
    comp_ndcg_at2 = ndcg(outputs, targets, topk=[2])[0]

    assert np.isclose(true_ndcg_at2, comp_ndcg_at2)
コード例 #2
0
ファイル: test_ndcg.py プロジェクト: rongyinet/catalyst
def test_zero_ndcg():
    """
    Tests for catalyst.ndcg metric.
    """
    y_pred = [6, 5, 4, 3, 2, 1, 0]
    y_true = [0, 0, 0, 0, 0, 0, 1]

    ndcg_at1, ndcg_at3, ndcg_at7 = ndcg(torch.tensor([y_pred]),
                                        torch.tensor([y_true]),
                                        topk=[1, 3, 7])

    assert torch.isclose(ndcg_at1, torch.tensor(0.0))
    assert torch.isclose(ndcg_at3, torch.tensor(0.0))
    assert torch.isclose(ndcg_at7, torch.tensor(0.3), atol=0.05)
コード例 #3
0
ファイル: _ndcg.py プロジェクト: rongyinet/catalyst
    def update(self, logits: torch.Tensor, targets: torch.Tensor) -> List[float]:
        """
        Update metric value with ndcg for new data and return intermediate metrics values.

        Args:
            logits (torch.Tensor): tensor of logits
            targets (torch.Tensor): tensor of targets

        Returns:
            list of ndcg@k values
        """
        values = ndcg(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