Ejemplo n.º 1
0
    def test_compute_rank_from_scores(self):
        """Test the _compute_rank_from_scores() function."""
        batch_size = 3
        all_scores = torch.tensor([
            [2., 2., 1., 3., 5.],
            [1., 1., 3., 4., 0.],
            [1., 1., 3., float('nan'), 0],
        ])
        # true_score: (2, 3, 3)
        true_score = torch.tensor([2., 3., 3.]).view(batch_size, 1)
        exp_best_rank = torch.tensor([3., 2., 1.])
        exp_worst_rank = torch.tensor([4., 2., 1.])
        exp_avg_rank = 0.5 * (exp_best_rank + exp_worst_rank)
        exp_adj_rank = exp_avg_rank / torch.tensor([(5 + 1) / 2, (5 + 1) / 2,
                                                    (4 + 1) / 2])
        ranks = compute_rank_from_scores(true_score=true_score,
                                         all_scores=all_scores)

        best_rank = ranks.get('best')
        assert best_rank.shape == (batch_size, )
        assert (best_rank == exp_best_rank).all()

        worst_rank = ranks.get('worst')
        assert worst_rank.shape == (batch_size, )
        assert (worst_rank == exp_worst_rank).all()

        avg_rank = ranks.get('avg')
        assert avg_rank.shape == (batch_size, )
        assert (avg_rank == exp_avg_rank).all(), (avg_rank, exp_avg_rank)

        adj_rank = ranks.get('adj')
        assert adj_rank.shape == (batch_size, )
        assert (adj_rank == exp_adj_rank).all(), (adj_rank, exp_adj_rank)
Ejemplo n.º 2
0
    def test_compute_rank_from_scores(self):
        """Test the _compute_rank_from_scores() function."""
        batch_size = 3
        all_scores = torch.tensor([
            [2., 2., 1., 3., 5.],
            [1., 1., 3., 4., 0.],
            [1., 1., 3., float('nan'), 0],
        ])
        # true_score: (2, 3, 3)
        true_score = torch.as_tensor([2., 3., 3.]).view(batch_size, 1)
        exp_best_rank = torch.as_tensor([3., 2., 1.])
        exp_worst_rank = torch.as_tensor([4., 2., 1.])
        exp_avg_rank = 0.5 * (exp_best_rank + exp_worst_rank)
        exp_exp_rank = torch.as_tensor([(5 + 1) / 2, (5 + 1) / 2, (4 + 1) / 2])
        ranks = compute_rank_from_scores(true_score=true_score,
                                         all_scores=all_scores)

        optimistic_rank = ranks.get(RANK_OPTIMISTIC)
        assert optimistic_rank.shape == (batch_size, )
        assert (optimistic_rank == exp_best_rank).all()

        pessimistic_rank = ranks.get(RANK_PESSIMISTIC)
        assert pessimistic_rank.shape == (batch_size, )
        assert (pessimistic_rank == exp_worst_rank).all()

        realistic_rank = ranks.get(RANK_REALISTIC)
        assert realistic_rank.shape == (batch_size, )
        assert (realistic_rank == exp_avg_rank).all(), (realistic_rank,
                                                        exp_avg_rank)

        expected_realistic_rank = ranks.get(RANK_EXPECTED_REALISTIC)
        assert expected_realistic_rank is not None
        assert expected_realistic_rank.shape == (batch_size, )
        assert (expected_realistic_rank == exp_exp_rank).all(), (
            expected_realistic_rank, exp_exp_rank)