-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_transformer.py
249 lines (198 loc) · 9.61 KB
/
models_transformer.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
import utils as u
import torch
from torch.nn.parameter import Parameter
import torch.nn as nn
import torch.nn.functional as F
import dgl
import math
from graph_transformer_layer import GraphTransformerLayer
from mlp_readout_layer import MLPReadout
from models import Sp_GCN
from tst import Transformer as TemporalTransformer
class GraphTransformerNet(nn.Module):
def __init__(self, net_params, device='cpu'):
super().__init__()
# in_dim_node = net_params.in_dim # node_dim (feat is an integer)
in_dim_node = net_params.feats_per_node
hidden_dim = net_params.hidden_dim
out_dim = net_params.out_dim
n_classes = net_params.n_classes
num_heads = net_params.n_heads
in_feat_dropout = net_params.in_feat_dropout
dropout = net_params.dropout
n_layers = net_params.L
self.readout = net_params.readout
self.layer_norm = net_params.layer_norm
self.batch_norm = net_params.batch_norm
self.residual = net_params.residual
self.dropout = dropout
self.n_classes = n_classes
# self.device = net_params.device
self.device = device
self.lap_pos_enc = net_params.lap_pos_enc
self.wl_pos_enc = net_params.wl_pos_enc
max_wl_role_index = 100
if self.lap_pos_enc:
pos_enc_dim = net_params.pos_enc_dim
self.embedding_lap_pos_enc = nn.Linear(pos_enc_dim, hidden_dim)
if self.wl_pos_enc:
self.embedding_wl_pos_enc = nn.Embedding(max_wl_role_index, hidden_dim)
self.use_2_hot_node_feats = net_params.use_2_hot_node_feats
self.use_1_hot_node_feats = net_params.use_1_hot_node_feats
if net_params.use_2_hot_node_feats or net_params.use_1_hot_node_feats:
self.embedding_h = nn.Embedding(in_dim_node, hidden_dim) # node feat is an integer
else:
self.embedding_h = nn.Linear(in_dim_node, hidden_dim) # node feat is an integer
self.in_feat_dropout = nn.Dropout(in_feat_dropout)
self.layers = nn.ModuleList([GraphTransformerLayer(hidden_dim, hidden_dim, num_heads,
dropout, self.layer_norm, self.batch_norm, self.residual) for _ in range(n_layers-1)])
self.layers.append(GraphTransformerLayer(hidden_dim, out_dim, num_heads, dropout, self.layer_norm, self.batch_norm, self.residual))
# self.MLP_layer = MLPReadout(out_dim, n_classes)
def forward(self, g, h, e=None, h_lap_pos_enc=None, h_wl_pos_enc=None):
# input embedding
h = self.embedding_h(h)
if self.lap_pos_enc:
h_lap_pos_enc = self.embedding_lap_pos_enc(h_lap_pos_enc.float())
h = h + h_lap_pos_enc
if self.wl_pos_enc:
h_wl_pos_enc = self.embedding_wl_pos_enc(h_wl_pos_enc)
h = h + h_wl_pos_enc
h = self.in_feat_dropout(h)
# GraphTransformer Layers
for conv in self.layers:
h = conv(g, h)
# output
# h_out = self.MLP_layer(h)
return h_out
def loss(self, pred, label):
# calculating label weights for weighted loss computation
V = label.size(0)
label_count = torch.bincount(label)
label_count = label_count[label_count.nonzero()].squeeze()
cluster_sizes = torch.zeros(self.n_classes).long().to(self.device)
cluster_sizes[torch.unique(label)] = label_count
weight = (V - cluster_sizes).float() / V
weight *= (cluster_sizes>0).float()
# weighted cross-entropy for unbalanced classes
criterion = nn.CrossEntropyLoss(weight=weight)
loss = criterion(pred, label)
return loss
class Transformer_LSTM(GraphTransformerNet):
def __init__(self,transformer_args,gcn_args,activation, device='cpu', lappe=True, timepe=False):
super().__init__(transformer_args,device)
# TODO: Define net params
# conv = GraphTransformerNet(args)
self.rnn = nn.LSTM(
# input_size=gcn_args.layer_2_feats,
input_size=transformer_args.out_dim,
hidden_size=gcn_args.lstm_l2_feats,
num_layers=gcn_args.lstm_l2_layers
)
# TODO: handle if Nodes_list is None
# TODO: check A_list (and Ahat)
# def forward(self,A_list, Nodes_list = None, nodes_mask_list = None):
def forward(self,A_list, Nodes_list, nodes_mask_list = None, graph_list=[], pos_enc_list=[]):
last_l_seq=[]
for t,Ahat in enumerate(A_list):
# if isinstance(Nodes_list[t],torch.Tensor):
# node_feats = Nodes_list[t]
# else:
# node_feats = Nodes_list[t]._indices()[1,:]
if self.use_2_hot_node_feats or self.use_1_hot_node_feats:
node_feats = Nodes_list[t]._indices()[1,:]
else:
node_feats = Nodes_list[t]
# A_list: T, each element sparse tensor
last_l = self.embedding_h(node_feats)
if self.lap_pos_enc:
pos_enc = pos_enc_list[t]
pos_enc_emb = self.embedding_lap_pos_enc(pos_enc)
last_l = last_l + pos_enc_emb
# GraphTransformer Layers
# TODO: make dgl graph from Ahat?
if len(graph_list)==0:
g = Ahat
else:
g = graph_list[t]
for conv in self.layers:
last_l = conv(g, last_l)
last_l_seq.append(last_l)
last_l_seq = torch.stack(last_l_seq)
out, _ = self.rnn(last_l_seq, None)
return out[-1]
class GCN_Transformer(Sp_GCN):
def __init__(self,transformer_args,gcn_args,activation, device='cpu', lappe=False, timepe=True):
super().__init__(gcn_args,activation)
# TODO: handle custom time position encoding
self.time_transformer = TemporalTransformer(gcn_args.layer_2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_layers, chunk_mode=None, pe='original', pe_period=None, use_decoder=transformer_args.tst_use_decoder)
# , pe_period=transformer_args.pe_period)
def forward(self,A_list, Nodes_list = None, nodes_mask_list = None):
last_l_seq=[]
for t,Ahat in enumerate(A_list):
node_feats = Nodes_list[t]
#A_list: T, each element sparse tensor
#note(bwheatman, tfk): change order of matrix multiply
last_l = self.activation(Ahat.matmul(node_feats.matmul(self.w_list[0])))
for i in range(1, self.num_layers):
last_l = self.activation(Ahat.matmul(last_l.matmul(self.w_list[i])))
last_l_seq.append(last_l)
last_l_seq = torch.stack(last_l_seq)
# TODO: check/test
last_l_seq = last_l_seq.permute((1,0,2))
out = self.time_transformer(last_l_seq)
out = out.permute((1,0,2))
return out[-1]
class Spatio_Temporal_Transformer(GraphTransformerNet):
def __init__(self, transformer_args, gcn_args, activation, device='cpu', lappe=True, timepe=True):
# super().__init__()
super().__init__(transformer_args,device)
self.time_transformer = TemporalTransformer(transformer_args.out_dim, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_feats, transformer_args.tst_l2_layers, chunk_mode=None, pe='original', pe_period=None, use_decoder=transformer_args.tst_use_decoder)
# def parameters(self):
# return self._parameters
def forward(self,A_list, Nodes_list, nodes_mask_list = None, graph_list=[], pos_enc_list=[]):
last_l_seq=[]
for t,Ahat in enumerate(A_list):
node_feats = Nodes_list[t]._indices()[1,:]
# A_list: T, each element sparse tensor
last_l = self.embedding_h(node_feats)
if self.lap_pos_enc:
pos_enc = pos_enc_list[t]
pos_enc_emb = self.embedding_lap_pos_enc(pos_enc)
last_l = last_l + pos_enc_emb
# GraphTransformer Layers
# TODO: make dgl graph from Ahat?
if len(graph_list)==0:
g = Ahat
else:
g = graph_list[t]
for conv in self.layers:
last_l = conv(g, last_l)
last_l_seq.append(last_l)
last_l_seq = torch.stack(last_l_seq)
last_l_seq = last_l_seq.permute((1,0,2))
out = self.time_transformer(last_l_seq)
out = out.permute((1,0,2))
return out[-1]
class StaticGraphTransformer(GraphTransformerNet):
def __init__(self,transformer_args,gcn_args,activation, device='cpu', lappe=True, timepe=False):
super().__init__(transformer_args, device)
def forward(self,A_list, Nodes_list, nodes_mask_list, graph_list=[], pos_enc_list=[]):
if self.use_2_hot_node_feats or self.use_1_hot_node_feats:
node_feats = Nodes_list[-1]._indices()[1,:]
else:
node_feats = Nodes_list[-1]
# A_list: T, each element sparse tensor
last_l = self.embedding_h(node_feats)
if self.lap_pos_enc:
pos_enc = pos_enc_list[-1]
pos_enc_emb = self.embedding_lap_pos_enc(pos_enc)
last_l = last_l + pos_enc_emb
# GraphTransformer Layers
# TODO: make dgl graph from Ahat?
if len(graph_list)==0:
g = Ahat
else:
g = graph_list[-1]
for conv in self.layers:
last_l = conv(g, last_l)
return last_l