-
Notifications
You must be signed in to change notification settings - Fork 2
/
IDEA_WIDE_demo.py
895 lines (866 loc) · 37.5 KB
/
IDEA_WIDE_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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
import torch
import torch.optim as optim
from modules.IDEA import GenNet_exp
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 = 'WIDE'
num_nodes_gbl = 422614 # Number of accumulated nodes
num_snaps = 800 # Number of snapshots
max_thres = 512 # Threshold for maximum edge weight
noise_dim = 512 # Dimensionality of noise input
feat_dim = 32 # Dimensionality of node feature
pos_dim = 256 # Dimensionality of positional embedding
GNN_feat_dim = feat_dim+pos_dim
FEM_dims = [GNN_feat_dim, 256, 128] # Layer configuration of feature extraction module (FEM)
EDM_dims = [(FEM_dims[-1]+noise_dim), 512, 256] # Layer configuration of embedding derivation module (EDM)
EAM_dims = [(EDM_dims[-1]+FEM_dims[-1]), 256, 128] # Layer configuration of embedding aggregation module (EAM)
disc_dims = [FEM_dims[-1], 128, 64, 32] # 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.05 # Parameter to balance the SDM loss
lambd = 0.0 # Parameter of attentive aligning unit
theta = 0.1 # Decaying factor
# ====================
edge_seq = np.load('data/%s_edge_seq.npy' % (data_name), allow_pickle=True)
node_set_seq = np.load('data/%s_node_seq.npy' % (data_name), allow_pickle=True)
rand_seq = np.load('data/%s_rand_feat_seq.npy' % (data_name), allow_pickle=True)
align_seq_gbl = np.load('data/%s_align_seq.npy' % (data_name), allow_pickle=True)
# =========
node_map_seq_gbl = []
num_nodes_seq_gbl = []
for t in range(num_snaps):
node_set = node_set_seq[t]
node_map = get_node_map(node_set)
node_map_seq_gbl.append(node_map)
num_nodes_seq_gbl.append(len(node_set))
# ==========
# Get global node features
feat_gbl = np.load('data/%s_feat.npy' % (data_name), allow_pickle=True)
feat_lcl_seq = []
for t in range(num_snaps):
node_set = node_set_seq[t]
node_idxs = sorted(list(node_set))
feat_lcl = feat_gbl[node_idxs, :]
feat_lcl_seq.append(feat_lcl)
# ====================
dropout_rate = 0.0 # Dropout rate
win_size = 5 # Window size of historical snapshots
epsilon = 1e-5 # Threshold of the zero-refining
num_pre_epochs = 30 # Number of pre-training epochs
num_epochs = 100 # 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
# ====================
# Define the model
gen_net = GenNet_exp(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)
gen_opt = optim.Adam(gen_net.parameters(), lr=5e-4, weight_decay=1e-5)
disc_opt = optim.Adam(disc_net.parameters(), lr=5e-4, weight_decay=1e-5)
# ====================
# 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
align_list = [] # List of aligning matrices
feat_list = [] # List of node feature inputs
num_nodes_list = []
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
adj = get_adj_wei_map(edges, node_map, 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
noise_tnr = torch.FloatTensor(rand_seq[t][0]).to(device)
noise_list.append(noise_tnr)
# ==========
align_tnr = torch.FloatTensor(align_seq_gbl[t]).to(device)
align_list.append(align_tnr)
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
# ==========
gnd_list = []
real_sup_list = []
for t in range(tau-win_size+1, tau+1):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
gnd = get_adj_wei_map(edges, node_map, 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)
# ==========
if t==tau:
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
# ====================
# 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_L2 = []
MAE_list_L2 = []
MLSD_list_L2 = []
MR_list_L2 = []
# ==========
RMSE_list_L3 = []
MAE_list_L3 = []
MLSD_list_L3 = []
MR_list_L3 = []
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
align_list = [] # List of aligning matrices
feat_list = [] # List of node feature inputs
num_nodes_list = [] # List of #nodes in each snapshot
pre_node_map_list = []
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
adj = get_adj_wei_map(edges, node_map, 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
noise_tnr = torch.FloatTensor(rand_seq[t][0]).to(device)
noise_list.append(noise_tnr)
# ==========
align_tnr = torch.FloatTensor(align_seq_gbl[t]).to(device)
align_list.append(align_tnr)
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
pre_node_map_list.append(node_map)
# ====================
# Get the ground-truth
edges = edge_seq[tau]
# ==========
# For L3
num_nodes_L3 = num_nodes_seq_gbl[tau] # Number of nodes for L3
node_map_L3 = node_map_seq_gbl[tau]
gnd_L3 = get_adj_wei_map(edges, node_map_L3, num_nodes_L3, max_thres) # Ground-truth
feat_lcl = feat_lcl_seq[tau]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes_L3)
# ==========
# For L2
node_idxs_L2 = get_node_idxs_L2(pre_node_map_list, node_map_L3)
num_nodes_L2 = len(node_idxs_L2) # Number of nodes for L2
gnd_L2 = gnd_L3[node_idxs_L2, :]
gnd_L2 = gnd_L2[:, node_idxs_L2]
# ===================
# 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_L3 = adj_est_list[-1]
adj_est_L2 = adj_est_L3[node_idxs_L2, :]
adj_est_L2 = adj_est_L2[:, node_idxs_L2]
if torch.cuda.is_available():
adj_est_L2 = adj_est_L2.cpu().data.numpy()
adj_est_L3 = adj_est_L3.cpu().data.numpy()
else:
adj_est_L2 = adj_est_L2.data.numpy()
adj_est_L3 = adj_est_L3.data.numpy()
# Rescale the edge weights to the original value range
adj_est_L2 *= max_thres
adj_est_L3 *= max_thres
# ==========
# Refine the prediction result
for r in range(num_nodes_L3):
if r<num_nodes_L2:
adj_est_L2[r, r] = 0
adj_est_L3[r, r] = 0
for r in range(num_nodes_L3):
for c in range(num_nodes_L3):
if r<num_nodes_L2 and c<num_nodes_L2:
if adj_est_L2[r, c]<=epsilon:
adj_est_L2[r, c] = 0
if adj_est_L3[r, c]<=epsilon:
adj_est_L3[r, c] = 0
# ====================
# Evaluate the prediction result
RMSE_L2 = get_RMSE(adj_est_L2, gnd_L2, num_nodes_L2)
MAE_L2 = get_MAE(adj_est_L2, gnd_L2, num_nodes_L2)
MLSD_L2 = get_MLSD(adj_est_L2, gnd_L2, num_nodes_L2)
MR_L2 = get_MR(adj_est_L2, gnd_L2, num_nodes_L2)
# ==========
RMSE_list_L2.append(RMSE_L2)
MAE_list_L2.append(MAE_L2)
MLSD_list_L2.append(MLSD_L2)
MR_list_L2.append(MR_L2)
# ==========
RMSE_L3 = get_RMSE(adj_est_L3, gnd_L3, num_nodes_L3)
MAE_L3 = get_MAE(adj_est_L3, gnd_L3, num_nodes_L3)
MLSD_L3 = get_MLSD(adj_est_L3, gnd_L3, num_nodes_L3)
MR_L3 = get_MR(adj_est_L3, gnd_L3, num_nodes_L3)
# ==========
RMSE_list_L3.append(RMSE_L3)
MAE_list_L3.append(MAE_L3)
MLSD_list_L3.append(MLSD_L3)
MR_list_L3.append(MR_L3)
# ====================
RMSE_mean_L2 = np.mean(RMSE_list_L2)
RMSE_std_L2 = np.std(RMSE_list_L2, ddof=1)
MAE_mean_L2 = np.mean(MAE_list_L2)
MAE_std_L2 = np.std(MAE_list_L2, ddof=1)
MLSD_mean_L2 = np.mean(MLSD_list_L2)
MLSD_std_L2 = np.std(MLSD_list_L2, ddof=1)
MR_mean_L2 = np.mean(MR_list_L2)
MR_std_L2 = np.std(MR_list_L2, ddof=1)
print('(L2) Val Pre-#%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L2) Val Pre #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input.close()
# ==========
RMSE_mean_L3 = np.mean(RMSE_list_L3)
RMSE_std_L3 = np.std(RMSE_list_L3, ddof=1)
MAE_mean_L3 = np.mean(MAE_list_L3)
MAE_std_L3 = np.std(MAE_list_L3, ddof=1)
MLSD_mean_L3 = np.mean(MLSD_list_L3)
MLSD_std_L3 = np.std(MLSD_list_L3, ddof=1)
MR_mean_L3 = np.mean(MR_list_L3)
MR_std_L3 = np.std(MR_list_L3, ddof=1)
print('(L3) Val Pre-#%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L3) Val Pre #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
f_input.close()
# ====================
# Test the model
gen_net.eval()
disc_net.eval()
# ==========
RMSE_list_L2 = []
MAE_list_L2 = []
MLSD_list_L2 = []
MR_list_L2 = []
# ==========
RMSE_list_L3 = []
MAE_list_L3 = []
MLSD_list_L3 = []
MR_list_L3 = []
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
align_list = [] # List of aligning matrices
feat_list = [] # List of node feature inputs
num_nodes_list = [] # List of #nodes in each snapshot
pre_node_map_list = []
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
adj = get_adj_wei_map(edges, node_map, 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
noise_tnr = torch.FloatTensor(rand_seq[t][0]).to(device)
noise_list.append(noise_tnr)
# ==========
align_tnr = torch.FloatTensor(align_seq_gbl[t]).to(device)
align_list.append(align_tnr)
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
pre_node_map_list.append(node_map)
# ==========
# Get the ground-truth
edges = edge_seq[tau]
# ==========
# For L3
num_nodes_L3 = num_nodes_seq_gbl[tau] # Number of nodes for L3
node_map_L3 = node_map_seq_gbl[tau]
gnd_L3 = get_adj_wei_map(edges, node_map_L3, num_nodes_L3, max_thres) # Ground-truth
feat_lcl = feat_lcl_seq[tau]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes_L3)
# ==========
# For L2
node_idxs_L2 = get_node_idxs_L2(pre_node_map_list, node_map_L3)
num_nodes_L2 = len(node_idxs_L2) # Number of nodes for L2
gnd_L2 = gnd_L3[node_idxs_L2, :]
gnd_L2 = gnd_L2[:, node_idxs_L2]
# ====================
# 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_L3 = adj_est_list[-1]
adj_est_L2 = adj_est_L3[node_idxs_L2, :]
adj_est_L2 = adj_est_L2[:, node_idxs_L2]
if torch.cuda.is_available():
adj_est_L2 = adj_est_L2.cpu().data.numpy()
adj_est_L3 = adj_est_L3.cpu().data.numpy()
else:
adj_est_L2 = adj_est_L2.data.numpy()
adj_est_L3 = adj_est_L3.data.numpy()
# Rescale the edge weights to the original value range
adj_est_L2 *= max_thres
adj_est_L3 *= max_thres
# ==========
# Refine the prediction result
for r in range(num_nodes_L3):
if r<num_nodes_L2:
adj_est_L2[r, r] = 0
adj_est_L3[r, r] = 0
for r in range(num_nodes_L3):
for c in range(num_nodes_L3):
if r<num_nodes_L2 and c<num_nodes_L2:
if adj_est_L2[r, c]<=epsilon:
adj_est_L2[r, c] = 0
if adj_est_L3[r, c]<=epsilon:
adj_est_L3[r, c] = 0
# ====================
# Evaluate the prediction result
RMSE_L2 = get_RMSE(adj_est_L2, gnd_L2, num_nodes_L2)
MAE_L2 = get_MAE(adj_est_L2, gnd_L2, num_nodes_L2)
MLSD_L2 = get_MLSD(adj_est_L2, gnd_L2, num_nodes_L2)
MR_L2 = get_MR(adj_est_L2, gnd_L2, num_nodes_L2)
# ==========
RMSE_list_L2.append(RMSE_L2)
MAE_list_L2.append(MAE_L2)
MLSD_list_L2.append(MLSD_L2)
MR_list_L2.append(MR_L2)
# ==========
RMSE_L3 = get_RMSE(adj_est_L3, gnd_L3, num_nodes_L3)
MAE_L3 = get_MAE(adj_est_L3, gnd_L3, num_nodes_L3)
MLSD_L3 = get_MLSD(adj_est_L3, gnd_L3, num_nodes_L3)
MR_L3 = get_MR(adj_est_L3, gnd_L3, num_nodes_L3)
# ==========
RMSE_list_L3.append(RMSE_L3)
MAE_list_L3.append(MAE_L3)
MLSD_list_L3.append(MLSD_L3)
MR_list_L3.append(MR_L3)
# ====================
RMSE_mean_L2 = np.mean(RMSE_list_L2)
RMSE_std_L2 = np.std(RMSE_list_L2, ddof=1)
MAE_mean_L2 = np.mean(MAE_list_L2)
MAE_std_L2 = np.std(MAE_list_L2, ddof=1)
MLSD_mean_L2 = np.mean(MLSD_list_L2)
MLSD_std_L2 = np.std(MLSD_list_L2, ddof=1)
MR_mean_L2 = np.mean(MR_list_L2)
MR_std_L2 = np.std(MR_list_L2, ddof=1)
print('(L2) Test Pre-#%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L2) Test Pre #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input.close()
# ==========
RMSE_mean_L3 = np.mean(RMSE_list_L3)
RMSE_std_L3 = np.std(RMSE_list_L3, ddof=1)
MAE_mean_L3 = np.mean(MAE_list_L3)
MAE_std_L3 = np.std(MAE_list_L3, ddof=1)
MLSD_mean_L3 = np.mean(MLSD_list_L3)
MLSD_std_L3 = np.std(MLSD_list_L3, ddof=1)
MR_mean_L3 = np.mean(MR_list_L3)
MR_std_L3 = np.std(MR_list_L3, ddof=1)
print('(L3) Test Pre-#%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L3) Test Pre #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
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
gen_loss_list = []
disc_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
align_list = [] # List of aligning matrices
feat_list = [] # List of node feature inputs
num_nodes_list = [] # List of #nodes in each snapshot
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
adj = get_adj_wei_map(edges, node_map, 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
noise_tnr = torch.FloatTensor(rand_seq[t][0]).to(device)
noise_list.append(noise_tnr)
# ==========
align_tnr = torch.FloatTensor(align_seq_gbl[t]).to(device)
align_list.append(align_tnr)
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
# ==========
gnd_list = []
real_sup_list = []
for t in range(tau-win_size+1, tau+1):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
gnd = get_adj_wei_map(edges, node_map, 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)
# ==========
if t==tau:
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
# ====================
# 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[1:], adj_est_list, feat_list[1:])
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[1:], adj_est_list, feat_list[1:])
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/%s_IDEA_gen_%d.pkl' % (data_name, epoch))
torch.save(disc_net, 'pt/%s_IDEA_disc_%d.pkl' % (data_name, epoch))
# ====================
# Validate the model
gen_net.eval()
disc_net.eval()
# ==========
RMSE_list_L2 = []
MAE_list_L2 = []
MLSD_list_L2 = []
MR_list_L2 = []
# ==========
RMSE_list_L3 = []
MAE_list_L3 = []
MLSD_list_L3 = []
MR_list_L3 = []
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
align_list = [] # List of aligning matrices
feat_list = [] # List of node feature inputs
num_nodes_list = [] # List of #nodes in each snapshot
pre_node_map_list = []
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
adj = get_adj_wei_map(edges, node_map, 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
noise_tnr = torch.FloatTensor(rand_seq[t][0]).to(device)
noise_list.append(noise_tnr)
# ==========
align_tnr = torch.FloatTensor(align_seq_gbl[t]).to(device)
align_list.append(align_tnr)
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
pre_node_map_list.append(node_map)
# ==========
# Get the ground-truth
edges = edge_seq[tau]
# ==========
# For L3
num_nodes_L3 = num_nodes_seq_gbl[tau] # Number of nodes for L3
node_map_L3 = node_map_seq_gbl[tau]
gnd_L3 = get_adj_wei_map(edges, node_map_L3, num_nodes_L3, max_thres) # Ground-truth
feat_lcl = feat_lcl_seq[tau]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes_L3)
# ==========
# For L2
node_idxs_L2 = get_node_idxs_L2(pre_node_map_list, node_map_L3)
num_nodes_L2 = len(node_idxs_L2) # Number of nodes for L2
gnd_L2 = gnd_L3[node_idxs_L2, :]
gnd_L2 = gnd_L2[:, node_idxs_L2]
# ==========
# 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_L3 = adj_est_list[-1]
adj_est_L2 = adj_est_L3[node_idxs_L2, :]
adj_est_L2 = adj_est_L2[:, node_idxs_L2]
if torch.cuda.is_available():
adj_est_L2 = adj_est_L2.cpu().data.numpy()
adj_est_L3 = adj_est_L3.cpu().data.numpy()
else:
adj_est_L2 = adj_est_L2.data.numpy()
adj_est_L3 = adj_est_L3.data.numpy()
# Rescale the edge weights to the original value range
adj_est_L2 *= max_thres
adj_est_L3 *= max_thres
# ==========
# Refine the prediction result
for r in range(num_nodes_L3):
if r<num_nodes_L2:
adj_est_L2[r, r] = 0
adj_est_L3[r, r] = 0
for r in range(num_nodes_L3):
for c in range(num_nodes_L3):
if r<num_nodes_L2 and c<num_nodes_L2:
if adj_est_L2[r, c]<=epsilon:
adj_est_L2[r, c] = 0
if adj_est_L3[r, c]<=epsilon:
adj_est_L3[r, c] = 0
# ====================
# Evaluate the prediction result
RMSE_L2 = get_RMSE(adj_est_L2, gnd_L2, num_nodes_L2)
MAE_L2 = get_MAE(adj_est_L2, gnd_L2, num_nodes_L2)
MLSD_L2 = get_MLSD(adj_est_L2, gnd_L2, num_nodes_L2)
MR_L2 = get_MR(adj_est_L2, gnd_L2, num_nodes_L2)
# ==========
RMSE_list_L2.append(RMSE_L2)
MAE_list_L2.append(MAE_L2)
MLSD_list_L2.append(MLSD_L2)
MR_list_L2.append(MR_L2)
# ==========
RMSE_L3 = get_RMSE(adj_est_L3, gnd_L3, num_nodes_L3)
MAE_L3 = get_MAE(adj_est_L3, gnd_L3, num_nodes_L3)
MLSD_L3 = get_MLSD(adj_est_L3, gnd_L3, num_nodes_L3)
MR_L3 = get_MR(adj_est_L3, gnd_L3, num_nodes_L3)
# ==========
RMSE_list_L3.append(RMSE_L3)
MAE_list_L3.append(MAE_L3)
MLSD_list_L3.append(MLSD_L3)
MR_list_L3.append(MR_L3)
# ====================
RMSE_mean_L2 = np.mean(RMSE_list_L2)
RMSE_std_L2 = np.std(RMSE_list_L2, ddof=1)
MAE_mean_L2 = np.mean(MAE_list_L2)
MAE_std_L2 = np.std(MAE_list_L2, ddof=1)
MLSD_mean_L2 = np.mean(MLSD_list_L2)
MLSD_std_L2 = np.std(MLSD_list_L2, ddof=1)
MR_mean_L2 = np.mean(MR_list_L2)
MR_std_L2 = np.std(MR_list_L2, ddof=1)
print('(L2) Val #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L2) Val #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input.close()
# ==========
RMSE_mean_L3 = np.mean(RMSE_list_L3)
RMSE_std_L3 = np.std(RMSE_list_L3, ddof=1)
MAE_mean_L3 = np.mean(MAE_list_L3)
MAE_std_L3 = np.std(MAE_list_L3, ddof=1)
MLSD_mean_L3 = np.mean(MLSD_list_L3)
MLSD_std_L3 = np.std(MLSD_list_L3, ddof=1)
MR_mean_L3 = np.mean(MR_list_L3)
MR_std_L3 = np.std(MR_list_L3, ddof=1)
print('(L3) Val #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L3) Val #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
f_input.close()
# ====================
# Test the model
gen_net.eval()
disc_net.eval()
# ==========
RMSE_list_L2 = []
MAE_list_L2 = []
MLSD_list_L2 = []
MR_list_L2 = []
# ==========
RMSE_list_L3 = []
MAE_list_L3 = []
MLSD_list_L3 = []
MR_list_L3 = []
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
align_list = [] # List of aligning matrices
feat_list = [] # List of node feature inputs
num_nodes_list = [] # List of #nodes in each snapshot
pre_node_map_list = []
for t in range(tau-win_size, tau):
# ==========
edges = edge_seq[t]
num_nodes = num_nodes_seq_gbl[t]
node_map = node_map_seq_gbl[t]
adj = get_adj_wei_map(edges, node_map, 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
noise_tnr = torch.FloatTensor(rand_seq[t][0]).to(device)
noise_list.append(noise_tnr)
# ==========
align_tnr = torch.FloatTensor(align_seq_gbl[t]).to(device)
align_list.append(align_tnr)
feat_lcl = feat_lcl_seq[t]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes)
pre_node_map_list.append(node_map)
# ====================
# Get the ground-truth
edges = edge_seq[tau]
# ==========
# For L3
num_nodes_L3 = num_nodes_seq_gbl[tau] # Number of nodes for L3
node_map_L3 = node_map_seq_gbl[tau]
gnd_L3 = get_adj_wei_map(edges, node_map_L3, num_nodes_L3, max_thres) # Ground-truth
feat_lcl = feat_lcl_seq[tau]
feat_tnr = torch.FloatTensor(feat_lcl).to(device)
feat_list.append(feat_tnr)
num_nodes_list.append(num_nodes_L3)
# ==========
# For L2
node_idxs_L2 = get_node_idxs_L2(pre_node_map_list, node_map_L3)
num_nodes_L2 = len(node_idxs_L2) # Number of nodes for L2
gnd_L2 = gnd_L3[node_idxs_L2, :]
gnd_L2 = gnd_L2[:, node_idxs_L2]
# ====================
# 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_L3 = adj_est_list[-1]
adj_est_L2 = adj_est_L3[node_idxs_L2, :]
adj_est_L2 = adj_est_L2[:, node_idxs_L2]
if torch.cuda.is_available():
adj_est_L2 = adj_est_L2.cpu().data.numpy()
adj_est_L3 = adj_est_L3.cpu().data.numpy()
else:
adj_est_L2 = adj_est_L2.data.numpy()
adj_est_L3 = adj_est_L3.data.numpy()
# Rescale the edge weights to the original value range
adj_est_L2 *= max_thres
adj_est_L3 *= max_thres
# ==========
# Refine the prediction result
for r in range(num_nodes_L3):
if r<num_nodes_L2:
adj_est_L2[r, r] = 0
adj_est_L3[r, r] = 0
for r in range(num_nodes_L3):
for c in range(num_nodes_L3):
if r<num_nodes_L2 and c<num_nodes_L2:
if adj_est_L2[r, c]<=epsilon:
adj_est_L2[r, c] = 0
if adj_est_L3[r, c]<=epsilon:
adj_est_L3[r, c] = 0
# ====================
# Evaluate the prediction result
RMSE_L2 = get_RMSE(adj_est_L2, gnd_L2, num_nodes_L2)
MAE_L2 = get_MAE(adj_est_L2, gnd_L2, num_nodes_L2)
MLSD_L2 = get_MLSD(adj_est_L2, gnd_L2, num_nodes_L2)
MR_L2 = get_MR(adj_est_L2, gnd_L2, num_nodes_L2)
# ==========
RMSE_list_L2.append(RMSE_L2)
MAE_list_L2.append(MAE_L2)
MLSD_list_L2.append(MLSD_L2)
MR_list_L2.append(MR_L2)
# ==========
RMSE_L3 = get_RMSE(adj_est_L3, gnd_L3, num_nodes_L3)
MAE_L3 = get_MAE(adj_est_L3, gnd_L3, num_nodes_L3)
MLSD_L3 = get_MLSD(adj_est_L3, gnd_L3, num_nodes_L3)
MR_L3 = get_MR(adj_est_L3, gnd_L3, num_nodes_L3)
# ==========
RMSE_list_L3.append(RMSE_L3)
MAE_list_L3.append(MAE_L3)
MLSD_list_L3.append(MLSD_L3)
MR_list_L3.append(MR_L3)
# ====================
RMSE_mean_L2 = np.mean(RMSE_list_L2)
RMSE_std_L2 = np.std(RMSE_list_L2, ddof=1)
MAE_mean_L2 = np.mean(MAE_list_L2)
MAE_std_L2 = np.std(MAE_list_L2, ddof=1)
MLSD_mean_L2 = np.mean(MLSD_list_L2)
MLSD_std_L2 = np.std(MLSD_list_L2, ddof=1)
MR_mean_L2 = np.mean(MR_list_L2)
MR_std_L2 = np.std(MR_list_L2, ddof=1)
print('(L2) Test #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L2) Test #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L2, RMSE_std_L2, MAE_mean_L2, MAE_std_L2,
MLSD_mean_L2, MLSD_std_L2, MR_mean_L2, MR_std_L2))
f_input.close()
# ==========
RMSE_mean_L3 = np.mean(RMSE_list_L3)
RMSE_std_L3 = np.std(RMSE_list_L3, ddof=1)
MAE_mean_L3 = np.mean(MAE_list_L3)
MAE_std_L3 = np.std(MAE_list_L3, ddof=1)
MLSD_mean_L3 = np.mean(MLSD_list_L3)
MLSD_std_L3 = np.std(MLSD_list_L3, ddof=1)
MR_mean_L3 = np.mean(MR_list_L3)
MR_std_L3 = np.std(MR_list_L3, ddof=1)
print('(L3) Test #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
f_input = open('res/%s_IDEA_rec.txt' % (data_name), 'a+')
f_input.write('(L3) Test #%d RMSE %f %f MAE %f %f MLSD %f %f MR %f %f\n'
% (epoch, RMSE_mean_L3, RMSE_std_L3, MAE_mean_L3, MAE_std_L3,
MLSD_mean_L3, MLSD_std_L3, MR_mean_L3, MR_std_L3))
f_input.write('\n')
f_input.close()