-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolver.py
More file actions
455 lines (361 loc) · 15.9 KB
/
solver.py
File metadata and controls
455 lines (361 loc) · 15.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import torch
import numpy as np
import abc
from tqdm import trange
from scipy.stats import ortho_group
import torch.nn.functional as F
from losses import get_score_fn, get_score_fn_adj
from utils.graph_utils import mask_adjs, mask_x, gen_noise, gen_spec_noise, gen_spec_noise2
from sde import VPSDE, subVPSDE
import math
from sympy.matrices import Matrix, GramSchmidt
def orthogo_tensor(x):
m, n = x.size()
x_np = x.t().numpy()
matrix = [Matrix(col) for col in x_np.T]
gram = GramSchmidt(matrix)
ort_list = []
for i in range(m):
vector = []
for j in range(n):
vector.append(float(gram[i][j]))
ort_list.append(vector)
ort_list = np.mat(ort_list)
ort_list = torch.from_numpy(ort_list)
ort_list = F.normalize(ort_list, dim=1)
return ort_list
def top_k_eigen(adjs_tensor, k=20):
data_size, n, _ = adjs_tensor.shape
# Initialize tensors to store top k eigenvalues and eigenvectors
la, u = torch.linalg.eigh(adjs_tensor[0])
top_eigenvalues = torch.zeros((data_size, k), dtype=la.dtype, device=adjs_tensor.device)
top_eigenvectors = torch.zeros((data_size, n, k), dtype=u.dtype, device=adjs_tensor.device)
for i in range(data_size):
la, u = torch.linalg.eigh(adjs_tensor[i])
# Select the top k eigenvalues and corresponding eigenvectors
top_indices = torch.argsort(la, descending=True)[:k]
top_eigenvalues[i] = la[top_indices]
top_eigenvectors[i] = u[:, top_indices]
if i%32==0: print(f"Compute Top K Eigen: {i}/{data_size}")
return top_eigenvalues, top_eigenvectors
class Predictor(abc.ABC):
"""The abstract class for a predictor algorithm."""
def __init__(self, sde, score_fn, probability_flow=False):
super().__init__()
self.sde = sde
# Compute the reverse SDE/ODE
self.rsde = sde.reverse(score_fn, probability_flow)
self.score_fn = score_fn
@abc.abstractmethod
def update_fn(self, x, t, flags):
pass
class Corrector(abc.ABC):
"""The abstract class for a corrector algorithm."""
def __init__(self, sde, score_fn, snr, scale_eps, n_steps):
super().__init__()
self.sde = sde
self.score_fn = score_fn
self.snr = snr
self.scale_eps = scale_eps
self.n_steps = n_steps
@abc.abstractmethod
def update_fn(self, x, t, flags):
pass
class EulerMaruyamaPredictor(Predictor):
def __init__(self, obj, sde, score_fn, probability_flow=False):
super().__init__(sde, score_fn, probability_flow)
self.obj = obj
def update_fn(self, x, adj, flags, t):
dt = -1. / self.rsde.N
# print("EulerMaruyamaPredictor update_fn")
if self.obj=='x':
z = gen_noise(x, flags, sym=False)
drift, diffusion = self.rsde.sde(x, adj, flags, t, is_adj=False)
x_mean = x + drift * dt
x = x_mean + diffusion[:, None, None] * np.sqrt(-dt) * z
return x, x_mean
elif self.obj=='adj':
# print("EulerMaruyamaPredictor update adj")
z = gen_noise(adj, flags)
# z = gen_spec_noise2(adj, flags, u, la)
# la, u = torch.symeig(adj, eigenvectors=True)
# la = torch.diag_embed(la)
#
# z = gen_spec_noise(adj, flags, u, la)
drift, diffusion = self.rsde.sde(x, adj, flags, t, is_adj=True)
adj_mean = adj + drift * dt
adj = adj_mean + diffusion[:, None, None] * np.sqrt(-dt) * z
return adj, adj_mean
else:
raise NotImplementedError(f"obj {self.obj} not yet supported.")
class EulerMaruyamaPredictor2(Predictor):
def __init__(self, obj, sde, score_fn, probability_flow=False):
super().__init__(sde, score_fn, probability_flow)
self.obj = obj
def update_fn(self, x, adj, flags, t, u, la):
dt = -1. / self.rsde.N
# print("EulerMaruyamaPredictor update_fn")
if self.obj=='x':
z = gen_noise(x, flags, sym=False)
drift, diffusion = self.rsde.sde(x, adj, flags, t,u, la, is_adj=False)
x_mean = x + drift * dt
x = x_mean + diffusion[:, None, None] * np.sqrt(-dt) * z
return x, x_mean
elif self.obj=='u':
z = gen_noise(u, flags, sym=False)
drift, diffusion = self.rsde.sde(x, adj, flags, t,u, la, is_adj=False, is_u=True)
u_mean = u + drift * dt
u = u_mean + diffusion[:, None, None] * np.sqrt(-dt) * z
return u, u_mean
elif self.obj=='adj':
z = gen_spec_noise2(adj, flags, u, la)
drift, diffusion = self.rsde.sde(x, adj, flags, t,u, la, is_adj=True)
adj_eigen_mean = la + drift * dt
adj_eigen = adj_eigen_mean + diffusion[:, None] * np.sqrt(-dt) * z
# u_T = torch.transpose(u, -1, -2)
# adj_eigen_diag = torch.diag_embed(adj_eigen)
# adj_eigen_mean_diag = torch.diag_embed(adj_eigen_mean)
# adj = torch.bmm(torch.bmm(u, adj_eigen_diag), u_T)
# adj_mean = torch.bmm(torch.bmm(u, adj_eigen_mean_diag), u_T)
u_conj_T = torch.conj(torch.transpose(u, -1, -2))
adj_eigen_diag = torch.diag_embed(adj_eigen).type(torch.complex64)
adj_eigen_mean_diag = torch.diag_embed(adj_eigen_mean).type(torch.complex64)
adj = torch.matmul(torch.matmul(u, adj_eigen_diag), u_conj_T)
adj_mean = torch.matmul(torch.matmul(u, adj_eigen_mean_diag), u_conj_T)
adj = mask_adjs(adj, flags)
adj_mean = mask_adjs(adj_mean, flags)
return adj, adj_mean, adj_eigen, adj_eigen_mean
# return adj_eigen, adj_eigen_mean
else:
raise NotImplementedError(f"obj {self.obj} not yet supported.")
class ReverseDiffusionPredictor(Predictor):
def __init__(self, obj, sde, score_fn, probability_flow=False):
super().__init__(sde, score_fn, probability_flow)
self.obj = obj
def update_fn(self, x, adj, flags, t):
if self.obj == 'x':
f, G = self.rsde.discretize(x, adj, flags, t, is_adj=False)
z = gen_noise(x, flags, sym=False)
x_mean = x - f
x = x_mean + G[:, None, None] * z
return x, x_mean
elif self.obj == 'adj':
f, G = self.rsde.discretize(x, adj, flags, t, is_adj=True)
z = gen_noise(adj, flags)
#
# la, u = torch.symeig(adj, eigenvectors=True)
# la = torch.diag_embed(la)
# z = gen_spec_noise(adj, flags, u, la)
# z = gen_noise(adj, flags)
adj_mean = adj - f
adj = adj_mean + G[:, None, None] * z
return adj, adj_mean
else:
raise NotImplementedError(f"obj {self.obj} not yet supported.")
class ReverseDiffusionPredictor2(Predictor):
def __init__(self, obj, sde, score_fn, probability_flow=False):
super().__init__(sde, score_fn, probability_flow)
self.obj = obj
def update_fn(self, x, adj, flags, t, u, la):
if self.obj == 'x':
f, G = self.rsde.discretize(x, adj, flags, t, is_adj=False)
z = gen_noise(x, flags, sym=False)
x_mean = x - f
x = x_mean + G[:, None, None] * z
return x, x_mean
elif self.obj == 'adj':
# print("in ReverseDiffusionPredictor2 before self.rsde.discretize")
f, G = self.rsde.discretize(x, adj, flags, t, is_adj=True)
# z = gen_noise(adj, flags)
#
# la, u = torch.symeig(adj, eigenvectors=True)
# la = torch.diag_embed(la)
z = gen_spec_noise2(adj, flags, u, la)
# z = gen_noise(adj, flags)
adj_mean = adj - f
adj = adj_mean + G[:, None, None] * z
return adj, adj_mean
else:
raise NotImplementedError(f"obj {self.obj} not yet supported.")
class NoneCorrector(Corrector):
"""An empty corrector that does nothing."""
def __init__(self, obj, sde, score_fn, snr, scale_eps, n_steps):
self.obj = obj
pass
def update_fn(self, x, adj, flags, t):
if self.obj == 'x':
return x, x
elif self.obj == 'adj':
return adj, adj
else:
raise NotImplementedError(f"obj {self.obj} not yet supported.")
class NoneCorrector2(Corrector):
"""An empty corrector that does nothing."""
def __init__(self, obj, sde, score_fn, snr, scale_eps, n_steps):
self.obj = obj
pass
def update_fn(self, x, adj, flags, t, u, la):
if self.obj == 'x':
return x, x
elif self.obj == 'adj':
return adj, adj, la, la
else:
raise NotImplementedError(f"obj {self.obj} not yet supported.")
class LangevinCorrector(Corrector):
def __init__(self, obj, sde, score_fn, snr, scale_eps, n_steps):
super().__init__(sde, score_fn, snr, scale_eps, n_steps)
self.obj = obj
def update_fn(self, x, adj, flags, t):
# print("LangevinCorrector update fn")
sde = self.sde
score_fn = self.score_fn
n_steps = self.n_steps
target_snr = self.snr
seps = self.scale_eps
if isinstance(sde, VPSDE) or isinstance(sde, subVPSDE):
timestep = (t * (sde.N - 1) / sde.T).long()
alpha = sde.alphas.to(t.device)[timestep]
else:
alpha = torch.ones_like(t)
if self.obj == 'x':
for i in range(n_steps):
grad = score_fn(x, adj, flags, t)
noise = gen_noise(x, flags, sym=False)
grad_norm = torch.norm(grad.reshape(grad.shape[0], -1), dim=-1).mean()
noise_norm = torch.norm(noise.reshape(noise.shape[0], -1), dim=-1).mean()
step_size = (target_snr * noise_norm / grad_norm) ** 2 * 2 * alpha
x_mean = x + step_size[:, None, None] * grad
x = x_mean + torch.sqrt(step_size * 2)[:, None, None] * noise * seps
return x, x_mean
elif self.obj == 'adj':
for i in range(n_steps):
grad = score_fn(x, adj, flags, t)
noise = gen_noise(adj, flags)
grad_norm = torch.norm(grad.reshape(grad.shape[0], -1), dim=-1).mean()
noise_norm = torch.norm(noise.reshape(noise.shape[0], -1), dim=-1).mean()
step_size = (target_snr * noise_norm / grad_norm) ** 2 * 2 * alpha
adj_mean = adj + step_size[:, None, None] * grad
adj = adj_mean + torch.sqrt(step_size * 2)[:, None, None] * noise * seps
return adj, adj_mean
else:
raise NotImplementedError(f"obj {self.obj} not yet supported")
class LangevinCorrector2(Corrector):
def __init__(self, obj, sde, score_fn, snr, scale_eps, n_steps):
super().__init__(sde, score_fn, snr, scale_eps, n_steps)
self.obj = obj
def update_fn(self, x, adj, flags, t, u, la):
# print("LangevinCorrector update fn")
sde = self.sde
score_fn = self.score_fn
n_steps = self.n_steps
target_snr = self.snr
seps = self.scale_eps
if isinstance(sde, VPSDE) or isinstance(sde, subVPSDE):
timestep = (t * (sde.N - 1) / sde.T).long()
alpha = sde.alphas.to(t.device)[timestep]
else:
alpha = torch.ones_like(t)
if self.obj == 'x':
for i in range(n_steps):
grad = score_fn(x, adj, flags, t, u, la)
noise = gen_noise(x, flags, sym=False)
grad_norm = torch.norm(grad.reshape(grad.shape[0], -1), dim=-1).mean()
noise_norm = torch.norm(noise.reshape(noise.shape[0], -1), dim=-1).mean()
step_size = (target_snr * noise_norm / grad_norm) ** 2 * 2 * alpha
x_mean = x + step_size[:, None, None] * grad
x = x_mean + torch.sqrt(step_size * 2)[:, None, None] * noise * seps
return x, x_mean
elif self.obj == 'adj':
for i in range(n_steps):
grad = score_fn(x, adj, flags, t, u, la)
# noise = gen_noise(adj, flags)
noise = gen_spec_noise2(adj, flags, u, la)
grad_norm = torch.norm(grad.reshape(grad.shape[0], -1), dim=-1).mean()
noise_norm = torch.norm(noise.reshape(noise.shape[0], -1), dim=-1).mean()
step_size = (target_snr * noise_norm / grad_norm) ** 2 * 2 * alpha
adj_eigen_mean = la + step_size[:, None] * grad
adj_eigen = adj_eigen_mean + torch.sqrt(step_size * 2)[:, None] * noise * seps
#for complex
# adj_eigen_diag = torch.diag_embed(adj_eigen)
# adj_eigen_mean_diag = torch.diag_embed(adj_eigen_mean)
# u_T = torch.transpose(u, -1, -2)
# print("u:", u.shape, " ")
# adj = torch.bmm(torch.bmm(u, adj_eigen_diag), u_T)
# adj_mean = torch.bmm(torch.bmm(u, adj_eigen_mean_diag), u_T)
u_conj_T = torch.conj(torch.transpose(u, -1, -2))
adj_eigen_diag = torch.diag_embed(adj_eigen).type(torch.complex64)
adj_eigen_mean_diag = torch.diag_embed(adj_eigen_mean).type(torch.complex64)
adj = torch.matmul(torch.matmul(u, adj_eigen_diag), u_conj_T)
adj_mean = torch.matmul(torch.matmul(u, adj_eigen_mean_diag), u_conj_T)
adj = mask_adjs(adj, flags)
adj_mean = mask_adjs(adj_mean, flags)
return adj, adj_mean, adj_eigen, adj_eigen_mean
else:
raise NotImplementedError(f"obj {self.obj} not yet supported")
# -------- PC sampler --------
def get_pc_sampler_4Di_spec(sde_x, sde_adj, shape_x, shape_adj, predictor='Euler', corrector='None',
snr=0.1, scale_eps=1.0, n_steps=1,
probability_flow=False, continuous=False,
denoise=True, eps=1e-3, device='cuda'):
def pc_sampler(model_x, model_adj, init_flags, train_tensors, spec_dim, diff_steps):
# la, u = torch.symeig(train_tensors, eigenvectors=True)
# la, u = torch.linalg.eigh(train_tensors)
la, u = top_k_eigen(train_tensors, spec_dim)
u = u.to(device)
#u_T = torch.transpose(u, -1, -2)
u_conj_T = torch.conj(torch.transpose(u, -1, -2))
num_nodes = torch.sum(init_flags, dim=1)
score_fn_x = get_score_fn(sde_x, model_x, train=False, continuous=continuous)
score_fn_adj = get_score_fn_adj(sde_adj, model_adj, train=False, continuous=continuous)
# predictor_fn = ReverseDiffusionPredictor if predictor=='Reverse' else EulerMaruyamaPredictor
predictor_fn = ReverseDiffusionPredictor2 if predictor == 'Reverse' else EulerMaruyamaPredictor2
corrector_fn = LangevinCorrector2 if corrector == 'Langevin' else NoneCorrector2
predictor_obj_x = predictor_fn('x', sde_x, score_fn_x, probability_flow)
corrector_obj_x = corrector_fn('x', sde_x, score_fn_x, snr, scale_eps, n_steps)
predictor_obj_adj = predictor_fn('adj', sde_adj, score_fn_adj, probability_flow)
corrector_obj_adj = corrector_fn('adj', sde_adj, score_fn_adj, snr, scale_eps, n_steps)
with torch.no_grad():
# -------- Initial sample --------
x = sde_x.prior_sampling(shape_x).to(device)
adj_eigen = sde_adj.prior_sampling_sym3(la.shape, u).to(device)
flags = init_flags
x = mask_x(x, flags)
eigen_diag = torch.diag_embed(adj_eigen).type(torch.complex64)
#adj = torch.bmm(torch.bmm(u, eigen_diag), u_T)
#u_conj_T = torch.conj(torch.transpose(u, -1, -2))
adj = torch.matmul(torch.matmul(u, eigen_diag), u_conj_T)
adj = mask_adjs(adj, flags)
nonzero_count_flag = torch.zeros((flags.shape[0]))
eigen_mask = torch.zeros((la.shape))
for i in range(flags.shape[0]):
count = torch.count_nonzero(flags[i])
nonzero_count_flag[i] = count
eigen_mask[i,:count//2] = 1
eigen_mask[i, -count//2:] = 1
# print("eigen_mask:",eigen_mask[i])
eigen_mask = eigen_mask.to(adj.device)
timesteps = torch.linspace(sde_adj.T, eps, diff_steps, device=device)
# -------- Reverse diffusion process --------
la = adj_eigen
if diff_steps: diff_steps = diff_steps
else: diff_steps = sde_adj.N
print("diff_steps:",diff_steps)
#for i in trange(0, (diff_steps), desc = '[Sampling]', position = 1, leave=False):
for i in range(0, (diff_steps)):
t = timesteps[i]
vec_t = torch.ones(shape_adj[0], device=t.device) * t
_x = x
x, x_mean = corrector_obj_x.update_fn(x, adj, flags, vec_t,u, la)
# print("before corrector_obj_adj:",adj.shape)
adj, adj_mean, adj_eigen, adj_mean_eigen = corrector_obj_adj.update_fn(_x, adj, flags, vec_t, u, la)
# print("after corrector_obj_adj:", adj.shape, adj_mean.shape)
# la = adj_eigen
la = adj_mean_eigen
la = la*eigen_mask
_x = x
x, x_mean = predictor_obj_x.update_fn(x, adj, flags, vec_t, u, la)
adj, adj_mean,adj_eigen, adj_mean_eigen = predictor_obj_adj.update_fn(_x, adj, flags, vec_t, u, la)
la = adj_mean_eigen
la = la * eigen_mask
return (x_mean if denoise else x), (adj_mean if denoise else adj), diff_steps * (n_steps + 1)
return pc_sampler