-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunExp_GAT(E).py
1633 lines (1462 loc) · 79.2 KB
/
runExp_GAT(E).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.functional as F
from torch import Tensor
from torch.nn import Parameter
#from torch_geometric.nn.conv import MessagePassing
from torch_geometric.nn.dense.linear import Linear
from torch_geometric.nn.inits import glorot, zeros
from torch_geometric.typing import (
Adj,
OptTensor,
PairTensor,
SparseTensor,
#torch_sparse,
)
from typing import Optional, Tuple, Union
from torch_geometric.utils import (
add_self_loops,
is_torch_sparse_tensor,
remove_self_loops,
softmax,
is_sparse,
)
from torch_geometric.datasets import Planetoid
from torch_geometric.transforms import NormalizeFeatures,LargestConnectedComponents
import networkx as nx
import torch_geometric.transforms as T
from torch_geometric.data import Data
from torch_geometric.utils import segregate_self_loops,dense_to_sparse,\
index_to_mask,get_laplacian,erdos_renyi_graph,to_networkx
from torchmetrics.functional import pairwise_cosine_similarity
from torchmetrics import AUROC
import torch
from torch.nn import Linear
import torch.nn.functional as F
#from torch_geometric.nn import GCNConv,GATv2Conv
import numpy as np
import matplotlib.pyplot as plt
import copy
import json
import pprint
import pickle
from scipy import stats
import pandas as pd
import os.path
from math import floor,ceil
from itertools import product
import scipy.sparse as sp
from torch.distributions.multivariate_normal import MultivariateNormal
from tsne_torch import TorchTSNE as TSNE
from sklearn.cluster import KMeans
import heapq
from scipy import stats
from msgpass import MessagePassing
from torchmetrics.functional.pairwise.helpers import _check_input
path = "ExpResults_GAT(E)/"
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def getDataHet(datasetName,splitID=1):
print("Loading datasets as npz-file..")
data = np.load('data/heterophilous-graphs/'+datasetName+'.npz')
x = torch.tensor(data['node_features'], dtype=torch.float)
y = torch.tensor(data['node_labels'], dtype=torch.long)
edge_index = torch.tensor(data['edges'], dtype=torch.long).t().contiguous()
train_mask = torch.tensor(data['train_masks'], dtype=torch.bool).transpose(0, 1).contiguous()
val_mask = torch.tensor(data['val_masks'], dtype=torch.bool).transpose(0, 1).contiguous()
test_mask = torch.tensor(data['test_masks'], dtype=torch.bool).transpose(0, 1).contiguous()
num_classes = len(torch.unique(y))
num_targets = 1 if num_classes == 2 else num_classes
print("Converting to PyG dataset...")
data = Data(x=x, edge_index=edge_index)
data.y = y
data.num_classes = num_classes
data.num_targets = num_targets
data.train_mask = train_mask[:,splitID] # 10 splits provided in dataset
data.val_mask = val_mask[:,splitID]
data.test_mask = test_mask[:,splitID]
return data,data.num_features,data.num_classes
def getData(datasetName, dataTransform, randomLabels=False,oneHotFeatures=False,randomLabelCount=None,splitID=1):
if datasetName[:3]=='Syn': # == 'Synthetic':
synID = datasetName.split("_")[1]
with open('SyntheticData/D'+str(synID)+'.pkl', 'rb') as f:
data = pickle.load(f)
return data,data.x.shape[1],len(torch.unique(data.y))
if datasetName in ['Cora','Citeseer','Pubmed']:
dataset = Planetoid(root='data/Planetoid', name=datasetName, transform=NormalizeFeatures())
data = dataset[0]
if dataTransform=='removeIsolatedNodes':
out = segregate_self_loops(data.edge_index)
edge_index, edge_attr, loop_edge_index, loop_edge_attr = out
mask = torch.zeros(data.num_nodes, dtype=torch.bool, device=data.x.device)
mask[edge_index.view(-1)] = 1
data.train_mask = data.train_mask & mask
data.val_mask = data.val_mask & mask
data.test_mask = data.test_mask & mask
if dataTransform=='useLCC':
transformLCC = LargestConnectedComponents()
data = transformLCC(data)
if randomLabels:
if randomLabelCount==None:
data.y = torch.randint(low=0,high=len(torch.unique(data.y)),size=data.y.shape)
else:
data.y = torch.randint(low=0,high=randomLabelCount,size=data.y.shape)
if oneHotFeatures:
data.x = torch.tensor(F.one_hot(data.y).clone().detach(),dtype = torch.float32)
return data,data.x.shape[1],len(torch.unique(data.y))#dataset.num_features,dataset.num_classes
else:
s = datasetName.split("_")
if len(s) == 1:
s = s + [str(splitID)]
return getDataHet(s[0],int(s[1]))
class GATv2Conv(MessagePassing):
r"""The GATv2 operator from the `"How Attentive are Graph Attention
Networks?" <https://arxiv.org/abs/2105.14491>`_ paper, which fixes the
static attention problem of the standard
:class:`~torch_geometric.conv.GATConv` layer.
Since the linear layers in the standard GAT are applied right after each
other, the ranking of attended nodes is unconditioned on the query node.
In contrast, in :class:`GATv2`, every node can attend to any other node.
.. math::
\mathbf{x}^{\prime}_i = \alpha_{i,i}\mathbf{\Theta}\mathbf{x}_{i} +
\sum_{j \in \mathcal{N}(i)} \alpha_{i,j}\mathbf{\Theta}\mathbf{x}_{j},
where the attention coefficients :math:`\alpha_{i,j}` are computed as
.. math::
\alpha_{i,j} =
\frac{
\exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left(\mathbf{\Theta}
[\mathbf{x}_i \, \Vert \, \mathbf{x}_j]
\right)\right)}
{\sum_{k \in \mathcal{N}(i) \cup \{ i \}}
\exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left(\mathbf{\Theta}
[\mathbf{x}_i \, \Vert \, \mathbf{x}_k]
\right)\right)}.
If the graph has multi-dimensional edge features :math:`\mathbf{e}_{i,j}`,
the attention coefficients :math:`\alpha_{i,j}` are computed as
.. math::
\alpha_{i,j} =
\frac{
\exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left(\mathbf{\Theta}
[\mathbf{x}_i \, \Vert \, \mathbf{x}_j \, \Vert \, \mathbf{e}_{i,j}]
\right)\right)}
{\sum_{k \in \mathcal{N}(i) \cup \{ i \}}
\exp\left(\mathbf{a}^{\top}\mathrm{LeakyReLU}\left(\mathbf{\Theta}
[\mathbf{x}_i \, \Vert \, \mathbf{x}_k \, \Vert \, \mathbf{e}_{i,k}]
\right)\right)}.
Args:
in_channels (int or tuple): Size of each input sample, or :obj:`-1` to
derive the size from the first input(s) to the forward method.
A tuple corresponds to the sizes of source and target
dimensionalities.
out_channels (int): Size of each output sample.
heads (int, optional): Number of multi-head-attentions.
(default: :obj:`1`)
concat (bool, optional): If set to :obj:`False`, the multi-head
attentions are averaged instead of concatenated.
(default: :obj:`True`)
negative_slope (float, optional): LeakyReLU angle of the negative
slope. (default: :obj:`0.2`)
dropout (float, optional): Dropout probability of the normalized
attention coefficients which exposes each node to a stochastically
sampled neighborhood during training. (default: :obj:`0`)
add_self_loops (bool, optional): If set to :obj:`False`, will not add
self-loops to the input graph. (default: :obj:`True`)
edge_dim (int, optional): Edge feature dimensionality (in case
there are any). (default: :obj:`None`)
fill_value (float or torch.Tensor or str, optional): The way to
generate edge features of self-loops
(in case :obj:`edge_dim != None`).
If given as :obj:`float` or :class:`torch.Tensor`, edge features of
self-loops will be directly given by :obj:`fill_value`.
If given as :obj:`str`, edge features of self-loops are computed by
aggregating all features of edges that point to the specific node,
according to a reduce operation. (:obj:`"add"`, :obj:`"mean"`,
:obj:`"min"`, :obj:`"max"`, :obj:`"mul"`). (default: :obj:`"mean"`)
bias (bool, optional): If set to :obj:`False`, the layer will not learn
an additive bias. (default: :obj:`True`)
share_weights (bool, optional): If set to :obj:`True`, the same matrix
will be applied to the source and the target node of every edge.
(default: :obj:`False`)
**kwargs (optional): Additional arguments of
:class:`torch_geometric.nn.conv.MessagePassing`.
Shapes:
- **input:**
node features :math:`(|\mathcal{V}|, F_{in})` or
:math:`((|\mathcal{V_s}|, F_{s}), (|\mathcal{V_t}|, F_{t}))`
if bipartite,
edge indices :math:`(2, |\mathcal{E}|)`,
edge features :math:`(|\mathcal{E}|, D)` *(optional)*
- **output:** node features :math:`(|\mathcal{V}|, H * F_{out})` or
:math:`((|\mathcal{V}_t|, H * F_{out})` if bipartite.
If :obj:`return_attention_weights=True`, then
:math:`((|\mathcal{V}|, H * F_{out}),
((2, |\mathcal{E}|), (|\mathcal{E}|, H)))`
or :math:`((|\mathcal{V_t}|, H * F_{out}), ((2, |\mathcal{E}|),
(|\mathcal{E}|, H)))` if bipartite
"""
_alpha: OptTensor
def reset_parameters(self):
super().reset_parameters()
self.lin_l.reset_parameters()
if self.lin_r is not None:
self.lin_r.reset_parameters()
self.lin_lAP.reset_parameters()
self.lin_rAP.reset_parameters()
if self.lin_edge is not None:
self.lin_edge.reset_parameters()
glorot(self.att)
glorot(self.att2)
zeros(self.bias)
def __init__(
self,
in_channels: Union[int, Tuple[int, int]],
out_channels: int,
heads: int = 1,
concat: bool = True,
negative_slope: float = 0.2,
dropout: float = 0.0,
add_self_loops: bool = True,
edge_dim: Optional[int] = None,
fill_value: Union[float, Tensor, str] = 'mean',
bias: bool = True,
share_weights: bool = False,
attParamSharing = True,
linWghtSharing = True,
hasOmega = False,
omegaInitVal : float = 1.0,
defaultInit : bool = False,
linAggrSharing : bool = False, #change default later to True
alphasActivation : str = 'relu',
**kwargs,
):
super().__init__(node_dim=0, **kwargs)
self.in_channels = in_channels
self.out_channels = out_channels
self.heads = heads
self.concat = concat
self.negative_slope = negative_slope
self.dropout = dropout
self.add_self_loops = add_self_loops
self.edge_dim = edge_dim
self.fill_value = fill_value
self.share_weights = share_weights
self.attParamSharing = attParamSharing
self.linWghtSharing = linWghtSharing
self.hasOmega = hasOmega
self.linAggrSharing = linAggrSharing
self.alphasActivation = alphasActivation
if isinstance(in_channels, int):
self.lin_l = Linear(in_channels, heads * out_channels, bias=bias)
#weight_initializer='glorot')
if share_weights:
self.lin_r = self.lin_l
else:
self.lin_r = Linear(in_channels, heads * out_channels,
bias=bias)#, weight_initializer='glorot')
if linAggrSharing:
self.lin_s = self.lin_l
else:
self.lin_s = Linear(in_channels, heads * out_channels, bias=bias)
else: #not usually the case.. not sure what case this is.. different input sizes for node self and nbts to compute alphas
self.lin_l = Linear(in_channels[0], heads * out_channels,
bias=bias)#, weight_initializer='glorot')
if share_weights:
self.lin_r = self.lin_l
else:
self.lin_r = Linear(in_channels[1], heads * out_channels,
bias=bias)#, weight_initializer='glorot')
self.att = Parameter(torch.empty(1, heads, out_channels))
#self.omega = Parameter(torch.ones(heads * out_channels,))
if self.attParamSharing:
self.att2 = self.att
else:
self.att2 = Parameter(torch.empty(1, heads, out_channels))
if linWghtSharing:
self.lin_lAP = self.lin_l
self.lin_rAP = self.lin_r
else:
self.lin_lAP = Linear(in_channels, heads * out_channels, bias=bias)
if share_weights:
self.lin_rAP = self.lin_lAP
else:
self.lin_rAP = Linear(in_channels, heads * out_channels, bias=bias)
if self.hasOmega:
self.omega = Parameter(torch.zeros(heads * out_channels,))
self.omega.data.fill_(omegaInitVal)
#self.signParam = Parameter(torch.zeros(heads * out_channels,))
if edge_dim is not None:
self.lin_edge = Linear(edge_dim, heads * out_channels, bias=False),
#weight_initializer='glorot')
else:
self.lin_edge = None
if bias and concat:
self.bias = Parameter(torch.empty(heads * out_channels))
elif bias and not concat:
self.bias = Parameter(torch.empty(out_channels))
else:
self.register_parameter('bias', None)
self._alpha = None
if defaultInit:
self.reset_parameters()
def forward(self, x: Union[Tensor, PairTensor], edge_index: Adj,
edge_attr: OptTensor = None,
return_attention_weights: bool = None):
# type: (Union[Tensor, PairTensor], Tensor, OptTensor, NoneType) -> Tensor # noqa
# type: (Union[Tensor, PairTensor], SparseTensor, OptTensor, NoneType) -> Tensor # noqa
# type: (Union[Tensor, PairTensor], Tensor, OptTensor, bool) -> Tuple[Tensor, Tuple[Tensor, Tensor]] # noqa
# type: (Union[Tensor, PairTensor], SparseTensor, OptTensor, bool) -> Tuple[Tensor, SparseTensor] # noqa
r"""Runs the forward pass of the module.
Args:
return_attention_weights (bool, optional): If set to :obj:`True`,
will additionally return the tuple
:obj:`(edge_index, attention_weights)`, holding the computed
attention weights for each edge. (default: :obj:`None`)
"""
H, C = self.heads, self.out_channels
x_l: OptTensor = None
x_r: OptTensor = None
x_s: OptTensor = None
x_lAP: OptTensor = None
x_rAP: OptTensor = None
if isinstance(x, Tensor):
assert x.dim() == 2
x_l = self.lin_l(x).view(-1, H, C)
x_s = self.lin_s(x).view(-1, H, C)
if self.linWghtSharing:
if self.share_weights :
x_r = x_l
else:
x_r = self.lin_r(x).view(-1, H, C)
else:
x_l, x_r = x[0], x[1]
assert x[0].dim() == 2
x_l = self.lin_l(x_l).view(-1, H, C)
if x_r is not None:
x_r = self.lin_r(x_r).view(-1, H, C)
#added for attn perceptron weight sharing
if self.linWghtSharing:
x_lAP = x_l
x_rAP = x_r
else:
x_lAP = self.lin_lAP(x).view(-1, H, C)
if self.share_weights:
x_rAP = x_lAP
else:
x_rAP = self.lin_rAP(x).view(-1, H, C)
# print(x_l.shape)
# print(x_lAP.shape)
# print(x_r.shape)
# print(x_rAP.shape)
assert x_l is not None
assert x_s is not None
#assert x_r is not None #can be none if linWghtSharing==False
assert x_lAP is not None
assert x_rAP is not None
if self.add_self_loops:
if isinstance(edge_index, Tensor):
num_nodes = x_l.size(0)
if x_r is not None:
num_nodes = min(num_nodes, x_r.size(0))
edge_index, edge_attr = remove_self_loops(
edge_index, edge_attr)
edge_index, edge_attr = add_self_loops(
edge_index, edge_attr, fill_value=self.fill_value,
num_nodes=num_nodes)
elif isinstance(edge_index, SparseTensor):
if self.edge_dim is None:
print('Not implemented :( ')
#edge_index = torch_sparse.set_diag(edge_index)
else:
raise NotImplementedError(
"The usage of 'edge_attr' and 'add_self_loops' "
"simultaneously is currently not yet supported for "
"'edge_index' in a 'SparseTensor' form")
# print('X_L',x_l)
# print('X_R',x_r)
# propagate_type: (x: PairTensor, edge_attr: OptTensor)
out = self.propagate(edge_index, x=(x_l, x_r), edge_attr=edge_attr,
size=None,edgeIndex = edge_index,
xAP=(x_lAP,x_rAP), x_s=x_s) #x_l corresponds to x_j in
alpha = self._alpha
assert alpha is not None
self._alpha = None
if self.concat:
out = out.view(-1, self.heads * self.out_channels)
else:
out = out.mean(dim=1)
if self.bias is not None:
out = out + self.bias
if isinstance(return_attention_weights, bool):
if isinstance(edge_index, Tensor):
if is_torch_sparse_tensor(edge_index):
# TODO TorchScript requires to return a tuple
adj = 0 #set_sparse_value(edge_index, alpha) ######CHANGEDDDD
return out, (adj, alpha)
else:
return out, (edge_index, alpha)
elif isinstance(edge_index, SparseTensor):
return out, edge_index.set_value(alpha, layout='coo')
else:
return out
def message(self, x_j: Tensor, x_i: Tensor, edge_attr: OptTensor,
index: Tensor, ptr: OptTensor,
size_i: Optional[int],edgeIndex: OptTensor,
xAP_lAP: Tensor, xAP_rAP: Tensor, x_s: Tensor) -> Tensor: #, x_s: Tensor
# print(x_j.shape)
# print(x_s.shape)
# print('x_j==xAP_s',torch.equal(x_j,x_s))
# print('x_j==xAP_lAP',torch.equal(x_j,xAP_lAP))
# print('x_i==xAP_rAP',torch.equal(x_i,xAP_rAP))
# print('x_i==x_j',torch.equal(x_i,x_j))
# input("Press Enter to continue...")
#x = x_i + x_j
x = xAP_lAP + xAP_rAP
#sign = torch.sign((F.tanh(x)*self.signParam).sum(dim=-1))
if edge_attr is not None:
if edge_attr.dim() == 1:
edge_attr = edge_attr.view(-1, 1)
assert self.lin_edge is not None
edge_attr = self.lin_edge(edge_attr)
edge_attr = edge_attr.view(-1, self.heads, self.out_channels)
x = x + edge_attr
#Replaced laeky_relu by relu so -ve and +ve values of attn parameters a1 and a2, respectively, correspond
#-ve and +ve values of e_{uv} and e_{vv}
if self.attParamSharing:
x = F.leaky_relu(x, self.negative_slope)
else:
if self.alphasActivation == 'relu':
x = F.relu(x)#, self.negative_slope)
elif self.alphasActivation == 'leaky_relu':
x = F.leaky_relu(x,self.negative_slope)
alpha1 = (x * self.att).sum(dim=-1)
ijNotEq = torch.tensor(edgeIndex[0]!=edgeIndex[1],dtype=torch.float).unsqueeze(-1)
ijEq = torch.tensor(edgeIndex[0]==edgeIndex[1],dtype=torch.float).unsqueeze(-1)#.unsqueeze(-1)
alpha1 = alpha1*ijNotEq
alpha2 = (x * self.att2).sum(dim=-1)
alpha2 = alpha2*ijEq
alpha = alpha1+alpha2
# print('alpha1:',alpha1.unsqueeze(-1).shape)
# print('alpha2:',alpha2.shape)
# print('ijNotEq:',ijNotEq.shape)
# print('ijEq:',ijEq.shape)
#print('e: ',alpha.shape)
alpha = softmax(alpha, index, ptr, size_i)
# print('ALPHA SHAPE: ',alpha.shape)
# print('ALPHA SHAPE unsqueeze: ',alpha.unsqueeze(-1).shape)
# print('NODE J SHAPE: ' ,x_j.shape)
# print('MESSAGE J SHAPE: ' ,(x_j * alpha.unsqueeze(-1) * self.omega).shape)
self._alpha = alpha
alpha = F.dropout(alpha, p=self.dropout, training=self.training)
#x_jP = x_j
x_j = (x_j*ijNotEq.unsqueeze(-1)) + (x_s*ijEq.unsqueeze(-1))
# print('x_j==x_jP',torch.equal(x_j,x_jP))
# input('Enterrrr...')
if self.hasOmega:
alpha = alpha.unsqueeze(-1)
ijEq = ijEq.unsqueeze(-1)
return x_j*(ijEq-(self.omega*(ijEq-alpha)))
else:
# print(edgeIndex.shape)
# print(x_j.shape)
# print(alpha.shape)
#input('Enter......')
return x_j * alpha.unsqueeze(-1)
#(f-(o*(f-a)))*b
#
def __repr__(self) -> str:
return (f'{self.__class__.__name__}({self.in_channels}, '
f'{self.out_channels}, heads={self.heads})')
def printOmega(self):
print(self.omega)
def sq_euclidean_dist(x,y,zero_diagonal=True):
x, y, zero_diagonal = _check_input(x, y, zero_diagonal)
# upcast to float64 to prevent precision issues
_orig_dtype = x.dtype
x = x.to(torch.float64)
y = y.to(torch.float64)
x_norm = (x * x).sum(dim=1, keepdim=True)
y_norm = (y * y).sum(dim=1)
distance = (x_norm + y_norm - 2 * x.mm(y.T)).to(_orig_dtype)
if zero_diagonal:
distance.fill_diagonal_(0)
return distance#.sqrt()
class MLP(torch.nn.Module):
def __init__(self, numLayers, dims, heads, concat, weightSharing, selfLoops = True,
attnDropout=0,bias=False, activation='relu',useIdMap=False, useResLin=False,
attParamSharing=True,linWghtSharing=True,linLastLayer=False,hasOmega=False,
omegaInitVal=1,defaultInit=False,linAggrSharing=True,alphasActivation='relu'):
super().__init__()
self.numLayers = numLayers
self.bias=bias
if activation=='relu':
self.activation = F.relu
self.layers = torch.nn.ModuleList(
[Linear(dims[j],dims[j+1],bias=self.bias)
for j in range(self.numLayers)])
def forward(self, x, edge_index, getSelfAttnCoef=False,getMetric=[False,False],adj=None,masks={},classMasks=[],classWise=False,getEntropy=False):
for l in range(numLayers):
x = self.layers(x)
if l<(numLayers-1):
x = self.activation(x)
return x, None,None,None
class GATv2(torch.nn.Module):
def __init__(self, numLayers, dims, heads, concat, weightSharing, selfLoops = True,
attnDropout=0,bias=False, activation='relu',useIdMap=False, useResLin=False,
attParamSharing=True,linWghtSharing=True,linLastLayer=False,hasOmega=False,
omegaInitVal=1,defaultInit=False,linAggrSharing=True,alphasActivation='relu'):
super().__init__()
self.numLayers = numLayers
self.heads = heads
self.weightSharing = weightSharing
self.selfLoops = selfLoops
self.dropout = attnDropout
self.bias=bias
if activation=='relu':
self.activation = F.relu
elif activation=='elu':
self.activation = F.elu # as used previously
self.useIdMap = useIdMap
self.useResLin = useResLin
self.linLastLayer = linLastLayer
if self.linLastLayer:
self.layers = torch.nn.ModuleList(
[GATv2Conv(dims[j]*heads[j],dims[j+1],bias=bias,
heads=heads[j+1],concat=concat[j],add_self_loops=selfLoops,
share_weights=weightSharing,dropout=attnDropout,
attParamSharing=attParamSharing,linWghtSharing=linWghtSharing,
hasOmega=hasOmega,omegaInitVal=omegaInitVal,defaultInit=defaultInit,
linAggrSharing=linAggrSharing,alphasActivation=alphasActivation)
for j in range(self.numLayers-1)]
+[Linear(dims[numLayers-1],dims[numLayers],bias=self.bias)])
else:
self.layers = torch.nn.ModuleList(
[GATv2Conv(dims[j]*heads[j],dims[j+1],bias=bias,
heads=heads[j+1],concat=concat[j],add_self_loops=selfLoops,
share_weights=weightSharing,dropout=attnDropout,
attParamSharing=attParamSharing,linWghtSharing=linWghtSharing,
hasOmega=hasOmega,omegaInitVal=omegaInitVal,defaultInit=defaultInit,
linAggrSharing=linAggrSharing,alphasActivation=alphasActivation)
for j in range(self.numLayers)])
if self.useIdMap:
self.residual = torch.nn.ModuleList(
[torch.nn.Linear(dims[0]*heads[0],dims[1]*heads[1],bias=False),
torch.nn.Linear(dims[self.numLayers-1]*heads[self.numLayers-1],dims[self.numLayers],bias=False)])
# self.residual = torch.nn.ModuleList(
# [torch.nn.Linear(dims[j]*heads[j],dims[j+1]*heads[j+1],bias=False) for j in [0,self.numLayers-1]])
if self.useResLin:
self.residual = torch.nn.ModuleList(
[torch.nn.Linear(dims[j],dims[j+1],bias=False) for j in range(numLayers)])
def forward(self, x, edge_index, getSelfAttnCoef=False,getMetric=[False,False],adj=None,masks={},classMasks=[],classWise=False,getEntropy=False):
#leakyrelu for computing alphas have negative_slope=0.2 (as set in GAT and used in GATv2)
attnCoef = [0] * len(self.layers)
entropy = [0] * len(self.layers)
dirEnGlb = None#{k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
dirEnNbr = None#{k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
madGlb = None #{k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
madNbr = None #{k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
numClasses = len(classMasks)
dirEnGlbClassWise=None
dirEnNbrClassWise=None
dirEnDisClassWise=None
madGlbClassWise=None
madNbrClassWise=None
madDisClassWise=None
dirEnGlb1ClsVsAll=None
dirEnNbr1ClsVsAll=None
dirEnDis1ClsVsAll=None
madGlb1ClsVsAll=None
madNbr1ClsVsAll=None
madDis1ClsVsAll=None
if adj!=None:
adjPrime = (1-adj).fill_diagonal_(0)
with torch.no_grad():
if getMetric[0]:
dirEnGlb = {k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
dirEnNbr = {k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
#dirEnLcl = {k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
d = sq_euclidean_dist(x.detach().clone(),x.detach().clone())#torch.square(pairwise_euclidean_distance(x.detach().clone()))
md=torch.mul(adj,d)
dirEnGlb['All'][0] = torch.sum(d)/2
dirEnNbr['All'][0] = torch.nansum(md)/2#torch.nanmean(torch.sum(md,axis=1)/torch.count_nonzero(adj[1],axis=1))
for k,v in masks.items():
dirEnGlb[k][0]=torch.sum(d[v,v])/2
#dirEnLcl[k][0]=torch.sum(md[v,:])
dirEnNbr[k][0]=torch.nansum(md[v,:][:,v])/2 #torch.nanmean(torch.sum(md[v,:],axis=1)/torch.count_nonzero(adj[1][v,:],axis=1))
if classWise:
dirEnGlbClassWise = torch.FloatTensor(self.numLayers+1,numClasses,numClasses)
dirEnNbrClassWise = torch.FloatTensor(self.numLayers+1,numClasses,numClasses)
dirEnDisClassWise = torch.FloatTensor(self.numLayers+1,numClasses,numClasses)
dirEnGlb1ClsVsAll = torch.FloatTensor(self.numLayers+1,numClasses)
dirEnNbr1ClsVsAll = torch.FloatTensor(self.numLayers+1,numClasses)
dirEnDis1ClsVsAll = torch.FloatTensor(self.numLayers+1,numClasses)
mdPrime = torch.mul(1-adj,d)
for c1 in range(numClasses):
m1 = classMasks[c1]
m2 = torch.logical_not(m1)
dirEnGlb1ClsVsAll[0][c1]=torch.sum(d[m1,:][:,m2])
dirEnNbr1ClsVsAll[0][c1]=torch.nansum(md[m1,:][:,m2])
dirEnDis1ClsVsAll[0][c1]=torch.nansum(mdPrime[m1,:][:,m2])
for c2 in range(numClasses):
m2 = classMasks[c2]
dirEnGlbClassWise[0][c1][c2]=torch.sum(d[m1,:][:,m2])
dirEnNbrClassWise[0][c1][c2]=torch.nansum(md[m1,:][:,m2])
dirEnDisClassWise[0][c1][c2]=torch.nansum(mdPrime[m1,:][:,m2])
if getMetric[1]:
madGlb = {k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
madNbr = {k: [0] * (self.numLayers+1) for k in ['All']+list(masks.keys())}
d = 1 - pairwise_cosine_similarity(x.detach().clone(),zero_diagonal=False)
md=torch.mul(adj,d)
madGlb['All'][0] = torch.mean(d)
madNbr['All'][0] = torch.nanmean(torch.sum(md,axis=1)/torch.count_nonzero(adj,axis=1))
for k,v in masks.items():
madGlb[k][0]=torch.mean(d[v,v])
madNbr[k][0]=torch.nanmean(torch.sum(md[v,:][:,v],axis=1)/torch.count_nonzero(adj[v,:][:,v],axis=1))
if classWise:
madGlbClassWise = torch.FloatTensor(self.numLayers+1,numClasses,numClasses)
madNbrClassWise = torch.FloatTensor(self.numLayers+1,numClasses,numClasses)
madDisClassWise = torch.FloatTensor(self.numLayers+1,numClasses,numClasses)
madGlb1ClsVsAll = torch.FloatTensor(self.numLayers+1,numClasses)
madNbr1ClsVsAll = torch.FloatTensor(self.numLayers+1,numClasses)
madDis1ClsVsAll = torch.FloatTensor(self.numLayers+1,numClasses)
mdPrime = torch.mul(1-adj,d)
for c1 in range(numClasses):
m1 = classMasks[c1]
m2 = torch.logical_not(m1)
madGlb1ClsVsAll[0][c1]=torch.mean(d[m1,:][:,m2])
madNbr1ClsVsAll[0][c1]=torch.nanmean(torch.sum(md[m1,:][:,m2],axis=1)/torch.count_nonzero(adj[m1,:][:,m2],axis=1))
madDis1ClsVsAll[0][c1]=torch.nanmean(torch.sum(mdPrime[m1,:][:,m2],axis=1)/torch.count_nonzero(adjPrime[m1,:][:,m2],axis=1))
for c2 in range(numClasses):
m2 = classMasks[c2]
madGlbClassWise[0][c1][c2]=torch.mean(d[m1,:][:,m2])
madNbrClassWise[0][c1][c2]=torch.nanmean(torch.sum(md[m1,:][:,m2],axis=1)/torch.count_nonzero(adj[m1,:][:,m2],axis=1))
madDisClassWise[0][c1][c2]=torch.nanmean(torch.sum(mdPrime[m1,:][:,m2],axis=1)/torch.count_nonzero(adjPrime[m1,:][:,m2],axis=1))
# for k,v in masks.items():
# md=torch.mul(adj[1][v,:],d[v,:])
# metrics[1][k][0] = torch.nanmean(torch.sum(md,axis=1)/torch.count_nonzero(adj[1][v,:],axis=1))
for i in range(self.numLayers):#len(self.GATv2Convs)-1):
#print(i,': ',self.GATv2Convs[i].printOmega())
if self.linLastLayer and i==self.numLayers-1:
x_new=self.layers[i](x)
else:
x_new,a = self.layers[i](x,edge_index,return_attention_weights=getSelfAttnCoef)
a=(a[0].detach(),a[1].detach())
attnCoef[i] = (a[1][torch.where(torch.eq(a[0][0],a[0][1])==True)[0]]).squeeze(-1) #record only self-attn-coefs (a_ii)
if getEntropy:
#print(a[0][0],a[0][1])
eqIdx = (a[0][0]==a[0][1]).nonzero().squeeze()
nodes = a[0][0][eqIdx]
entropy[i] = torch.zeros(len(nodes))
#print(eqIdx)
for n,node in enumerate(nodes):
selfIdx = eqIdx[n]
#print(selfIdx)
#print(a[0][0][selfIdx],node)
nbrsIdx = (a[0][1]==node).nonzero().squeeze()
#print(nbrsIdx)
#print(a[0][0][nbrsIdx])
# print(a[0][1][nbrsIdx])
nbrsIdx = nbrsIdx[nbrsIdx!=selfIdx]
#print(nbrsIdx)
#print(a[0][0][nbrsIdx])
#print(a[0][1][nbrsIdx])
deg = len(nbrsIdx)
eL=(1.0/deg)*((a[1][selfIdx]*torch.log2(a[1][selfIdx]))[0])
eR = (((deg-1)*1.0)/deg) * ((a[1][nbrsIdx]*torch.log2(a[1][nbrsIdx])).sum())
entropy[i][n] = -1*(eL+eR)
if self.useIdMap:
# print(i)
# print(x.shape)
# print(x_new.shape)
if i==0:#in [0,numLayers-1]:
x_new = x_new + self.residual[0](x)
elif i==self.numLayers-1:
x_new = x_new + self.residual[1](x)
else:
x_new = x + x_new
if self.useResLin:
x_new = x_new + self.residual[i](x)
x=x_new
if i <(self.numLayers-1):
x = self.activation(x)#x.relu() #F.relu(x,inplace=True)
if self.dropout>0:
x = F.dropout(x, p=self.dropout, training=self.training)
#x,a = self.GATv2Convs[len(self.GATv2Convs)-1](x,edge_index,return_attention_weights=getAttnCoef)
with torch.no_grad():
if getMetric[0]:
#d=0
d = sq_euclidean_dist(x.detach().clone(),x.detach().clone())#torch.square(pairwise_euclidean_distance(x.detach().clone()))
md=torch.mul(adj,d)
dirEnGlb['All'][i+1] = torch.sum(d)/2
dirEnNbr['All'][i+1] = torch.nansum(md)/2#torch.nanmean(torch.sum(md,axis=1)/torch.count_nonzero(adj[1],axis=1))
for k,v in masks.items():
dirEnGlb[k][i+1]=torch.sum(d[v,v])/2
#dirEnLcl[k][0]=torch.sum(md[v,:])
dirEnNbr[k][i+1]=torch.nansum(md[v,:][:,v])/2 #torch.nanmean(torch.sum(md[v,:],axis=1)/torch.count_nonzero(adj[1][v,:],axis=1))
if classWise:
mdPrime = torch.mul(1-adj,d)
for c1 in range(numClasses):
m1 = classMasks[c1]
m2 = torch.logical_not(m1)
dirEnGlb1ClsVsAll[i+1][c1]=torch.sum(d[m1,:][:,m2])
dirEnNbr1ClsVsAll[i+1][c1]=torch.nansum(md[m1,:][:,m2])
dirEnDis1ClsVsAll[i+1][c1]=torch.nansum(mdPrime[m1,:][:,m2])
for c2 in range(numClasses):
m2 = classMasks[c2]
dirEnGlbClassWise[i+1][c1][c2]=torch.sum(d[m1,:][:,m2])
dirEnNbrClassWise[i+1][c1][c2]=torch.nansum(md[m1,:][:,m2])
dirEnDisClassWise[i+1][c1][c2]=torch.nansum(mdPrime[m1,:][:,m2])
if getMetric[1]:
d = 1 - pairwise_cosine_similarity(x.detach().clone(),zero_diagonal=False)
md=torch.mul(adj,d)
madGlb['All'][i+1] = torch.mean(d)
madNbr['All'][i+1] = torch.nanmean(torch.sum(md,axis=1)/torch.count_nonzero(adj,axis=1))
for k,v in masks.items():
madGlb[k][i+1]=torch.mean(d[v,v])
madNbr[k][i+1]=torch.nanmean(torch.sum(md[v,:][:,v],axis=1)/torch.count_nonzero(adj[v,:][:,v],axis=1))
if classWise:
mdPrime = torch.mul(1-adj,d)
for c1 in range(numClasses):
m1 = classMasks[c1]
m2 = torch.logical_not(m1)
madGlb1ClsVsAll[i+1][c1]=torch.mean(d[m1,:][:,m2])
madNbr1ClsVsAll[i+1][c1]=torch.nanmean(torch.sum(md[m1,:][:,m2],axis=1)/torch.count_nonzero(adj[m1,:][:,m2],axis=1))
madDis1ClsVsAll[i+1][c1]=torch.nanmean(torch.sum(mdPrime[m1,:][:,m2],axis=1)/torch.count_nonzero(adjPrime[m1,:][:,m2],axis=1))
for c2 in range(numClasses):
m2 = classMasks[c2]
madGlbClassWise[i+1][c1][c2]=torch.mean(d[m1,:][:,m2])
madNbrClassWise[i+1][c1][c2]=torch.nanmean(torch.sum(md[m1,:][:,m2],axis=1)/torch.count_nonzero(adj[m1,:][:,m2],axis=1))
madDisClassWise[i+1][c1][c2]=torch.nanmean(torch.sum(mdPrime[m1,:][:,m2],axis=1)/torch.count_nonzero(adjPrime[m1,:][:,m2],axis=1))
#attnCoef[len(self.GATv2Convs)-1] = (a[0].detach(),a[1].adj())
smoothnessMetrics={
'dirEnGl':dirEnGlb,
'dirEnNb':dirEnNbr,
'madGl':madGlb,
'madNb':madNbr,
'dirEnGlClassWise':dirEnGlbClassWise,
'dirEnNbClassWise':dirEnNbrClassWise,
'dirEnDiClassWise':dirEnDisClassWise,
'madGlClassWise':madGlbClassWise,
'madNbClassWise':madNbrClassWise,
'madDiClassWise':madDisClassWise,
'dirEnGl1ClsVsAll':dirEnGlb1ClsVsAll,
'dirEnNb1ClsVsAll':dirEnNbr1ClsVsAll,
'dirEnDi1ClsVsAll':dirEnDis1ClsVsAll,
'madGl1ClsVsAll':madGlb1ClsVsAll,
'madNb1ClsVsAll':madNbr1ClsVsAll,
'madDi1ClsVsAll':madDis1ClsVsAll
}
return x,attnCoef,smoothnessMetrics,entropy
def computeStatSumry(arr,quantiles):
r = {'mean': arr.mean(),
'std': arr.std()}
quantiles=torch.cat((torch.tensor([0,1],device=device),quantiles),dim=0)
p = torch.quantile(arr,quantiles)
r['min'] = p[0]
r['max'] = p[1]
for i in range(2,len(quantiles)):
r[str(int(quantiles[i]*100))+'%ile'] = p[i]
return r
def computeAlphaStatSumry(alphas,quantiles):
return [computeStatSumry(alphas[1][np.where(np.equal(alphas[0][0],alphas[0][1])==True)[0]],quantiles),
computeStatSumry(alphas[1][np.where(np.equal(alphas[0][0],alphas[0][1])==False)[0]],quantiles)]
def printExpSettings(expID,expSetting):
print('Exp: '+str(expID))
for k,v in expSetting.items():
for k2,v2 in expSetting[k].items():
if(k2==expID):
print(k,': ',v2)
def set_seeds(seed):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def initializeParams(params,initScheme,initA1, initA2, activation, paramTypes, attnParamTypes):
numLayers = len(params)
with torch.no_grad():
d = {'attn':initA1, 'attn2':initA2}
for k in attnParamTypes:
v = d[k]
for l in range(numLayers):
if k in params[l].keys():
if v=='XavN':
torch.nn.init.xavier_normal_(params[l][k].data)
elif v=='posXavN':
torch.nn.init.xavier_normal_(params[l][k].data)
params[l][k].data = torch.abs(params[l][k].data)
elif v=='negXavN':
torch.nn.init.xavier_normal_(params[l][k].data)
params[l][k].data = -1*torch.abs(params[l][k].data)
elif v=='negXavN*2':
torch.nn.init.xavier_normal_(params[l][k].data)
params[l][k].data = -2*torch.abs(params[l][k].data)
elif v=='XavU' or v=='posXavU': #U in (0,1)
torch.nn.init.xavier_uniform_(params[l][k].data)
elif v=='negXavU':
torch.nn.init.xavier_uniform_(params[l][k].data)
params[l][k].data = -1*(params[l][k].data)
else:
v=float(v)
params[l][k].data.fill_(v)
if(initScheme[:2]!='LL'):
for l in range(numLayers):
for f in paramTypes:#)-set(['attn','attn2']):
if(initScheme=='xavierN'):
torch.nn.init.xavier_normal_(params[l][f].data)
if(initScheme=='xavierU'):
torch.nn.init.xavier_uniform_(params[l][f].data)
if(initScheme=='kaimingN'):
torch.nn.init.kaiming_normal_(params[l][f].data,mode='fan_in',nonlinearity=activation)
if(initScheme=='kaimingU'):
torch.nn.init.kaiming_uniform_(params[l][f].data,mode='fan_in',nonlinearity=activation)
elif(initScheme[:2]=='LL'):
#attn params defined above globally
#for l in range(numLayers):
#torch.nn.init.xavier_normal_(params[l]['attn'].data)#.fill_(0)
#torch.nn.init.xavier_normal_(params[l]['attn2'].data)#.fill_(0)# = torch.zeros(params[l]['attn2'].data.shape,device=device) ##LL attnWeights are 0
#params[l]['attn'].data.fill_(-1)
#params[l]['attn2'].data.fill_(0)
for f in paramTypes:#)-set(['attn','attn2']):
#print(l,f,params[0][f].data,params[0][f])
firstLayerDeltaDim = (ceil(params[0][f].data.shape[0]/2),params[0][f].data.shape[1])
if initScheme=='LLxavierU':
firstLayerDelta = torch.nn.init.xavier_uniform_(torch.empty(firstLayerDeltaDim[0],firstLayerDeltaDim[1],device=device))
if initScheme=='LLxavierN':
firstLayerDelta = torch.nn.init.xavier_normal_(torch.empty(firstLayerDeltaDim[0],firstLayerDeltaDim[1],device=device))
if initScheme=='LLkaimingU':
firstLayerDelta = torch.nn.init.kaiming_uniform_(torch.empty(firstLayerDeltaDim[0],firstLayerDeltaDim[1],device=device),nonlinearity=activation)
if initScheme=='LLkaimingN':
firstLayerDelta = torch.nn.init.kaiming_normal_(torch.empty(firstLayerDeltaDim[0],firstLayerDeltaDim[1],device=device),nonlinearity=activation)
if initScheme=='LLortho':
firstLayerDelta = torch.nn.init.orthogonal_(torch.empty(firstLayerDeltaDim[0],firstLayerDeltaDim[1],device=device))
if initScheme=='LLidentity':
firstLayerDelta = torch.nn.init.eye_(torch.empty(firstLayerDeltaDim[0],firstLayerDeltaDim[1],device=device))
if initScheme!='LLidentityHid':
params[0][f].data = torch.cat((firstLayerDelta,-firstLayerDelta),dim=0) #BUG CHECK
if f in params[numLayers-1].keys():
finalLayerDeltaDim= (params[numLayers-1][f].data.shape[0],ceil(params[numLayers-1][f].data.shape[1]/2))
if initScheme=='LLxavierU':
finalLayerDelta = torch.nn.init.xavier_uniform_(torch.empty(finalLayerDeltaDim[0],finalLayerDeltaDim[1],device=device))
if initScheme=='LLxavierN':
finalLayerDelta = torch.nn.init.xavier_normal_(torch.empty(finalLayerDeltaDim[0],finalLayerDeltaDim[1],device=device))
if initScheme=='LLkaimingU':
finalLayerDelta = torch.nn.init.kaiming_uniform_(torch.empty(finalLayerDeltaDim[0],finalLayerDeltaDim[1],device=device),nonlinearity=activation)
if initScheme=='LLkaimingN':
finalLayerDelta = torch.nn.init.kaiming_normal_(torch.empty(finalLayerDeltaDim[0],finalLayerDeltaDim[1],device=device),nonlinearity=activation)
if initScheme=='LLortho':
finalLayerDelta = torch.nn.init.orthogonal_(torch.empty(finalLayerDeltaDim[0],finalLayerDeltaDim[1],device=device))
if initScheme=='LLidentity':
finalLayerDelta = torch.nn.init.eye_(torch.empty(finalLayerDeltaDim[0],finalLayerDeltaDim[1],device=device))
if initScheme!='LLidentityHid':
params[numLayers-1][f].data = torch.cat((finalLayerDelta,-finalLayerDelta),dim=1) #BUG CHECK
for l in range(1,numLayers-1):
for f in paramTypes:#)-set(['attn','attn2']):
dim = params[l][f].data.shape
if initScheme=='LLxavierU':
delta = torch.nn.init.xavier_uniform_(torch.empty(ceil(dim[0]/2),ceil(dim[1]/2),device=device))
if initScheme=='LLxavierN':
delta = torch.nn.init.xavier_normal_(torch.empty(ceil(dim[0]/2),ceil(dim[1]/2),device=device))
if initScheme=='LLkaimingU':
delta = torch.nn.init.kaiming_uniform_(torch.empty(ceil(dim[0]/2),ceil(dim[1]/2),device=device),nonlinearity=activation)
if initScheme=='LLkaimingN':
delta = torch.nn.init.kaiming_normal_(torch.empty(ceil(dim[0]/2),ceil(dim[1]/2),device=device),nonlinearity=activation)
if initScheme=='LLortho':
delta = torch.nn.init.orthogonal_(torch.empty(ceil(dim[0]/2),ceil(dim[1]/2),device=device))
if initScheme=='LLidentity' or initScheme=='LLidentityHid':
delta = torch.nn.init.eye_(torch.empty(ceil(dim[0]/2),ceil(dim[1]/2),device=device))
delta = torch.cat((delta, -delta), dim=0)
delta = torch.cat((delta, -delta), dim=1)
params[l][f].data = delta
# if(initScheme=='xavrWzeroA'):
# for l in range(numLayers):
# torch.nn.init.zeros_(params[l]['attn'].data)
# for f in set(paramTypes)-set(['attn','attn2']):
# torch.nn.init.xavier_normal_(params[l][f].data)
if(initScheme=='identityAll'):
#for l in range(numLayers):
#torch.nn.init.zeros_(params[l]['attn'].data)
for f in paramTypes:#)-set(['attn','attn2']):
torch.nn.init.eye_(params[l][f].data)
if(initScheme=='identityHid'):
#for l in range(numLayers):
#torch.nn.init.zeros_(params[l]['attn'].data)
for f in paramTypes:#)-set(['attn','attn2']):
torch.nn.init.xavier_normal_(params[0][f].data)
torch.nn.init.xavier_normal_(params[numLayers-1][f].data)
for l in range(1,numLayers-1):
for f in set(paramTypes)-set(['attn','attn2']):
torch.nn.init.eye_(params[l][f].data)
if(initScheme=='LLidentityHid'):
#for l in range(numLayers):
#torch.nn.init.zeros_(params[l]['attn'].data)
for f in paramTypes:#)-set(['attn','attn2']):
torch.nn.init.xavier_normal_(params[0][f].data)
torch.nn.init.xavier_normal_(params[numLayers-1][f].data)
for l in range(numLayers):
for f in paramTypes+attnParamTypes:
if f in params[l].keys():
params[l][f].data.requires_grad=True #because of initialization update
return params
def deepCopyParamsToNumpy(params):
paramsCopy = [{} for i in range(len(params))]