-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
346 lines (271 loc) · 13.3 KB
/
utils.py
File metadata and controls
346 lines (271 loc) · 13.3 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from torch.utils.data.sampler import Sampler
from torch.utils.data.sampler import BatchSampler
import torch
import numpy as np
import itertools
from collections import OrderedDict
class _RepeatSampler(object):
"""
Sampler that repeats forever.
Args:
sampler (Sampler)
"""
def __init__(self, sampler):
self.sampler = sampler
def __iter__(self):
while True:
yield from iter(self.sampler)
class HensmanDataLoader(torch.utils.data.dataloader.DataLoader):
"""
Dataloader when using minibatching with Stochastic Variational Inference.
"""
def __init__(self, dataset, batch_sampler, num_workers):
super().__init__(dataset, batch_sampler=_RepeatSampler(batch_sampler), num_workers=num_workers)
self.iterator = super().__iter__()
def __len__(self):
return len(self.batch_sampler.sampler)
def __iter__(self):
for i in range(len(self)):
yield next(self.iterator)
class SubjectSampler(Sampler):
"""
Perform individual-wise sampling
"""
def __init__(self, data_source, P, T):
super(SubjectSampler, self).__init__(data_source)
self.data_source = data_source
self.P = P
self.T = T
def __iter__(self):
r = np.arange(self.P)
np.random.shuffle(r)
list_of_lists = list(map(lambda x: [i for i in range(self.T*x, self.T*(x+1))], r))
res = list(itertools.chain.from_iterable(list_of_lists))
return iter(res)
def __len__(self):
return len(self.data_source)
class VaryingLengthSubjectSampler(Sampler):
"""
Perform individual-wise sampling when individuals have varying number of temporal samples.
"""
def __init__(self, data_source, id_covariate):
super(VaryingLengthSubjectSampler, self).__init__(data_source)
self.data_source = data_source
self.id_covariate = id_covariate
def f(x):
return int(x['label'][id_covariate].item())
l = list(map(f, data_source))
self.P = len(set(l))
self.start_indices = [l.index(x) for x in list(OrderedDict.fromkeys(l))]
self.end_indices = self.start_indices[1:] + [len(data_source)]
def __iter__(self):
r = np.arange(self.P)
np.random.shuffle(r)
list_of_lists = list(map(lambda x: [(i, x) for i in range(self.start_indices[x], self.end_indices[x])], r))
res = iter(itertools.chain.from_iterable(list_of_lists))
return iter(res)
def __len__(self):
return self.P
class VaryingLengthBatchSampler(BatchSampler):
"""
Perform batch sampling when individuals have varying number of temporal samples.
"""
def __init__(self, sampler, batch_size):
super(VaryingLengthBatchSampler, self).__init__(sampler, batch_size, False)
assert isinstance(sampler, VaryingLengthSubjectSampler)
self.sampler = sampler
self.batch_size = batch_size
#__len__ defined by the superclass
def __iter__(self):
batch = []
batch_subjects = set()
for idx, subj in self.sampler:
if subj not in batch_subjects:
if len(batch_subjects) == self.batch_size:
yield batch
batch = []
batch_subjects.clear()
batch_subjects.add(subj)
batch.append(idx)
yield batch
def batch_predict_varying_T(latent_dim, covar_module0, covar_module1, likelihoods, prediction_x,
test_x, mu, zt_list, id_covariate, eps):
"""
Perform batch predictions when individuals have varying number of temporal samples.
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Q = prediction_x.shape[1]
M = zt_list[0].shape[0]
I_M = torch.eye(M, dtype=torch.double).to(device)
if isinstance(covar_module0, list):
K0xz = torch.zeros(latent_dim, prediction_x.shape[0], M).double().to(device)
K0zz = torch.zeros(latent_dim, M, M).double().to(device)
K0Xz = torch.zeros(latent_dim, test_x.shape[0], M).double().to(device)
for i in range(latent_dim):
covar_module0[i].eval()
covar_module1[i].eval()
likelihoods[i].eval()
z = zt_list[i].to(device)
K0xz[i] = covar_module0[i](prediction_x, z).evaluate()
K0zz[i] = covar_module0[i](z, z).evaluate()
K0Xz[i] = covar_module0[i](test_x, z).evaluate()
else:
covar_module0.eval()
covar_module1.eval()
likelihoods.eval()
K0xz = covar_module0(prediction_x, zt_list).evaluate()
K0zz = covar_module0(zt_list, zt_list).evaluate()
K0Xz = covar_module0(test_x, zt_list).evaluate()
K0zz = K0zz + eps * I_M
K0zx = K0xz.transpose(-1, -2)
iB_st_list = []
H = K0zz
subjects = torch.unique(prediction_x[:, id_covariate]).tolist()
iB_mu = torch.zeros(latent_dim, prediction_x.shape[0], 1, dtype=torch.double).to(device)
for s in subjects:
indices = prediction_x[:, id_covariate] == s
x_st = prediction_x[indices]
T = x_st.shape[0]
I_T = torch.eye(T, dtype=torch.double).to(device)
if isinstance(covar_module0, list):
B_st = torch.zeros(latent_dim, T, T, dtype=torch.double).to(device)
for i in range(latent_dim):
B_st[i] = covar_module1[i](x_st, x_st).evaluate() + I_T * likelihoods[i].noise_covar.noise
else:
stacked_x_st = torch.stack([x_st for i in range(latent_dim)], dim=0)
B_st = covar_module1(stacked_x_st, stacked_x_st).evaluate() + I_T * likelihoods.noise_covar.noise.unsqueeze(dim=2)
LB_st = torch.cholesky(B_st)
iB_st = torch.cholesky_solve(I_T, LB_st)
K0xz_st = K0xz[:, indices]
K0zx_st = K0xz_st.transpose(-1, -2)
iB_K0xz = torch.matmul(iB_st, K0xz_st)
K0zx_iB_K0xz = torch.matmul(K0zx_st, iB_K0xz)
H = H + K0zx_iB_K0xz
iB_mu[:, indices] = torch.matmul(iB_st, mu[indices].T.unsqueeze(dim=2))
iB_st_list.append(iB_st)
K0xz_iH_K0zx_iB_mu_st = torch.matmul(K0xz, torch.solve(torch.matmul(K0zx, iB_mu), H)[0])
iB_K0xz_iH_K0zx_iB_mu = torch.zeros(latent_dim, prediction_x.shape[0], 1, dtype=torch.double).to(device)
for i, s in enumerate(subjects):
indices = prediction_x[:, id_covariate] == s
iB_K0xz_iH_K0zx_iB_mu[:, indices] = torch.matmul(iB_st_list[i], K0xz_iH_K0zx_iB_mu_st[:, indices])
mu_tilde = iB_mu - iB_K0xz_iH_K0zx_iB_mu
K0Xz_iK0zz_K0zx_mu_tilde = torch.matmul(K0Xz, torch.solve(torch.matmul(K0zx, mu_tilde), K0zz)[0])
test_subjects = torch.unique(test_x[:, id_covariate]).cpu().numpy()
mask = np.isin(prediction_x[:, id_covariate].cpu().numpy(), test_subjects)
K1Xx_mu_tilde = torch.zeros(latent_dim, test_x.shape[0], 1, dtype=torch.double).to(device)
for s in test_subjects:
indices = test_x[:, id_covariate] == s
if isinstance(covar_module0, list):
K1Xx = torch.zeros(latent_dim, test_x[indices].shape[0], np.sum(mask)).double().to(device)
for i in range(latent_dim):
K1Xx[i] = covar_module1[i](test_x[indices], prediction_x[mask]).evaluate()
else:
stacked_test_x_indices = torch.stack([test_x[indices] for i in range(latent_dim)], dim=0)
stacked_prediction_x_mask = torch.stack([prediction_x[mask] for i in range(latent_dim)], dim=0)
K1Xx = covar_module1(stacked_test_x_indices, stacked_prediction_x_mask).evaluate()
K1Xx_mu_tilde[:, indices] = torch.matmul(K1Xx, mu_tilde[:, mask])
Z_pred = (K0Xz_iK0zz_K0zx_mu_tilde + K1Xx_mu_tilde).squeeze(dim=2).T
return Z_pred
def batch_predict(latent_dim, covar_module0, covar_module1, likelihoods, prediction_x, test_x, mu,
zt_list, P, T, id_covariate, eps):
"""
Perform batch-wise predictions
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Q = prediction_x.shape[1]
M = zt_list[0].shape[0]
I_M = torch.eye(M, dtype=torch.double).to(device)
I_T = torch.eye(T, dtype=torch.double).to(device)
x_st = torch.reshape(prediction_x, [P, T, Q])
mu = mu.T
mu_st = torch.reshape(mu, [latent_dim, P, T, 1])
if isinstance(covar_module0, list):
K0xz = torch.zeros(latent_dim, P*T, M).double().to(device)
K0zz = torch.zeros(latent_dim, M, M).double().to(device)
B_st = torch.zeros(latent_dim, P, T, T).double().to(device)
K0Xz = torch.zeros(latent_dim, test_x.shape[0], M).double().to(device)
for i in range(latent_dim):
covar_module0[i].eval()
covar_module1[i].eval()
likelihoods[i].eval()
z = zt_list[i].to(device)
K0xz[i] = covar_module0[i](prediction_x, z).evaluate()
K0zz[i] = covar_module0[i](z, z).evaluate()
B_st[i] = covar_module1[i](x_st, x_st).evaluate() + I_T * likelihoods[i].noise_covar.noise
K0Xz[i] = covar_module0[i](test_x, z).evaluate()
else:
covar_module0.eval()
covar_module1.eval()
likelihoods.eval()
stacked_x_st = torch.stack([x_st for i in range(latent_dim)], dim=1)
K0xz = covar_module0(prediction_x, zt_list).evaluate()
K0zz = covar_module0(zt_list, zt_list).evaluate()
B_st = (covar_module1(stacked_x_st, stacked_x_st).evaluate() + I_T * likelihoods.noise_covar.noise.unsqueeze(dim=2)).transpose(0, 1)
K0Xz = covar_module0(test_x, zt_list).evaluate()
K0zz = K0zz + eps * I_M
LB_st = torch.cholesky(B_st)
iB_st = torch.cholesky_solve(I_T, LB_st)
K0xz_st = torch.reshape(K0xz, [latent_dim, P, T, M])
K0zx_st = K0xz_st.transpose(-1, -2)
K0zx = K0xz.transpose(-1, -2)
iB_K0xz = torch.matmul(iB_st, K0xz_st)
K0zx_iB_K0xz = torch.matmul(K0zx, torch.reshape(iB_K0xz, [latent_dim, P*T, M]))
H = K0zz + K0zx_iB_K0xz
iB_mu = torch.matmul(iB_st, mu_st).view(latent_dim, -1, 1)
K0xz_iH_K0zx_iB_mu_st = torch.matmul(K0xz, torch.solve(torch.matmul(K0zx, iB_mu), H)[0]).reshape(latent_dim, P, T, -1)
iB_K0xz_iH_K0zx_iB_mu = torch.matmul(iB_st, K0xz_iH_K0zx_iB_mu_st).view(latent_dim, -1, 1)
mu_tilde = iB_mu - iB_K0xz_iH_K0zx_iB_mu
K0Xz_iK0zz_K0zx_mu_tilde = torch.matmul(K0Xz, torch.solve(torch.matmul(K0zx, mu_tilde), K0zz)[0])
test_subjects = torch.unique(test_x[:, id_covariate]).cpu().numpy()
mask = np.isin(prediction_x[:, id_covariate].cpu().numpy(), test_subjects)
K1Xx_mu_tilde = torch.zeros(latent_dim, test_x.shape[0], 1, dtype=torch.double).to(device)
for s in test_subjects:
indices = test_x[:, id_covariate] == s
if isinstance(covar_module0, list):
K1Xx = torch.zeros(latent_dim, test_x[indices].shape[0], np.sum(mask)).double().to(device)
for i in range(latent_dim):
K1Xx[i] = covar_module1[i](test_x[indices], prediction_x[mask]).evaluate()
else:
stacked_test_x_indices = torch.stack([test_x[indices] for i in range(latent_dim)], dim=0)
stacked_prediction_x_mask = torch.stack([prediction_x[mask] for i in range(latent_dim)], dim=0)
K1Xx = covar_module1(stacked_test_x_indices, stacked_prediction_x_mask).evaluate()
K1Xx_mu_tilde[:, indices] = torch.matmul(K1Xx, mu_tilde[:, mask])
Z_pred = (K0Xz_iK0zz_K0zx_mu_tilde + K1Xx_mu_tilde).squeeze(dim=2).T
return Z_pred
def predict(covar_module0, covar_module1, likelihood, train_xt, test_x, mu, z, P, T, id_covariate, eps):
"""
Helper function to perform predictions.
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Q = train_xt.shape[1]
M = z.shape[0]
I_M = torch.eye(M, dtype=torch.double).to(device)
I_T = torch.eye(T, dtype=torch.double).to(device)
x_st = torch.reshape(train_xt, [P, T, Q])
mu_st = torch.reshape(mu, [P, T, 1])
K0xz = covar_module0(train_xt, z).evaluate()
K0zz = covar_module0(z, z).evaluate() + eps * I_M
K1_st = covar_module1(x_st, x_st).evaluate()
K0Xz = covar_module0(test_x, z).evaluate()
B_st = K1_st + I_T * likelihood.noise_covar.noise
LB_st = torch.cholesky(B_st)
iB_st = torch.cholesky_solve(I_T, LB_st)
K0xz_st = torch.reshape(K0xz, [P, T, M])
K0zx_st = K0xz_st.transpose(-1, -2)
iB_K0xz = torch.matmul(iB_st, K0xz_st)
K0zx_iB_K0xz = torch.matmul(K0xz.T, torch.reshape(iB_K0xz, [P*T, M]))
H = K0zz + K0zx_iB_K0xz
iB_mu = torch.matmul(iB_st, mu_st).view(-1)
K0xz_iH_K0zx_iB_mu_st = torch.matmul(K0xz, torch.solve(torch.matmul(K0xz.T, iB_mu).unsqueeze(dim=1), H)[0]).reshape(P, T, -1)
iB_K0xz_iH_K0zx_iB_mu = torch.matmul(iB_st, K0xz_iH_K0zx_iB_mu_st).view(-1)
mu_tilde = iB_mu - iB_K0xz_iH_K0zx_iB_mu
K0Xz_iK0zz_K0zx_mu_tilde = torch.matmul(K0Xz, torch.solve(torch.matmul(K0xz.T, mu_tilde).unsqueeze(dim=1), K0zz)[0]).squeeze()
test_subjects = torch.unique(test_x[:, id_covariate]).cpu().numpy()
mask = np.isin(train_xt[:, id_covariate].cpu().numpy(), test_subjects)
K1Xx_mu_tilde = torch.zeros(test_x.shape[0], dtype=torch.double).to(device)
for s in test_subjects:
indices = test_x[:, id_covariate] == s
K1Xx = covar_module1(test_x[indices], train_xt[mask]).evaluate()
K1Xx_mu_tilde[indices] = torch.matmul(K1Xx, mu_tilde[mask])
Z_pred = K0Xz_iK0zz_K0zx_mu_tilde + K1Xx_mu_tilde
return Z_pred