-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.py
60 lines (46 loc) · 1.96 KB
/
metrics.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
# Standard entropy loss
def compute_entropy(log_probs):
return torch.sum(-torch.exp(log_probs) * log_probs, dim=1)
# Entropy for Dirichlet output
def compute_total_entropy(log_alphas):
log_probs = log_alphas - torch.logsumexp(log_alphas, 1, keepdim=True)
return compute_entropy(log_probs)
# Max Probability for Dirichlet output
def compute_max_prob(log_alphas):
log_probs = log_alphas - torch.logsumexp(log_alphas, 1, keepdim=True)
log_confidence, _ = torch.max(log_probs, 1)
return torch.exp(log_confidence)
# Differential entropy for Dirichlet output
def compute_differential_entropy(log_alphas):
alphas = torch.exp(log_alphas)
alpha0 = torch.exp(torch.logsumexp(log_alphas, 1))
loss = torch.sum(torch.lgamma(alphas), 1) - torch.lgamma(alpha0) - torch.sum(
(alphas - 1) * (torch.digamma(alphas) - torch.digamma(alpha0).unsqueeze(-1)), 1)
return loss
# Mutual Information for Dirichlet output
def compute_mutual_information(log_alphas):
alphas = torch.exp(log_alphas)
log_alpha0 = torch.logsumexp(log_alphas, 1)
alpha0 = torch.exp(log_alpha0)
log_probs = log_alphas - log_alpha0.unsqueeze(-1)
loss = -torch.sum(torch.exp(log_probs) * (log_probs -
torch.digamma(alphas + 1) +
torch.digamma(alpha0 + 1).unsqueeze(-1)),
1)
return loss
# Precision for Dirichlet output
def compute_precision(log_alphas):
log_alpha0 = torch.logsumexp(log_alphas, 1)
return torch.exp(log_alpha0)
# Data Uncertainty for Dirichlet output
def compute_data_uncertainty(log_alphas):
log_alpha0 = torch.logsumexp(log_alphas, 1)
log_probs = log_alphas - log_alpha0.unsqueeze(-1)
alphas = torch.exp(log_alphas)
alpha0 = torch.exp(log_alpha0)
loss = - torch.sum(
log_probs * (torch.digamma(alphas + 1) -
torch.digamma(alpha0 + 1).unsqueeze(-1)),
1)
return loss