File size: 1,409 Bytes
c0ec7e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import torch
from torch import Tensor
from torchmetrics.retrieval.base import RetrievalMetric
from torchmetrics.utilities.checks import _check_retrieval_functional_inputs

from deepscreen.models.metrics.rie import calc_rie


class BEDROC(RetrievalMetric):
    is_differentiable: bool = False
    higher_is_better: bool = True
    full_state_update: bool = False

    def __init__(
            self,
            alpha: float = 80.5,
    ):
        super().__init__()
        self.alpha = alpha

    def _metric(self, preds: Tensor, target: Tensor) -> Tensor:
        preds, target = _check_retrieval_functional_inputs(preds, target)

        n_total = target.size(0)
        n_actives = target.sum()

        if n_actives == 0:
            return torch.tensor(0.0, device=preds.device)
        elif n_actives == n_total:
            return torch.tensor(1.0, device=preds.device)

        r_a = n_actives / n_total
        exp_a = torch.exp(torch.tensor(self.alpha))

        idx = torch.argsort(preds, descending=True, stable=True)
        active_ranks = torch.take(target, idx).nonzero() + 1

        rie = calc_rie(n_total, active_ranks, r_a, exp_a)
        rie_min = (1 - exp_a ** r_a) / (r_a * (1 - exp_a))
        rie_max = (1 - exp_a ** (-r_a)) / (r_a * (1 - exp_a ** (-1)))

        return (rie - rie_min) / (rie_max - rie_min)

    def plot(self, val=None, ax=None):
        return self._plot(val, ax)