-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
1089 lines (786 loc) · 43.6 KB
/
model.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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam
from sklearn.mixture import GaussianMixture
from sklearn.metrics import accuracy_score
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import os
from tqdm import tqdm
from layers import GraphConvolution, GraphConvolutionSparse, Linear, InnerDecoder, InnerProductDecoder,SpGAT,GAT
from utils import cluster_acc,clustering_evaluation
from utils_smiles import *
from estimators import estimate_mutual_information
from collections import Counter
class GCNModelAE(nn.Module):
def __init__(self, input_feat_dim, n_nodes, hidden_dim1, hidden_dim2, dropout,args,device=torch.device('cpu')):
super(GCNModelAE, self).__init__()
self.device = device
self.args = args
if self.args.encoder == 'gcn':
self.gc1 = GraphConvolutionSparse(input_feat_dim, hidden_dim1, dropout, act=torch.relu)
self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
if self.args.encoder == 'gat':
self.gc1 = SpGAT(input_feat_dim,hidden_dim1,hidden_dim1,dropout,alpha=0.2,nheads=5)
self.gc2 = SpGAT(hidden_dim1,hidden_dim2,hidden_dim2,dropout,alpha=0.2,nheads=5)
# self.gc3 = SpGAT(hidden_dim1,hidden_dim2,hidden_dim2,dropout,alpha=0.2,nheads=5)
# self.gc1 = GraphConvolutionSparse(input_feat_dim, hidden_dim1, dropout, act=torch.relu)
# self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
self.dc = InnerProductDecoder(dropout, act=lambda x: x)
# self.dc = InnerDecoder(dropout, act=lambda x: x)
def encoder(self, x, adj):
hidden1 = self.gc1(x, adj)
return self.gc2(hidden1, adj)
def decoder(self,z):
return self.dc(z)
def forward(self, x, adj):
z = self.encoder(x,adj)
return self.dc(z),z
def loss(self,x,adj,labels, n_nodes, n_features, norm, pos_weight,L=1):
z = self.encoder(x,adj)
pred_adj = self.decoder(z)
cost = norm * F.binary_cross_entropy_with_logits(pred_adj, labels,pos_weight = pos_weight)
return cost,
def check_parameters(self):
for name, param in self.named_parameters():
if param.requires_grad:
print(name, param.data,param.data.shape)
class GCNModelVAE(nn.Module):
def __init__(self, input_feat_dim, n_nodes, hidden_dim1, hidden_dim2, dropout,args):
super(GCNModelVAE, self).__init__()
self.args = args
self.gc1 = GraphConvolutionSparse(input_feat_dim, hidden_dim1, dropout, act=torch.relu)
self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
self.gc3 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
self.dc = InnerProductDecoder(dropout, act=lambda x: x)
# self.dc = InnerDecoder(dropout, act=lambda x: x)
def encoder(self, x, adj):
hidden1 = self.gc1(x, adj)
return self.gc2(hidden1, adj), self.gc3(hidden1, adj)
def decoder(self,mu,logvar):
z = self.reparameterize(mu, logvar)
return self.dc(z)
def reparameterize(self, mu, logvar):
std = torch.exp(logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(mu)
def forward(self, x, adj):
mu, logvar = self.encoder(x, adj)
z = self.reparameterize(mu, logvar)
# z_a = self.reparameterize(mu_a,logvar_a)
return self.dc(z), z
def loss(self,x,adj,labels, n_nodes, n_features, norm, pos_weight,L=1):
det=1e-10
norm_u = norm
pos_weight_u= pos_weight
L_rec_u=0
mu, logvar = self.encoder(x, adj)
# z_mu, z_sigma2_log = self.encoder(x)
for l in range(L):
pred_adj = self.decoder(mu,logvar)
cost_u = norm * F.binary_cross_entropy_with_logits(pred_adj, labels ,pos_weight = pos_weight)
L_rec_u += cost_u
L_rec_u/=L
KLD = -0.5 / n_nodes * torch.mean(torch.sum(-1 - 2 * logvar + mu.pow(2) + logvar.exp().pow(2),1))
return L_rec_u, -KLD
def check_parameters(self):
for name, param in self.named_parameters():
if param.requires_grad:
print(name, param.data,param.data.shape)
class GCNModelVAECD(nn.Module):
def __init__(self, input_feat_dim, n_nodes, hidden_dim1, hidden_dim2, dropout,args):
super(GCNModelVAECD, self).__init__()
self.args = args
self.gc1 = GraphConvolutionSparse(input_feat_dim, hidden_dim1, dropout, act=torch.relu)
self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
self.gc3 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
self.dc = InnerProductDecoder(dropout, act=lambda x: x)
self.pi_=nn.Parameter(torch.FloatTensor(args.nClusters,).fill_(1)/args.nClusters,requires_grad=True)
self.mu_c=nn.Parameter(torch.randn(args.nClusters,hidden_dim2),requires_grad=True)
self.log_sigma2_c=nn.Parameter(torch.randn(args.nClusters,hidden_dim2),requires_grad=True)
def encoder(self, x, adj):
hidden1 = self.gc1(x, adj)
return self.gc2(hidden1, adj), self.gc3(hidden1, adj)
def decoder(self,mu,logvar):
z_u = self.reparameterize(mu, logvar)
return self.dc(z_u)
def reparameterize(self, mu, logvar):
if self.training:
std = torch.exp(logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(mu)
else:
return mu
def forward(self, x, adj):
mu, logvar = self.encoder(x, adj)
z_u = self.reparameterize(mu, logvar)
return self.dc(z_u),z_u
def loss(self,x,adj,labels, n_nodes, n_features, norm, pos_weight,L=1):
det=1e-10
norm_u = norm
pos_weight_u= pos_weight
L_rec_u=0
mu, logvar = self.encoder(x, adj)
hidden_dim2 = mu.shape[1]
# z_mu, z_sigma2_log = self.encoder(x)
for l in range(L):
# z=torch.randn_like(z_mu)*torch.exp(z_sigma2_log/2)+z_mu
pred_adj = self.decoder(mu,logvar)
# L_rec+=F.binary_cross_entropy(x_pro,x)
cost_u = norm * F.binary_cross_entropy_with_logits(pred_adj, labels ,pos_weight = pos_weight)
L_rec_u += cost_u
# L_rec_a += cost_a
L_rec_u/=L
self.pi_.data = (self.pi_/self.pi_.sum()).data
z = self.reparameterize(mu,logvar)
gamma_c=torch.exp(torch.log(self.pi_.unsqueeze(0))+self.gaussian_pdfs_log(z,self.mu_c,self.log_sigma2_c))+det
gamma_c = F.softmax(gamma_c) # is softmax a good way?
gamma_c=gamma_c/(gamma_c.sum(1).view(-1,1)) #shape: batch_size*Clusters
self.pi_.data = gamma_c.mean(0).data # prior need to be re-normalized? In GMM, prior is based on gamma_c:https://brilliant.org/wiki/gaussian-mixture-model/
KLD_u_c=-(0.5/n_nodes)*torch.mean(torch.sum(gamma_c*torch.sum(-1+self.log_sigma2_c.unsqueeze(0)-2*logvar.unsqueeze(1)+
torch.exp(2*logvar.unsqueeze(1)-self.log_sigma2_c.unsqueeze(0))+
(mu.unsqueeze(1)-self.mu_c.unsqueeze(0)).pow(2)/torch.exp(self.log_sigma2_c.unsqueeze(0)),2),1))
gamma_loss = -(1 / self.args.nClusters) * torch.mean(torch.sum(gamma_c*torch.log(gamma_c/self.pi_.unsqueeze(0)),1))
return L_rec_u, -KLD_u_c-gamma_loss
def pre_train(self,x,adj,Y,pre_epoch=50):
'''
This function is used to initialize cluster paramters: pi_, mu_c, log_sigma2_c.
-------------
paramters:
x: is the feature matrix of graph G.
adj: is the adjacent matrix of graph G.
Y: is the class label for each node in graph G.
'''
if not os.path.exists('./pretrain_model_{}.pk'.format(self.args.dataset)):
Loss=nn.MSELoss()
opti=Adam(self.parameters()) #all paramters in model
print('Pretraining......')
# epoch_bar=tqdm(range(pre_epoch))
# for _ in epoch_bar:
for _ in range(pre_epoch):
self.train()
L=0
mu, logvar = self.encoder(x,adj)
pred_adj = self.decoder(mu,logvar)
loss= Loss(pred_adj,adj.to_dense())
L+=loss.detach().cpu().numpy()
opti.zero_grad()
loss.backward()
opti.step()
# epoch_bar.write('L2={:.4f}'.format(L))
print('L2={:.4f}'.format(L))
self.gc2.load_state_dict(self.gc3.state_dict())
# self.linear_a2.load_state_dict(self.linear_a3.state_dict())
with torch.no_grad():
mu, logvar = self.encoder(x,adj)
assert F.mse_loss(mu, logvar) == 0
# assert F.mse_loss(mu_a, logvar_a) == 0
Z = mu.data.numpy()
gmm = GaussianMixture(n_components=self.args.nClusters, covariance_type='diag')
pre = gmm.fit_predict(Z)
print('Acc={:.4f}%'.format(cluster_acc(pre, Y)[0] * 100))
self.pi_.data = torch.from_numpy(gmm.weights_).float()
self.mu_c.data = torch.from_numpy(gmm.means_).float()
self.log_sigma2_c.data = torch.log(torch.from_numpy(gmm.covariances_).float())
torch.save(self.state_dict(), './vaecd/pretrain_model_{}.pk'.format(self.args.dataset))
else:
self.load_state_dict(torch.load('./vaecd/pretrain_model_{}.pk'.format(self.args.dataset)))
def predict(self,z):
pi = self.pi_
log_sigma2_c = self.log_sigma2_c
mu_c = self.mu_c
gamma_c = torch.exp(torch.log(pi.unsqueeze(0))+self.gaussian_pdfs_log(z,mu_c,log_sigma2_c))
gamma=gamma_c.detach().cpu().numpy()
return np.argmax(gamma,axis=1),gamma
def gaussian_pdfs_log(self,x,mus,log_sigma2s):
G=[]
for c in range(self.args.nClusters):
G.append(self.gaussian_pdf_log(x,mus[c:c+1,:],log_sigma2s[c:c+1,:]).view(-1,1))
return torch.cat(G,1)
@staticmethod
def gaussian_pdf_log(x,mu,log_sigma2):
return -0.5*(torch.sum(np.log(np.pi*2)+log_sigma2+(x-mu).pow(2)/torch.exp(log_sigma2),1))
def check_parameters(self):
for name, param in self.named_parameters():
if param.requires_grad:
print(name, param.data,param.data.shape)
class NEC(nn.Module):
def __init__(self, input_feat_dim, n_nodes, hidden_dim1, hidden_dim2, dropout,args,device):
super(NEC, self).__init__()
self.args = args
self.device = device
self.gc1 = GraphConvolutionSparse(input_feat_dim, hidden_dim1, dropout, act=torch.relu)
self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
self.dc = InnerProductDecoder(dropout, act=torch.sigmoid)
#modularity layer
self.modularity_layer= Linear(hidden_dim2,args.nClusters,act=torch.relu)
self.mu_c=nn.Parameter(torch.FloatTensor(args.nClusters,hidden_dim2).fill_(0.00),requires_grad=True)
torch.nn.init.xavier_normal_(self.mu_c)
def encoder(self, x, adj):
hidden1 = self.gc1(x, adj)
return self.gc2(hidden1, adj)
def decoder(self,z):
return self.dc(z)
def forward(self, x, adj):
mu = self.encoder(x, adj)
return self.dc(mu)
def modularity_loss(self,z,adj):
# adj = adj.to_dense()
H = self.modularity_layer(z)
assert H.shape[0]==z.shape[0]
n = torch.tensor(1.0*z.shape[0])
H_norm = n.sqrt()*H.sqrt()/(H.sqrt().sum())
# print("H_norm shape",H_norm.shape)
# print("H_norm ",H_norm)
m = (adj-torch.eye(adj.shape[0]).to(self.device)).sum()/2
D = (adj-torch.eye(adj.shape[0]).to(self.device)).sum(1) # the degree of nodes, adj includes self loop
B = (adj-torch.eye(adj.shape[0]).to(self.device))-torch.matmul(D.view(-1,1),D.view(1,-1))/(2*m) # modularity matrix
mod_loss=torch.trace(torch.matmul(torch.matmul(H_norm.t(),B),H_norm)/(4*m))
# print("mod_loss",mod_loss)
return mod_loss
def calculateP(self, Q):
# Function to calculate the desired distribution Q^2, for more details refer to DEC paper
f = Q.sum(dim=0)
pij_numerator = Q * Q
# pij_numerator = Q
pij_numerator = pij_numerator / f
normalizer_p = pij_numerator.sum(dim=1).reshape((Q.shape[0], 1))
P = pij_numerator / normalizer_p
return P
def getKLDivLossExpression(self, Q_expression, P_expression):
# Loss = KL Divergence between the two distributions
log_arg = P_expression / Q_expression
log_exp = torch.log(log_arg)
sum_arg = P_expression * log_exp
loss = torch.sum(sum_arg)/P_expression.shape[0]
return loss
def getSoftAssignments(self,latent_space, cluster_centers, num_samples):
'''
Returns cluster membership distribution for each sample
:param latent_space: latent space representation of inputs
:param cluster_centers: the coordinates of cluster centers in latent space
:param num_clusters: total number of clusters
:param latent_space_dim: dimensionality of latent space
:param num_samples: total number of input samples
:return: soft assigment based on the equation qij = (1+|zi - uj|^2)^(-1)/sum_j'((1+|zi - uj'|^2)^(-1))
'''
# z_expanded = latent_space.reshape((num_samples, 1, latent_space_dim))
# z_expanded = T.tile(z_expanded, (1, num_clusters, 1))
# u_expanded = T.tile(cluster_centers, (num_samples, 1, 1))
# distances_from_cluster_centers = (z_expanded - u_expanded).norm(2, axis=2)
# qij_numerator = 1 + distances_from_cluster_centers * distances_from_cluster_centers
# qij_numerator = 1 / qij_numerator
# normalizer_q = qij_numerator.sum(axis=1).reshape((num_samples, 1))
# return qij_numerator / normalizer_q
distances_from_cluster_centers = torch.sum((latent_space.unsqueeze(1)- cluster_centers.unsqueeze(0))**2,2)
qij_numerator = 1 + distances_from_cluster_centers
qij_numerator = 1 / qij_numerator
normalizer_q = qij_numerator.sum(dim=1).reshape((num_samples, 1))
return qij_numerator / normalizer_q
def loss(self,x,adj,labels, n_nodes, n_features, norm, pos_weight,L=1):
det=1e-10
labels_sub_u = labels
norm_u= norm
pos_weight_u = pos_weight
L_rec=0
mi=0
z = self.encoder(x, adj)
pred_adj = self.decoder(z)
cost_u = norm_u * F.binary_cross_entropy_with_logits(pred_adj, labels_sub_u, pos_weight = pos_weight_u)
L_rec += cost_u
mod_loss=self.modularity_loss(z,adj)
print('z shape mu_c shape',z.shape,self.mu_c.shape)
Q = self.getSoftAssignments(z,self.mu_c.to(self.device),n_nodes)
P = self.calculateP(Q)
soft_cluster_loss = self.getKLDivLossExpression(Q,P)
print("Soft cluster assignment",Counter(torch.argmax(Q,1).tolist()))
return [L_rec,mod_loss,soft_cluster_loss],[z]
def predict_soft_assignment(self,z):
Q = self.getSoftAssignments(z,self.mu_c.to(self.device),z.shape[0])
gamma_c = Q
gamma=gamma_c.detach().cpu().numpy()
return np.argmax(gamma,axis=1),gamma
def init_clustering_params_kmeans(self,km):
self.mu_c = torch.nn.Parameter(torch.from_numpy(km.cluster_centers_))
class DAEGCE(nn.Module):
def __init__(self, input_feat_dim, n_nodes, hidden_dim1, hidden_dim2, dropout,args,device):
super(DAEGCE, self).__init__()
self.device = device
self.args = args
self.gc1 = SpGAT(input_feat_dim,hidden_dim1,hidden_dim1,dropout,alpha=0.2,nheads=4)
self.gc2 = SpGAT(hidden_dim1,hidden_dim2,hidden_dim2,dropout,alpha=0.2,nheads=4)
self.dc = InnerProductDecoder(dropout, act=torch.sigmoid)
# self.dc = InnerDecoder(dropout, act=lambda x: x) # should we use sigmoid
self.mu_c=nn.Parameter(torch.FloatTensor(args.nClusters,hidden_dim2).fill_(0.00),requires_grad=True)
torch.nn.init.xavier_normal_(self.mu_c)
self.Q = None
self.P = None
def encoder(self, x, adj):
hidden1 = self.gc1(x, adj)
return self.gc2(hidden1, adj)
def decoder(self,z):
return self.dc(z)
def forward(self, x, adj):
z = self.encoder(x, adj)
return self.dc(z)
def change_cluster_grad_false(self):
for name, param in self.named_parameters():
if name in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=False
def change_cluster_grad_true(self):
for name, param in self.named_parameters():
if name in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=True
def change_nn_grad_false(self):
for name, param in self.named_parameters():
if name not in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=False
def change_nn_grad_true(self):
for name, param in self.named_parameters():
if name not in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=True
def loss(self,x,adj,labels, n_nodes, n_features, norm, pos_weight,epoch):
labels_sub_u = labels
norm_u = norm
pos_weight_u = pos_weight
z = self.encoder(x, adj)
pred_adj = self.decoder(z)
L_rec = norm_u * F.binary_cross_entropy_with_logits(pred_adj, labels_sub_u, pos_weight = pos_weight_u)
self.Q = self.getSoftAssignments(z,self.mu_c.to(self.device),n_nodes)
self.P = self.calculateP(self.Q)
if epoch>=200:
# if self.P == None:
# self.P = self.calculateP(self.Q)
# if epoch%5 == 0:
# print("udpate P")
# self.P = self.calculateP(self.Q)
# soft_cluster_loss = self.getKLDivLossExpression(Q,P)/(n_nodes*self.args.hidden2)
soft_cluster_loss = self.getKLDivLossExpression(self.Q, self.P)
return [L_rec,soft_cluster_loss],[z]
else:
return [L_rec],[z]
def predict_soft_assignment(self,z):
Q = self.getSoftAssignments(z,self.mu_c.to(self.device),z.shape[0])
gamma_c = Q
gamma=gamma_c.detach().cpu().numpy()
return np.argmax(gamma,axis=1),gamma
def calculateP(self, Q):
# Function to calculate the desired distribution Q^2, for more details refer to DEC paper
f = Q.sum(dim=0)
pij_numerator = Q * Q
# pij_numerator = Q
pij_numerator = pij_numerator / f
normalizer_p = pij_numerator.sum(dim=1).reshape((Q.shape[0], 1))
P = pij_numerator / normalizer_p
return P
def getKLDivLossExpression(self, Q_expression, P_expression):
# Loss = KL Divergence between the two distributions
log_arg = P_expression / Q_expression
log_exp = torch.log(log_arg)
sum_arg = P_expression * log_exp
loss = torch.sum(sum_arg)/P_expression.shape[0]
return loss
def getSoftAssignments(self,latent_space, cluster_centers, num_samples):
'''
Returns cluster membership distribution for each sample
:param latent_space: latent space representation of inputs
:param cluster_centers: the coordinates of cluster centers in latent space
:param num_clusters: total number of clusters
:param latent_space_dim: dimensionality of latent space
:param num_samples: total number of input samples
:return: soft assigment based on the equation qij = (1+|zi - uj|^2)^(-1)/sum_j'((1+|zi - uj'|^2)^(-1))
'''
# z_expanded = latent_space.reshape((num_samples, 1, latent_space_dim))
# z_expanded = T.tile(z_expanded, (1, num_clusters, 1))
# u_expanded = T.tile(cluster_centers, (num_samples, 1, 1))
# distances_from_cluster_centers = (z_expanded - u_expanded).norm(2, axis=2)
# qij_numerator = 1 + distances_from_cluster_centers * distances_from_cluster_centers
# qij_numerator = 1 / qij_numerator
# normalizer_q = qij_numerator.sum(axis=1).reshape((num_samples, 1))
# return qij_numerator / normalizer_q
distances_from_cluster_centers = torch.sum((latent_space.unsqueeze(1)- cluster_centers.unsqueeze(0))**2,2)
qij_numerator = 1 + distances_from_cluster_centers
qij_numerator = 1 / qij_numerator
normalizer_q = qij_numerator.sum(dim=1).reshape((num_samples, 1))
return qij_numerator / normalizer_q
def init_clustering_params_kmeans(self,km):
self.mu_c = torch.nn.Parameter(torch.from_numpy(km.cluster_centers_))
class GCNModelVAECE(nn.Module):
def __init__(self, input_feat_dim, n_nodes, hidden_dim1, hidden_dim2, dropout,args,device):
super(GCNModelVAECE, self).__init__()
self.args = args
self.device = device
if self.args.encoder == 'gcn':
self.gc1 = GraphConvolutionSparse(input_feat_dim, hidden_dim1, dropout, act=torch.relu)
self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
self.gc3 = GraphConvolution(hidden_dim1, hidden_dim2, dropout, act=lambda x: x)
if self.args.encoder == 'gat':
self.gc1 = SpGAT(input_feat_dim,hidden_dim1,hidden_dim1,dropout,alpha=0.2,nheads=5)
self.gc2 = SpGAT(hidden_dim1,hidden_dim2,hidden_dim2,dropout,alpha=0.2,nheads=5)
self.gc3 = SpGAT(hidden_dim1,hidden_dim2,hidden_dim2,dropout,alpha=0.2,nheads=5)
# self.dc = InnerProductDecoder(dropout, act=lambda x: x)
self.dc = InnerDecoder(dropout, act=lambda x: x) # should we use sigmoid
#for embedding attributes/features
self.linear_a1= Linear(n_nodes, hidden_dim1, act = torch.tanh,sparse_inputs=True) # the input dim is the number of nodes
self.linear_a2= Linear(hidden_dim1, hidden_dim2, act = lambda x:x)
self.linear_a3= Linear(hidden_dim1, hidden_dim2, act = lambda x:x)
#modularity layer
self.modularity_layer= Linear(hidden_dim2,args.nClusters,act=torch.relu)
# self.cluster_choose= Linear(hidden_dim2,args.nClusters,act=torch.relu)
self.pi_=nn.Parameter(torch.FloatTensor(args.nClusters,).fill_(1)/args.nClusters,requires_grad=True)
self.mu_c=nn.Parameter(torch.FloatTensor(args.nClusters,hidden_dim2).fill_(0.00),requires_grad=True)
self.log_sigma2_c=nn.Parameter(torch.FloatTensor(args.nClusters,hidden_dim2).fill_(0.0),requires_grad=True)
torch.nn.init.xavier_normal_(self.mu_c)
torch.nn.init.xavier_normal_(self.log_sigma2_c)
# calculate mi
# critic_params = {'dim_x': x.shape[1],'dim_y':y.shape[1],'layers': 2,'embed_dim': 32,'hidden_dim': 64,'activation': 'relu',}
# self.critic_structure = ConcatCritic(hidden_dim2,n_nodes,256,3,'relu',rho=None,)
# self.critic_feature = ConcatCritic(hidden_dim2,input_feat_dim,256,3,'relu',rho=None,)
def encoder(self, x, adj):
hidden1 = self.gc1(x, adj)
hidden_a1 = self.linear_a1(x.t()) # transpose the input feature matrix
return self.gc2(hidden1, adj), self.gc3(hidden1, adj), self.linear_a2(hidden_a1),self.linear_a3(hidden_a1)
def decoder(self,mu,mu_a,logvar,logvar_a):
z_u = self.reparameterize(mu, logvar)
z_a = self.reparameterize(mu_a,logvar_a)
return self.dc((z_u,z_a))
def reparameterize(self, mu, logvar):
if self.training:
std = torch.exp(logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(mu)
else:
return mu
def forward(self, x, adj):
mu, logvar, mu_a, logvar_a = self.encoder(x, adj)
z_u = self.reparameterize(mu, logvar)
z_a = self.reparameterize(mu_a,logvar_a)
return self.dc((z_u,z_a)),mu, logvar, mu_a, logvar_a
def modularity_loss(self, z,adj):
# adj = adj.to_dense()
H = self.modularity_layer(z)
assert H.shape[0]==z.shape[0]
n = torch.tensor(1.0*z.shape[0])
H_norm = n.sqrt()*H.sqrt()/(H.sqrt().sum())
# print("H_norm shape",H_norm.shape)
# print("H_norm ",H_norm)
m = (adj-torch.eye(adj.shape[0]).to(self.device)).sum()/2
D = (adj-torch.eye(adj.shape[0]).to(self.device)).sum(1) # the degree of nodes, adj includes self loop
B = (adj-torch.eye(adj.shape[0]).to(self.device))-torch.matmul(D.view(-1,1),D.view(1,-1))/(2*m) # modularity matrix
mod_loss=torch.trace(torch.matmul(torch.matmul(H_norm.t(),B),H_norm)/(4*m))
# print("mod_loss",mod_loss)
return mod_loss
def dist(self,x):
# x = x/torch.norm(x,2,dim=1).view(-1,1)
assert len(x.size()) == 2
norm = (x ** 2).sum(1).view(-1, 1)
dn = (norm + norm.view(1, -1)) - 2.0 * (x @ x.t())
return torch.sum(torch.relu(dn).sqrt())
def mi_loss(self,z,x,a):
# critic_params = {'dim_x': x.shape[1],'dim_y':y.shape[1],'layers': 2,'embed_dim': 32,'hidden_dim': 64,'activation': 'relu',}
# critic = ConcatCritic(rho=None,**critic_params)
indice = torch.randperm(len(z))[0:50]
# mi_x = estimate_mutual_information('dv',z[indice],x[indice],self.critic_structure)
mi_a = estimate_mutual_information('js',z[indice],a[indice],self.critic_feature)
return mi_a
def change_cluster_grad_false(self):
for name, param in self.named_parameters():
if name in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=False
def change_cluster_grad_true(self):
for name, param in self.named_parameters():
if name in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=True
def change_nn_grad_false(self):
for name, param in self.named_parameters():
if name not in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=False
def change_nn_grad_true(self):
for name, param in self.named_parameters():
if name not in ['pi_','mu_c','log_sigma2_c']:
param.requires_grad=True
def loss(self,x,adj,labels, n_nodes, n_features, norm, pos_weight,L=1):
det=1e-10
labels_sub_u, labels_sub_a = labels
norm_u, norm_a = norm
pos_weight_u, pos_weight_a = pos_weight
L_rec_u=0
L_rec_a=0
mi=0
mu, logvar, mu_a, logvar_a = self.encoder(x, adj)
# mutual information loss
# z_mu, z_sigma2_log = self.encoder(x)
# mi_a = self.mi_loss(mu,adj.to_dense(),x.to_dense())
for l in range(L):
# z=torch.randn_like(z_mu)*torch.exp(z_sigma2_log/2)+z_mu
pred_adj, pred_x = self.decoder(mu,mu_a,logvar,logvar_a)
# L_rec+=F.binary_cross_entropy(x_pro,x)
cost_u = norm_u * F.binary_cross_entropy_with_logits(pred_adj, labels_sub_u, pos_weight = pos_weight_u)
cost_a = norm_a * F.binary_cross_entropy_with_logits(pred_x, labels_sub_a, pos_weight = pos_weight_a)
# cost_u = norm_u * F.binary_cross_entropy_with_logits(pred_adj, labels_sub_u, pos_weight = pos_weight_u)
# cost_a = norm_a * F.binary_cross_entropy_with_logits(pred_x, labels_sub_a, pos_weight = pos_weight_a)
# cost_a =torch.Tensor(1).fill_(0)
L_rec_u += cost_u
L_rec_a += cost_a
L_rec_u/=L
L_rec_a/=L
# KLD_a = (0.5 / n_features) * torch.mean(torch.sum(-1 - 2 * logvar_a + mu_a.pow(2) + logvar_a.exp().pow(2), 1))
KLD_a = -(0.5 / n_features) * torch.mean(torch.sum(-1 - 2 * logvar_a + mu_a.pow(2) + logvar_a.exp().pow(2), 1))
# KLD_a =torch.Tensor(1).fill_(0)
# Loss=L_rec*x.size(1)
# log_sigma2_c=self.log_sigma2_c
# mu_c=self.mu_c
# z = torch.randn_like(z_mu) * torch.exp(z_sigma2_log / 2) + z_mu
z = self.reparameterize(mu,logvar)
# how about fusing attribute embeddings into node embedding?
z_a = self.reparameterize(mu_a,logvar_a)
H = torch.matmul(x,z_a)
assert H.shape[0],H.shape[1] == (n_nodes,self.args.hidden2)
# mod_loss=self.modularity_loss(z,adj)
gamma_c=torch.exp(torch.log(self.pi_.unsqueeze(0))+self.gaussian_pdfs_log(z,self.mu_c,self.log_sigma2_c))+det
# gamma_c=torch.exp(self.gaussian_pdfs_log(z,self.mu_c,self.log_sigma2_c))+det
# gamma_c = self.cluster_choose(self.reparameterize(mu,logvar))
# print('gamma_c:',gamma_c)
gamma_c=gamma_c/(gamma_c.sum(1).view(-1,1))#batch_size*Clusters
# gamma_c=F.softmax(gamma_c)
# print('gamma_c normalized:',gamma_c)
# print('gamma_c argmax:',torch.argmax(gamma_c,1))
# print('gamma_c counter:',Counter(torch.argmax(gamma_c,1).tolist()))
# gamma_c=torch.nn.functional.one_hot(torch.argmax(gamma_c,1),self.args.nClusters)
# self.pi_.data = (self.pi_/self.pi_.sum()).data # prior need to be re-normalized? In GMM, prior is based on gamma_c:https://brilliant.org/wiki/gaussian-mixture-model/
# self.pi_.data = gamma_c.mean(0).data # prior need to be re-normalized? In GMM, prior is based on gamma_c:https://brilliant.org/wiki/gaussian-mixture-model/
# multiple gaussian priors for
KLD_u_c=-(0.5/n_nodes)*torch.mean(torch.sum(gamma_c*torch.sum(-1+self.log_sigma2_c.unsqueeze(0)-2*logvar.unsqueeze(1)+torch.exp(2*logvar.unsqueeze(1)-self.log_sigma2_c.unsqueeze(0))+(mu.unsqueeze(1)-self.mu_c.unsqueeze(0)).pow(2)/torch.exp(self.log_sigma2_c.unsqueeze(0)),2),1))
#single KLD_u
# KLD_u_c= -0.5 / n_nodes * torch.mean(torch.sum(-1 - 2 * logvar + mu.pow(2) + logvar.exp().pow(2),1))
# KLD_u_c=-(0.5/n_nodes)*torch.mean(torch.sum(gamma_c*torch.sum(-1-2*logvar.unsqueeze(1)+torch.exp(2*logvar.unsqueeze(1))+(mu.unsqueeze(1)-self.mu_c.unsqueeze(0)).pow(2),2),1))
# temp_kld=-(0.5/n_nodes)*torch.sum((mu.unsqueeze(1)-self.mu_c.unsqueeze(0)).pow(2),2)
# KLD_u_c_test=-(0.5/n_nodes)*F.mse_loss(mu.unsqueeze(1),self.mu_c.unsqueeze(0),reduction='none')
# print('kld_u_c_test:',KLD_u_c_test.sum(2))
# KLD_u_c=-(0.5/n_nodes)*F.mse_loss(mu.unsqueeze(1),self.mu_c.unsqueeze(0))
# KLD_u_c=(0.5 / n_nodes)*torch.mean(torch.sum(gamma_c*torch.sum(self.log_sigma2_c.unsqueeze(0)+\
# torch.exp(2*logvar.unsqueeze(1)-self.log_sigma2_c.unsqueeze(0))+\
# (mu.unsqueeze(1)-self.mu_c.unsqueeze(0)).pow(2)/torch.exp(self.log_sigma2_c.unsqueeze(0)),2),1))
# mutual_dist = (1/(self.args.nClusters**2))*self.dist(self.mu_c)
mutual_dist = self.dist(self.mu_c)
# gamma_loss=-(1/self.args.nClusters)*torch.mean(torch.sum(gamma_c*torch.log(gamma_c),1))
# gamma_loss = (1 / self.args.nClusters) * torch.mean(torch.sum(gamma_c*torch.log(gamma_c),1)) - (0.5 / self.args.hid_dim)*torch.mean(torch.sum(1+2*logvar,1))
gamma_loss = -(1 / self.args.nClusters) * torch.mean(torch.sum(gamma_c*torch.log(gamma_c/self.pi_.unsqueeze(0)),1))
# gamma_loss = (1 / self.args.nClusters) * torch.mean(torch.sum(gamma_c*torch.log(gamma_c/self.pi_.unsqueeze(0)),1)) - (0.5 / self.args.hid_dim)*torch.mean(torch.sum(1+2*logvar,1))
# soft cluster assignment
# z = torch.cat((z,H),dim=1)
z = z
# print('z shape mu_c shape',z.shape,self.mu_c.shape)
Q = self.getSoftAssignments(z,self.mu_c.to(self.device),n_nodes)
P = self.calculateP(Q)
# if epoch ==0:
# P = self.calculateP(Q)
# if epoch!=0 and epoch%5==0:
# P = self.calculateP(Q)
# soft_cluster_loss = self.getKLDivLossExpression(Q,P)/(n_nodes*self.args.hidden2)
soft_cluster_loss = self.getKLDivLossExpression(Q,P)
# print("Soft cluster assignment",Counter(torch.argmax(Q,1).tolist()))
# return L_rec_u , L_rec_a , -KLD_u_c ,-KLD_a
# return L_rec_u , L_rec_a , -KLD_u_c ,-KLD_a , -0.1*soft_cluster_loss
# return L_rec_u , L_rec_a , -KLD_u_c ,-KLD_a , -0.1*mutual_dist,-0.01*soft_cluster_loss
return [L_rec_u , L_rec_a , -KLD_u_c ,-KLD_a,-gamma_loss,-mutual_dist,-soft_cluster_loss],[mu,logvar,mu_a,logvar_a,z]
# return [L_rec_u , L_rec_a , -KLD_u_c ,-KLD_a,-0.1*mutual_dist,-0.01*soft_cluster_loss],[mu,logvar,mu_a,logvar_a,z]
# return L_rec_u , L_rec_a , -KLD_u_c ,-KLD_a , -gamma_loss, -0.1*soft_cluster_loss
# return L_rec_u , L_rec_a , -KLD_u_c ,-KLD_a , -gamma_loss,-mi_a
# return L_rec_u + L_rec_a + KLD_u_c + KLD_a + gamma_loss
def pre_train(self,x,adj,Y,pre_epoch=22):
'''
This function is used to initialize cluster paramters: pi_, mu_c, log_sigma2_c.
-------------
paramters:
x: is the feature matrix of graph G.
adj: is the adjacent matrix of graph G.
Y: is the class label for each node in graph G.
'''
if not os.path.exists('./pretrain_model_{}_{}.pk'.format(self.args.dataset,pre_epoch)):
Loss=nn.MSELoss()
opti=Adam(self.parameters()) #all paramters in model
print('Pretraining......')
# epoch_bar=tqdm(range(pre_epoch))
# for _ in epoch_bar:
for _ in range(pre_epoch):
self.train()
L=0
mu, logvar, mu_a, logvar_a = self.encoder(x,adj)
pred_adj, pred_x = self.decoder(mu,mu_a,logvar,logvar_a)
loss= F.mse_loss(pred_x,x) + F.mse_loss(pred_adj,adj)
# L+=loss
opti.zero_grad()
loss.backward()
opti.step()
# epoch_bar.write('L2={:.4f}'.format(L))
print('L2={:.4f}'.format(loss.item()))
# self.gc2.load_state_dict(self.gc3.state_dict())
# self.linear_a2.load_state_dict(self.linear_a3.state_dict())
# with torch.no_grad():
# mu, logvar, mu_a, logvar_a = self.encoder(x,adj)
# assert F.mse_loss(mu, logvar) == 0
# assert F.mse_loss(mu_a, logvar_a) == 0
# Z = mu.data.numpy()
mu, logvar, mu_a, logvar_a = self.encoder(x,adj)
Z = self.reparameterize(mu,logvar)
gmm = GaussianMixture(n_components=self.args.nClusters, covariance_type='diag')
pre = gmm.fit_predict(Z.cpu().detach().numpy())
# print('Acc={:.4f}%'.format(cluster_acc(pre, Y)[0] * 100))
H, C, V, ari, ami, nmi, purity = clustering_evaluation(pre,Y)
print("purity, NMI:",purity,nmi)
# self.plot_tsne(self.args.dataset,pre_epoch,Z.to('cpu'),Y,pre)
self.pi_= torch.nn.Parameter(torch.from_numpy(gmm.weights_))
self.mu_c = torch.nn.Parameter(torch.from_numpy(gmm.means_))
self.log_sigma2_c = torch.nn.Parameter(torch.from_numpy(gmm.covariances_))
torch.save(self.state_dict(), './pretrain_model_{}_{}.pk'.format(self.args.dataset,pre_epoch))
else:
self.load_state_dict(torch.load('./pretrain_model_{}_{}.pk'.format(self.args.dataset,pre_epoch)))
# def predict_nn(self,mu,logvar):
# z = self.reparameterize(mu,logvar)
# gamma_c = self.cluster_choose(self.reparameterize(mu,logvar))
# print('gamma_c,normalized:',gamma_c)
# print('gamma_c argmax:',torch.argmax(gamma_c,1))
# print('gamma_c argmax counter:',Counter(torch.argmax(gamma_c,1).tolist()))
# gamma=gamma_c.detach().cpu().numpy()
# return np.argmax(gamma,axis=1),gamma, z
def predict_soft_assignment(self, mu, logvar,z):
# z_mu, z_sigma2_log, z_ma,z_a_sigma2_log = self.encoder(x,adj)
# mu, logvar, mu_a, logvar_a = self.encoder(x,adj)
# z = torch.randn_like(mu) * torch.exp(z_sigma2_log / 2) + z_mu
det=1e-10
# z = self.reparameterize(mu,logvar)
Q = self.getSoftAssignments(z,self.mu_c.to(self.device),z.shape[0])
pi = self.pi_
# log_sigma2_c = self.log_sigma2_c
# mu_c = self.mu_c
# gamma_c = torch.exp(torch.log(pi.unsqueeze(0))+self.gaussian_pdfs_log(z,mu_c,log_sigma2_c))
# gamma_c = torch.exp(self.gaussian_pdfs_log(mu,self.mu_c,self.log_sigma2_c))+det
# gamma_c = torch.exp(self.gaussian_pdfs_log(z,self.mu_c,self.log_sigma2_c))+det
gamma_c = Q
# print('gamma_c:',gamma_c)
# gamma_c=gamma_c/(gamma_c.sum(1).view(-1,1))#batch_size*Clusters
# gamma_c=F.softmax(gamma_c)
# print('gamma_c,normalized:',gamma_c)
# print('gamma_c argmax:',torch.argmax(gamma_c,1))
gamma=gamma_c.detach().cpu().numpy()
return np.argmax(gamma,axis=1),gamma, z
def predict(self,mu, logvar):
# z_mu, z_sigma2_log, z_ma,z_a_sigma2_log = self.encoder(x,adj)
# mu, logvar, mu_a, logvar_a = self.encoder(x,adj)
# z = torch.randn_like(mu) * torch.exp(z_sigma2_log / 2) + z_mu
det=1e-10
z = self.reparameterize(mu,logvar)
pi = self.pi_
# log_sigma2_c = self.log_sigma2_c
# mu_c = self.mu_c
# gamma_c = torch.exp(torch.log(pi.unsqueeze(0))+self.gaussian_pdfs_log(z,mu_c,log_sigma2_c))
gamma_c = torch.exp(self.gaussian_pdfs_log(mu,self.mu_c,self.log_sigma2_c))+det
# gamma_c = torch.exp(self.gaussian_pdfs_log(z,self.mu_c,self.log_sigma2_c))+det
print('gamma_c:',gamma_c)
gamma_c=gamma_c/(gamma_c.sum(1).view(-1,1))#batch_size*Clusters
# gamma_c=F.softmax(gamma_c)
print('gamma_c,normalized:',gamma_c)
print('gamma_c argmax:',torch.argmax(gamma_c,1))
print('gamma_c argmax counter:',Counter(torch.argmax(gamma_c,1).tolist()))
gamma=gamma_c.detach().cpu().numpy()
return np.argmax(gamma,axis=1),gamma, z
def predict_dist(self,mu, logvar):
# z_mu, z_sigma2_log, z_ma,z_a_sigma2_log = self.encoder(x,adj)
# mu, logvar, mu_a, logvar_a = self.encoder(x,adj)
# z = torch.randn_like(mu) * torch.exp(z_sigma2_log / 2) + z_mu
z = self.reparameterize(mu,logvar)
pi = self.pi_
log_sigma2_c = self.log_sigma2_c
mu_c = self.mu_c
# gamma_c = torch.exp(self.gaussian_pdfs_log(z,mu_c,log_sigma2_c))
# gamma=gamma_c.detach().cpu().numpy()
gamma=[]
for e in range(z.shape[0]):
temp_dist=[]
for m in range(mu_c.shape[0]):
temp_dist.append(F.mse_loss(z[e],mu_c[m]).data)
gamma.append(temp_dist)
return np.argmin(gamma,axis=1),np.array(gamma)
def gaussian_pdfs_log(self,x,mus,log_sigma2s):
G=[]
for c in range(self.args.nClusters):
G.append(self.gaussian_pdf_log(x,mus[c:c+1,:],log_sigma2s[c:c+1,:]).view(-1,1))
return torch.cat(G,1)