-
Notifications
You must be signed in to change notification settings - Fork 2
/
IDEA_Mesh_1_demo.py
592 lines (563 loc) · 23.6 KB
/
IDEA_Mesh_1_demo.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import torch
import torch.optim as optim
from modules.IDEA import GenNet_tanh
from modules.IDEA import DiscNet
from modules.loss import get_pre_gen_loss
from modules.loss import get_gen_loss
from modules.loss import get_disc_loss
from utils import *
import scipy.sparse
import random
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
setup_seed(0)
# ====================
data_name = 'Mesh-1'
num_nodes = 38 # Number of nodes
num_snaps = 445 # Number of snapshots
max_thres = 2000 # Threshold for maximum edge weight
noise_dim = 32 # Dimensionality of noise input
feat_dim = 32 # Dimensionality of node feature
pos_dim = 16 # Dimensionality of positional embedding
GNN_feat_dim = feat_dim+pos_dim
FEM_dims = [GNN_feat_dim, 32, 16] # Layer configuration of feature extraction module (FEM)
EDM_dims = [(FEM_dims[-1]+noise_dim), 32, 32] # Layer configuration of embedding derivation module (EDM)
EAM_dims = [(EDM_dims[-1]+FEM_dims[-1]), 32, 16] # Layer configuration of embedding aggregation module (EAM)
disc_dims = [FEM_dims[-1], 32, 16, 8] # Layer configuration of discriminator
save_flag = False # Flag whether to save the trained model (w.r.t. each epoch)
# ====================
alpha = 10 # Parameter to balance the ER loss
beta = 0.1 # Parameter to balance the SDM loss
lambd = 0.1 # Parameter of attentive aligning unit
theta = 0.1 # Decaying factor
# ====================
edge_seq = np.load('data/%s_edge_seq.npy' % (data_name), allow_pickle=True)
mod_seq = np.load('data/%s_mod_seq.npy' % (data_name), allow_pickle=True)
# ==========
# Get the node features
feat = np.load('data/%s_feat.npy' % (data_name), allow_pickle=True)
feat_tnr = torch.FloatTensor(feat).to(device)
# Get positional embedding
pos_emb = None
for p in range(num_nodes):
if p==0:
pos_emb = get_pos_emb(p, pos_dim)
else:
pos_emb = np.concatenate((pos_emb, get_pos_emb(p, pos_dim)), axis=0)
pos_tnr = torch.FloatTensor(pos_emb).to(device)
feat_tnr = torch.cat((feat_tnr, pos_tnr), dim=1)
# ====================
dropout_rate = 0.0 # Dropout rate
win_size = 10 # Window size of historical snapshots
epsilon = 0.01 # Threshold of the zero-refining
num_pre_epochs = 30 # Number of pre-training epochs
num_epochs = 200 # Number of training epochs
num_test_snaps = 50 # Number of test snapshots
num_val_snaps = 10 # Number of validation snapshots
num_train_snaps = num_snaps-num_test_snaps-num_val_snaps # Number of training snapshots
# ====================
# Get align matrices (i.e., identity matrices for level-1)
align_mat = torch.eye(num_nodes).to(device)
align_list = []
for i in range(win_size):
align_list.append(align_mat)
# ==========
feat_list = []
for i in range(win_size+1):
feat_list.append(feat_tnr)
# ==========
num_nodes_list = []
for i in range(win_size+1):
num_nodes_list.append(num_nodes)
# ====================
# Define the model
gen_net = GenNet_tanh(FEM_dims, EDM_dims, EAM_dims, dropout_rate).to(device) # Generator
disc_net = DiscNet(FEM_dims, disc_dims, dropout_rate).to(device) # Discriminator
# ==========
# Define the optimizer
pre_gen_opt = optim.Adam(gen_net.parameters(), lr=5e-4, weight_decay=1e-5) # Optimizer for the pre-training of generator
gen_opt = optim.Adam(gen_net.parameters(), lr=5e-4, weight_decay=1e-5) # Optimizer for the formal optimization of generator
disc_opt = optim.Adam(disc_net.parameters(), lr=5e-4, weight_decay=1e-5) # Optimizer for the formal optimization of discrminator
# ====================
# Pre-training of generator
for epoch in range(num_pre_epochs):
# ====================
# Pre-train the model
gen_net.train()
disc_net.train()
# ==========
train_cnt = 0
gen_loss_list = []
# ==========
for tau in range(win_size, num_train_snaps):
# ====================
sup_list = [] # List of GNN support (tensor)
noise_list = [] # List of random noise inputs
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
adj = get_adj_wei(edges, num_nodes, max_thres)
adj_norm = adj/max_thres # Normalize the edge weights to [0, 1]
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup(adj_norm)
sup_sp = sp.sparse.coo_matrix(sup)
sup_sp = sparse_to_tuple(sup_sp)
idxs = torch.LongTensor(sup_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_sp[1]).to(device)
sup_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_sp[2]).float().to(device)
sup_list.append(sup_tnr)
# =========
# Generate the random noise via random projection
mod_tnr = torch.FloatTensor(mod_seq[t]).to(device)
rand_mat = rand_proj(num_nodes, noise_dim)
rand_tnr = torch.FloatTensor(rand_mat).to(device)
noise_tnr = torch.mm(mod_tnr, rand_tnr)
noise_list.append(noise_tnr)
# ==========
gnd_list = []
real_sup_list = []
for t in range(tau-win_size+1, tau+1):
# ==========
edges = edge_seq[t]
gnd = get_adj_wei(edges, num_nodes, max_thres)
gnd_norm = gnd/max_thres # Normalize the edge weights to [0, 1]
gnd_norm += np.eye(num_nodes)
gnd_tnr = torch.FloatTensor(gnd_norm).to(device)
gnd_list.append(gnd_tnr)
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup_woSE(gnd_norm)
sup_tnr = torch.FloatTensor(sup).to(device)
real_sup_list.append(sup_tnr)
# ====================
# Train the generator
adj_est_list = gen_net(sup_list, feat_list, noise_list, align_list, num_nodes_list, lambd, pred_flag=False)
pre_gen_loss = get_pre_gen_loss(adj_est_list, gnd_list, theta)
pre_gen_opt.zero_grad()
pre_gen_loss.backward()
pre_gen_opt.step()
# ====================
gen_loss_list.append(pre_gen_loss.item())
train_cnt += 1
if train_cnt % 100 == 0:
print('-Train %d / %d' % (train_cnt, num_train_snaps))
gen_loss_mean = np.mean(gen_loss_list)
print('#%d Pre-Train G-Loss %f' % (epoch, gen_loss_mean))
# ====================
# Validate the model
gen_net.eval()
disc_net.eval()
# ==========
RMSE_list = []
MAE_list = []
MLSD_list = []
MR_list = []
for tau in range(num_snaps-num_test_snaps-num_val_snaps, num_snaps-num_test_snaps):
# ====================
sup_list = [] # List of GNN support (tensor)
noise_list = [] # List of random noise inputs
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
adj = get_adj_wei(edges, num_nodes, max_thres)
adj_norm = adj/max_thres # Normalize the edge weights to [0, 1]
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup(adj_norm)
sup_sp = sp.sparse.coo_matrix(sup)
sup_sp = sparse_to_tuple(sup_sp)
idxs = torch.LongTensor(sup_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_sp[1]).to(device)
sup_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_sp[2]).float().to(device)
sup_list.append(sup_tnr)
# =========
# Generate the random noise via random projection
mod_tnr = torch.FloatTensor(mod_seq[t]).to(device)
rand_mat = rand_proj(num_nodes, noise_dim)
rand_tnr = torch.FloatTensor(rand_mat).to(device)
noise_tnr = torch.mm(mod_tnr, rand_tnr)
noise_list.append(noise_tnr)
# ==========
# Get the prediction result
adj_est_list = gen_net(sup_list, feat_list, noise_list, align_list, num_nodes_list, lambd, pred_flag=True)
adj_est = adj_est_list[-1]
if torch.cuda.is_available():
adj_est = adj_est.cpu().data.numpy()
else:
adj_est = adj_est.data.numpy()
adj_est *= max_thres # Rescale the edge weights to the original value range
# ==========
# Refine the prediction result
for r in range(num_nodes):
adj_est[r, r] = 0
for r in range(num_nodes):
for c in range(num_nodes):
if adj_est[r, c]<=epsilon:
adj_est[r, c] = 0
# ====================
# Get the ground-truth
edges = edge_seq[tau]
gnd = get_adj_wei(edges, num_nodes, max_thres)
# ====================
# Evaluate the prediction result
RMSE = get_RMSE(adj_est, gnd, num_nodes)
MAE = get_MAE(adj_est, gnd, num_nodes)
MLSD = get_MLSD(adj_est, gnd, num_nodes)
MR = get_MR(adj_est, gnd, num_nodes)
# ==========
RMSE_list.append(RMSE)
MAE_list.append(MAE)
MLSD_list.append(MLSD)
MR_list.append(MR)
# ====================
RMSE_mean = np.mean(RMSE_list)
RMSE_std = np.std(RMSE_list, ddof=1)
MAE_mean = np.mean(MAE_list)
MAE_std = np.std(MAE_list, ddof=1)
MLSD_mean = np.mean(MLSD_list)
MLSD_std = np.std(MLSD_list, ddof=1)
MR_mean = np.mean(MR_list)
MR_std = np.std(MR_list, ddof=1)
print('Val Pre-#%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
# ==========
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('Val Pre #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
f_input.close()
# ====================
# Test the model
gen_net.eval()
disc_net.eval()
# ==========
RMSE_list = []
MAE_list = []
MLSD_list = []
MR_list = []
for tau in range(num_snaps-num_test_snaps, num_snaps):
# ====================
sup_list = [] # List of GNN support (tensor)
noise_list = [] # List of random noise inputs
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
adj = get_adj_wei(edges, num_nodes, max_thres)
adj_norm = adj/max_thres # Normalize the edge weights to [0, 1]
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup(adj_norm)
sup_sp = sp.sparse.coo_matrix(sup)
sup_sp = sparse_to_tuple(sup_sp)
idxs = torch.LongTensor(sup_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_sp[1]).to(device)
sup_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_sp[2]).float().to(device)
sup_list.append(sup_tnr)
# =========
# Generate the random noise via random projection
mod_tnr = torch.FloatTensor(mod_seq[t]).to(device)
rand_mat = rand_proj(num_nodes, noise_dim)
rand_tnr = torch.FloatTensor(rand_mat).to(device)
noise_tnr = torch.mm(mod_tnr, rand_tnr)
noise_list.append(noise_tnr)
# ==========
# Get the prediction result
adj_est_list = gen_net(sup_list, feat_list, noise_list, align_list, num_nodes_list, lambd, pred_flag=True)
adj_est = adj_est_list[-1]
if torch.cuda.is_available():
adj_est = adj_est.cpu().data.numpy()
else:
adj_est = adj_est.data.numpy()
adj_est *= max_thres # Rescale the edge weights to the original value range
# ==========
# Refine the prediction result
for r in range(num_nodes):
adj_est[r, r] = 0
for r in range(num_nodes):
for c in range(num_nodes):
if adj_est[r, c]<=epsilon:
adj_est[r, c] = 0
# ====================
# Get the ground-truth
edges = edge_seq[tau]
gnd = get_adj_wei(edges, num_nodes, max_thres)
# ====================
# Evaluate the prediction result
RMSE = get_RMSE(adj_est, gnd, num_nodes)
MAE = get_MAE(adj_est, gnd, num_nodes)
MLSD = get_MLSD(adj_est, gnd, num_nodes)
MR = get_MR(adj_est, gnd, num_nodes)
# ==========
RMSE_list.append(RMSE)
MAE_list.append(MAE)
MLSD_list.append(MLSD)
MR_list.append(MR)
# ====================
RMSE_mean = np.mean(RMSE_list)
RMSE_std = np.std(RMSE_list, ddof=1)
MAE_mean = np.mean(MAE_list)
MAE_std = np.std(MAE_list, ddof=1)
MLSD_mean = np.mean(MLSD_list)
MLSD_std = np.std(MLSD_list, ddof=1)
MR_mean = np.mean(MR_list)
MR_std = np.std(MR_list, ddof=1)
print('Test Pre-#%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
# ==========
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('Test Pre #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
f_input.write('\n')
f_input.close()
# ====================
# Joint optimization of the generator & discriminator
for epoch in range(num_epochs):
# ====================
# Train the model
gen_net.train()
disc_net.train()
# ==========
train_cnt = 0
disc_loss_list = []
gen_loss_list = []
for tau in range(win_size, num_train_snaps):
# ====================
sup_list = [] # List of GNN support (tensor)
noise_list = [] # List of random noise inputs
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
adj = get_adj_wei(edges, num_nodes, max_thres)
adj_norm = adj/max_thres # Normalize the edge weights to [0, 1]
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup(adj_norm)
sup_sp = sp.sparse.coo_matrix(sup)
sup_sp = sparse_to_tuple(sup_sp)
idxs = torch.LongTensor(sup_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_sp[1]).to(device)
sup_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_sp[2]).float().to(device)
sup_list.append(sup_tnr)
# =========
# Generate the random noise via random projection
mod_tnr = torch.FloatTensor(mod_seq[t]).to(device)
rand_mat = rand_proj(num_nodes, noise_dim)
rand_tnr = torch.FloatTensor(rand_mat).to(device)
noise_tnr = torch.mm(mod_tnr, rand_tnr)
noise_list.append(noise_tnr)
# ==========
gnd_list = []
real_sup_list = []
for t in range(tau-win_size+1, tau+1):
# ==========
edges = edge_seq[t]
gnd = get_adj_wei(edges, num_nodes, max_thres)
gnd_norm = gnd/max_thres # Normalize the edge weights to [0, 1]
gnd_norm += np.eye(num_nodes)
gnd_tnr = torch.FloatTensor(gnd_norm).to(device)
gnd_list.append(gnd_tnr)
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup_woSE(gnd_norm)
sup_tnr = torch.FloatTensor(sup).to(device)
real_sup_list.append(sup_tnr)
# ====================
# Train the discriminator
adj_est_list = gen_net(sup_list, feat_list, noise_list, align_list, num_nodes_list, lambd, pred_flag=False)
disc_real_list, disc_fake_list = disc_net(real_sup_list, feat_list, adj_est_list, feat_list)
disc_loss = get_disc_loss(disc_real_list, disc_fake_list, theta)
disc_opt.zero_grad()
disc_loss.backward()
disc_opt.step()
# ==========
# Train the generator
adj_est_list = gen_net(sup_list, feat_list, noise_list, align_list, num_nodes_list, lambd, pred_flag=False)
_, disc_fake_list = disc_net(real_sup_list, feat_list, adj_est_list, feat_list)
gen_loss = get_gen_loss(adj_est_list, gnd_list, disc_fake_list, max_thres, alpha, beta, theta)
gen_opt.zero_grad()
gen_loss.backward()
gen_opt.step()
# ====================
gen_loss_list.append(gen_loss.item())
disc_loss_list.append(disc_loss.item())
train_cnt += 1
if train_cnt % 100 == 0:
print('-Train %d / %d' % (train_cnt, num_train_snaps))
gen_loss_mean = np.mean(gen_loss_list)
disc_loss_mean = np.mean(disc_loss_list)
print('#%d Train G-Loss %f D-Loss %f' % (epoch, gen_loss_mean, disc_loss_mean))
# ====================
# Save the trained model (w.r.t. current epoch)
if save_flag:
torch.save(gen_net, 'pt/IDEA_gen_%d.pkl' % (epoch))
torch.save(disc_net, 'pt/IDEA_disc_%d.pkl' % (epoch))
# ====================
# Validate the model
gen_net.eval()
disc_net.eval()
# ==========
RMSE_list = []
MAE_list = []
MLSD_list = []
MR_list = []
for tau in range(num_snaps-num_test_snaps-num_val_snaps, num_snaps-num_test_snaps):
# ====================
sup_list = [] # List of GNN support (tensor)
noise_list = [] # List of random noise inputs
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
adj = get_adj_wei(edges, num_nodes, max_thres)
adj_norm = adj/max_thres # Normalize the edge weights to [0, 1]
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup(adj_norm)
sup_sp = sp.sparse.coo_matrix(sup)
sup_sp = sparse_to_tuple(sup_sp)
idxs = torch.LongTensor(sup_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_sp[1]).to(device)
sup_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_sp[2]).float().to(device)
sup_list.append(sup_tnr)
# =========
# Generate the random noise via random projection
mod_tnr = torch.FloatTensor(mod_seq[t]).to(device)
rand_mat = rand_proj(num_nodes, noise_dim)
rand_tnr = torch.FloatTensor(rand_mat).to(device)
noise_tnr = torch.mm(mod_tnr, rand_tnr)
noise_list.append(noise_tnr)
# ==========
# Get the prediction result
adj_est_list = gen_net(sup_list, feat_list, noise_list, align_list, num_nodes_list, lambd, pred_flag=True)
adj_est = adj_est_list[-1]
if torch.cuda.is_available():
adj_est = adj_est.cpu().data.numpy()
else:
adj_est = adj_est.data.numpy()
adj_est *= max_thres # Rescale the edge weights to the original value range
# ==========
# Refine the prediction result
for r in range(num_nodes):
adj_est[r, r] = 0
for r in range(num_nodes):
for c in range(num_nodes):
if adj_est[r, c]<=epsilon:
adj_est[r, c] = 0
# ====================
# Get the ground-truth
edges = edge_seq[tau]
gnd = get_adj_wei(edges, num_nodes, max_thres)
# ====================
# Evaluate the prediction result
RMSE = get_RMSE(adj_est, gnd, num_nodes)
MAE = get_MAE(adj_est, gnd, num_nodes)
MLSD = get_MLSD(adj_est, gnd, num_nodes)
MR = get_MR(adj_est, gnd, num_nodes)
# ==========
RMSE_list.append(RMSE)
MAE_list.append(MAE)
MLSD_list.append(MLSD)
MR_list.append(MR)
# ====================
RMSE_mean = np.mean(RMSE_list)
RMSE_std = np.std(RMSE_list, ddof=1)
MAE_mean = np.mean(MAE_list)
MAE_std = np.std(MAE_list, ddof=1)
MLSD_mean = np.mean(MLSD_list)
MLSD_std = np.std(MLSD_list, ddof=1)
MR_mean = np.mean(MR_list)
MR_std = np.std(MR_list, ddof=1)
print('Val #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
# ==========
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('Val #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
f_input.close()
# ====================
# Test the model
gen_net.eval()
disc_net.eval()
# ==========
RMSE_list = []
MAE_list = []
MLSD_list = []
MR_list = []
for tau in range(num_snaps-num_test_snaps, num_snaps):
# ====================
sup_list = [] # List of GNN support (tensor)
noise_list = [] # List of random noise inputs
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
adj = get_adj_wei(edges, num_nodes, max_thres)
adj_norm = adj/max_thres # Normalize the edge weights to [0, 1]
# ==========
# Transfer the GNN support to a sparse tensor
sup = get_gnn_sup(adj_norm)
sup_sp = sp.sparse.coo_matrix(sup)
sup_sp = sparse_to_tuple(sup_sp)
idxs = torch.LongTensor(sup_sp[0].astype(float)).to(device)
vals = torch.FloatTensor(sup_sp[1]).to(device)
sup_tnr = torch.sparse.FloatTensor(idxs.t(), vals, sup_sp[2]).float().to(device)
sup_list.append(sup_tnr)
# =========
# Generate the random noise via random projection
mod_tnr = torch.FloatTensor(mod_seq[t]).to(device)
rand_mat = rand_proj(num_nodes, noise_dim)
rand_tnr = torch.FloatTensor(rand_mat).to(device)
noise_tnr = torch.mm(mod_tnr, rand_tnr)
noise_list.append(noise_tnr)
# ==========
# Get the prediction result
adj_est_list = gen_net(sup_list, feat_list, noise_list, align_list, num_nodes_list, lambd, pred_flag=True)
adj_est = adj_est_list[-1]
if torch.cuda.is_available():
adj_est = adj_est.cpu().data.numpy()
else:
adj_est = adj_est.data.numpy()
adj_est *= max_thres # Rescale the edge weights to the original value range
# ==========
# Refine the prediction result
for r in range(num_nodes):
adj_est[r, r] = 0
for r in range(num_nodes):
for c in range(num_nodes):
if adj_est[r, c] <= epsilon:
adj_est[r, c] = 0
# ====================
# Get the ground-truth
edges = edge_seq[tau]
gnd = get_adj_wei(edges, num_nodes, max_thres)
# ====================
# Evaluate the prediction result
RMSE = get_RMSE(adj_est, gnd, num_nodes)
MAE = get_MAE(adj_est, gnd, num_nodes)
MLSD = get_MLSD(adj_est, gnd, num_nodes)
MR = get_MR(adj_est, gnd, num_nodes)
# ==========
RMSE_list.append(RMSE)
MAE_list.append(MAE)
MLSD_list.append(MLSD)
MR_list.append(MR)
# ====================
RMSE_mean = np.mean(RMSE_list)
RMSE_std = np.std(RMSE_list, ddof=1)
MAE_mean = np.mean(MAE_list)
MAE_std = np.std(MAE_list, ddof=1)
MLSD_mean = np.mean(MLSD_list)
MLSD_std = np.std(MLSD_list, ddof=1)
MR_mean = np.mean(MR_list)
MR_std = np.std(MR_list, ddof=1)
print('Test #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
# ==========
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('Test #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean, RMSE_std, MAE_mean, MAE_std, MLSD_mean, MLSD_std, MR_mean, MR_std))
f_input.write('\n')
f_input.close()