-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
42 lines (33 loc) · 1.11 KB
/
utils.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
import torch
def pairwise_distances(x):
bn = x.shape[0]
x = x.view(bn, -1)
instances_norm = torch.sum(x ** 2, -1).reshape((-1, 1))
return -2 * torch.mm(x, x.t()) + instances_norm + instances_norm.t()
def calculate_gram_mat(x, sigma):
dist = pairwise_distances(x)
return torch.exp(-dist / sigma)
def reyi_entropy(x, sigma):
alpha = 1.01
k = calculate_gram_mat(x, sigma)
k = k / torch.trace(k)
eigv = torch.abs(torch.symeig(k, eigenvectors=True)[0])
eig_pow = eigv ** alpha
entropy = (1 / (1 - alpha)) * torch.log2(torch.sum(eig_pow))
return entropy
def joint_entropy(x, y, s_x, s_y):
alpha = 1.01
x = calculate_gram_mat(x, s_x)
y = calculate_gram_mat(y, s_y)
k = torch.mul(x, y)
k = k / torch.trace(k)
eigv = torch.abs(torch.symeig(k, eigenvectors=True)[0])
eig_pow = eigv ** alpha
entropy = (1 / (1 - alpha)) * torch.log2(torch.sum(eig_pow))
return entropy
def calculate_MI(x, y, s_x, s_y):
Hx = reyi_entropy(x, sigma=s_x)
Hy = reyi_entropy(y, sigma=s_y)
Hxy = joint_entropy(x, y, s_x, s_y)
Ixy = Hx + Hy - Hxy
return Ixy