-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsic.py
More file actions
171 lines (148 loc) · 4.87 KB
/
hsic.py
File metadata and controls
171 lines (148 loc) · 4.87 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# code from https://github.com/choasma/HSIC-bottleneck
import torch
import numpy as np
from torch.autograd import Variable, grad
def sigma_estimation(X, Y):
""" sigma from median distance
"""
D = distmat(torch.cat([X,Y]))
D = D.detach().cpu().numpy()
Itri = np.tril_indices(D.shape[0], -1)
Tri = D[Itri]
med = np.median(Tri)
if med <= 0:
med=np.mean(Tri)
if med<1E-2:
med=1E-2
return med
def distmat(X):
""" distance matrix
"""
r = torch.sum(X*X, 1)
r = r.view([-1, 1])
a = torch.mm(X, torch.transpose(X,0,1))
D = r.expand_as(a) - 2*a + torch.transpose(r,0,1).expand_as(a)
D = torch.abs(D)
return D
def kernelmat(X, sigma, k_type="gaussian"):
""" kernel matrix baker
"""
m = int(X.size()[0])
dim = int(X.size()[1]) * 1.0
H = torch.eye(m) - (1./m) * torch.ones([m,m])
if k_type == "gaussian":
Dxx = distmat(X)
if sigma:
variance = 2.*sigma*sigma*X.size()[1]
Kx = torch.exp( -Dxx / variance).type(torch.FloatTensor) # kernel matrices
# print(sigma, torch.mean(Kx), torch.max(Kx), torch.min(Kx))
else:
try:
sx = sigma_estimation(X,X)
Kx = torch.exp( -Dxx / (2.*sx*sx)).type(torch.FloatTensor)
except RuntimeError as e:
raise RuntimeError("Unstable sigma {} with maximum/minimum input ({},{})".format(
sx, torch.max(X), torch.min(X)))
## Adding linear kernel
elif k_type == "linear":
Kx = torch.mm(X, X.T).type(torch.FloatTensor)
Kxc = torch.mm(Kx,H)
return Kxc
def distcorr(X, sigma=1.0):
X = distmat(X)
X = torch.exp( -X / (2.*sigma*sigma))
return torch.mean(X)
def compute_kernel(x, y):
x_size = x.size(0)
y_size = y.size(0)
dim = x.size(1)
x = x.unsqueeze(1) # (x_size, 1, dim)
y = y.unsqueeze(0) # (1, y_size, dim)
tiled_x = x.expand(x_size, y_size, dim)
tiled_y = y.expand(x_size, y_size, dim)
kernel_input = (tiled_x - tiled_y).pow(2).mean(2)/float(dim)
return torch.exp(-kernel_input) # (x_size, y_size)
def mmd(x, y, sigma=None, use_cuda=True, to_numpy=False):
m = int(x.size()[0])
H = torch.eye(m) - (1./m) * torch.ones([m,m])
# H = Variable(H)
Dxx = distmat(x)
Dyy = distmat(y)
if sigma:
Kx = torch.exp( -Dxx / (2.*sigma*sigma)) # kernel matrices
Ky = torch.exp( -Dyy / (2.*sigma*sigma))
sxy = sigma
else:
sx = sigma_estimation(x,x)
sy = sigma_estimation(y,y)
sxy = sigma_estimation(x,y)
Kx = torch.exp( -Dxx / (2.*sx*sx))
Ky = torch.exp( -Dyy / (2.*sy*sy))
# Kxc = torch.mm(Kx,H) # centered kernel matrices
# Kyc = torch.mm(Ky,H)
Dxy = distmat(torch.cat([x,y]))
Dxy = Dxy[:x.size()[0], x.size()[0]:]
Kxy = torch.exp( -Dxy / (1.*sxy*sxy))
mmdval = torch.mean(Kx) + torch.mean(Ky) - 2*torch.mean(Kxy)
return mmdval
def mmd_pxpy_pxy(x,y,sigma=None,use_cuda=True, to_numpy=False):
"""
"""
if use_cuda:
x = x.cuda()
y = y.cuda()
m = int(x.size()[0])
Dxx = distmat(x)
Dyy = distmat(y)
if sigma:
Kx = torch.exp( -Dxx / (2.*sigma*sigma)) # kernel matrices
Ky = torch.exp( -Dyy / (2.*sigma*sigma))
else:
sx = sigma_estimation(x,x)
sy = sigma_estimation(y,y)
sxy = sigma_estimation(x,y)
Kx = torch.exp( -Dxx / (2.*sx*sx))
Ky = torch.exp( -Dyy / (2.*sy*sy))
A = torch.mean(Kx*Ky)
B = torch.mean(torch.mean(Kx,dim=0)*torch.mean(Ky, dim=0))
C = torch.mean(Kx)*torch.mean(Ky)
mmd_pxpy_pxy_val = A - 2*B + C
return mmd_pxpy_pxy_val
def hsic_regular(x, y, sigma=None, use_cuda=True, to_numpy=False):
"""
"""
Kxc = kernelmat(x, sigma)
Kyc = kernelmat(y, sigma)
KtK = torch.mul(Kxc, Kyc.t())
Pxy = torch.mean(KtK)
return Pxy
def hsic_normalized(x, y, sigma=None, use_cuda=True, to_numpy=True):
"""
"""
m = int(x.size()[0])
Pxy = hsic_regular(x, y, sigma, use_cuda)
Px = torch.sqrt(hsic_regular(x, x, sigma, use_cuda))
Py = torch.sqrt(hsic_regular(y, y, sigma, use_cuda))
thehsic = Pxy/(Px*Py)
return thehsic
def hsic_normalized_cca(x, y, sigma, use_cuda=True, to_numpy=True, k_type_y='gaussian'):
"""
"""
m = int(x.size()[0])
Kxc = kernelmat(x, sigma=sigma)
Kyc = kernelmat(y, sigma=sigma, k_type=k_type_y)
epsilon = 1E-5
K_I = torch.eye(m)
Kxc_i = torch.inverse(Kxc + epsilon*m*K_I)
Kyc_i = torch.inverse(Kyc + epsilon*m*K_I)
Rx = (Kxc.mm(Kxc_i))
Ry = (Kyc.mm(Kyc_i))
Pxy = torch.sum(torch.mul(Rx, Ry.t()))
return Pxy
if __name__ == "__main__":
x = torch.randn(size=(2, 5))
print(x)
kx_l = kernelmat(x, sigma=None, k_type='linear')
kx_g = kernelmat(x, sigma=None, k_type='gaussian')
print(kx_l)
print(kx_g)