-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild_kernel.py
209 lines (153 loc) · 7.4 KB
/
build_kernel.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# build_kernel.py
import torch
import hal.models as models
import hal.utils.misc as misc
import hal.kernels as kernels
__all__ = ['KernelMethodY']
class KernelMethodY:
def __init__(self, opts, modal):
self.hparams = opts
if modal=='image':
self.lambda_z = opts.tau_z_i
self.lambda_s = opts.tau_i
self.gamma = opts.gamma_i
else:
self.lambda_z = opts.tau_z_t
self.lambda_s = opts.tau_t
self.gamma = opts.gamma_t
self.kernel_x = getattr(kernels, self.hparams.kernel_x)(**self.hparams.kernel_x_options)
self.kernel_y = getattr(kernels, self.hparams.kernel_y)(**self.hparams.kernel_y_options)
self.kernel_s = getattr(kernels, self.hparams.kernel_s)(**self.hparams.kernel_s_options)
self.kernel_z = getattr(kernels, self.hparams.kernel_z)(**self.hparams.kernel_z_options)
def solver(self, X, Y, S, Z=None):
'''
Z = theta_D * R_xD
'''
device = 'cuda'
dtype = torch.float
s = S.to(dtype)
n = len(X)
if self.hparams.rff_flag:
R_x = self.kernel_x(X).to(dtype=dtype, device=device)
R_x_c = misc.mean_center(R_x, dim=0).to(dtype=dtype, device=device)
dtype = R_x.dtype
R_y = self.kernel_y(Y).to(dtype=dtype, device=device)
R_y_c = misc.mean_center(R_y, dim=0).to(dtype=dtype, device=device)
R_s = self.kernel_s(s).to(dtype=dtype, device=device)
R_s_c = misc.mean_center(R_s, dim=0).to(dtype=dtype, device=device)
if not Z is None:
Z_k = self.kernel_z(Z).to(dtype=dtype, device=device)
Z_k_c = misc.mean_center(Z_k, dim=0).to(dtype=dtype, device=device)
b_y = torch.mm(R_x.t(), R_y_c)
b_y = torch.mm(b_y, b_y.t())
if not Z is None:
b_z = torch.mm(R_x.t(), Z_k_c)
b_z = torch.mm(b_z, b_z.t())
b_s = torch.mm(R_x.t(), R_s_c)
b_s = torch.mm(b_s, b_s.t())
norm2_b_y = torch.linalg.norm(b_y, 2)
norm2_b_s = torch.linalg.norm(b_s, 2)
if not Z is None:
norm2_b_z = torch.linalg.norm(b_z, 2)
b = b_y / norm2_b_y + self.lambda_z / (1. - self.lambda_z) * b_z / norm2_b_z - self.lambda_s / (
1. - self.lambda_s) * b_s / norm2_b_s
self.norm2_b_z = norm2_b_z
else:
b = b_y / norm2_b_y - self.lambda_s / (1. - self.lambda_s) * b_s / norm2_b_s
self.norm2_b_y = norm2_b_y
self.norm2_b_s = norm2_b_s
b = (b + b.t()) / 2
c = torch.mm(R_x_c.t(), R_x_c) + n * self.gamma * torch.eye(R_x.shape[1], device=R_x.device)
c = (c + c.t()) / 2
eigs, V = torch.linalg.eig(torch.mm(torch.linalg.inv(c), b))
eigs = torch.real(eigs)
V = torch.real(V)
sorted, indeces = torch.sort(eigs, descending=True)
U = V[:, indeces[0:self.hparams.dim_z]]
#########################################
r0 = self.hparams.dim_z
if self.lambda_s == 0:
r = r0
else:
r1 = min((sorted > 0).sum(), r0)
###### Energy Thresholding ######
if r1 > 0:
for k in range(1, r1 + 1):
if torch.linalg.norm(sorted[0:k]) ** 2 / torch.linalg.norm(sorted[0:r1]) ** 2 >= 0.95:
r = k
break
else:
r = 0
######################################################
if self.lambda_s >= 0.999999999:
r = 0
######################################################
U[:, r:self.hparams.dim_z] = 0
encoder = models.KernelizedEncoder(U=n ** 0.5 * U, w=self.kernel_x.w, b=self.kernel_x.b)
if not Z is None:
Z_enc = encoder(X)
if ((Z_enc / Z) < 0).sum() / Z.numel() > 0.5:
U *= -1
encoder = models.KernelizedEncoder(U=n ** 0.5 * U, w=self.kernel_x.w, b=self.kernel_x.b)
else:
R_x = self.kernel_x(X).to(dtype=dtype, device=device)
R_x_c = misc.mean_center(R_x, dim=0).to(dtype=dtype, device=device)
dtype = R_x.dtype
R_y = self.kernel_y(Y).to(dtype=dtype, device=device)
R_y_c = misc.mean_center(R_y, dim=0).to(dtype=dtype, device=device)
R_y_c = misc.mean_center(R_y_c.t(), dim=0).to(dtype=dtype, device=device)
R_s = self.kernel_s(s).to(dtype=dtype, device=device)
R_s_c = misc.mean_center(R_s, dim=0).to(dtype=dtype, device=device)
R_s_c = misc.mean_center(R_s_c.t(), dim=0).to(dtype=dtype, device=device)
if not Z is None:
Z_k = self.kernel_z(Z).to(dtype=dtype, device=device)
Z_k_c = misc.mean_center(Z_k, dim=0).to(dtype=dtype, device=device)
Z_k_c = misc.mean_center(Z_k_c.t(), dim=0).to(dtype=dtype, device=device)
b_y = torch.mm(torch.mm(R_x.t(), R_y_c), R_x)
if not Z is None:
b_z = torch.mm(torch.mm(R_x.t(), Z_k_c), R_x)
b_s = torch.mm(torch.mm(R_x.t(), R_s_c), R_x)
norm2_b_y = torch.linalg.norm(b_y, 2)
norm2_b_s = torch.linalg.norm(b_s, 2)
if not Z is None:
norm2_b_z = torch.linalg.norm(b_z, 2)
b = b_y / norm2_b_y + self.lambda_z / (1. - self.lambda_z) * b_z / norm2_b_z - self.lambda_s / (
1. - self.lambda_s) * b_s / norm2_b_s
self.norm2_b_z = norm2_b_z
else:
b = b_y / norm2_b_y - self.lambda_s / (1. - self.lambda_s) * b_s / norm2_b_s
self.norm2_b_y = norm2_b_y
self.norm2_b_s = norm2_b_s
b = (b + b.t()) / 2
c = torch.mm(R_x_c.t(), R_x_c) + n * self.gamma * torch.eye(R_x.shape[1], device=R_x.device)
c = (c + c.t()) / 2
eigs, V = torch.linalg.eig(torch.mm(torch.linalg.inv(c), b))
eigs = torch.real(eigs)
V = torch.real(V)
sorted, indeces = torch.sort(eigs, descending=True)
U = V[:, indeces[0:self.hparams.dim_z]]
#########################################
r0 = self.hparams.dim_z
# if self.hparams.tau == 0:
if self.lambda_s == 0:
r = r0
else:
r1 = min((sorted > 0).sum(), r0)
###### Energy Thresholding ######
if r1 > 0:
for k in range(1, r1 + 1):
if torch.linalg.norm(sorted[0:k]) ** 2 / torch.linalg.norm(sorted[0:r1]) ** 2 >= 0.95:
r = k
break
else:
r = 0
######################################################
if self.lambda_s >= 0.999999999:
r = 0
######################################################
U[:, r:self.hparams.dim_z] = 0
encoder = models.KernelizedEncoderFull(U=n ** 0.5 * U, kernel=self.kernel_x, X=X)
self.encoder = encoder
return encoder
def encod(self, X):
return self.encoder(X)