-
Notifications
You must be signed in to change notification settings - Fork 5
/
graph_nns.py
484 lines (356 loc) · 17.6 KB
/
graph_nns.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
import torch, torch_geometric, math
from torch.nn import *
from torch_geometric.nn import MessagePassing, GlobalAttention, TransformerConv, GATv2Conv, LayerNorm, global_mean_pool, global_max_pool
from torch.nn.functional import leaky_relu
from config import config
def positional_encoding(pos, dim):
w = torch.exp(torch.arange(0, dim, 2, device=pos.device) * (-math.log(10000.0) / dim))
pos_w = pos.unsqueeze(1) * w
pe = torch.zeros(len(pos), dim, device=pos.device)
pe[:, 0::2] = torch.sin(pos_w)
pe[:, 1::2] = torch.cos(pos_w)
return pe
# ----------------------------------------------------------------------------------------
class MultiMessagePassingWithAttention(Module):
def __init__(self, steps):
super().__init__()
self.gnns = ModuleList( [GraphNet() for i in range(steps)] )
self.att = ModuleList( [GATv2Conv(config.emb_dim, config.emb_dim, add_self_loops=False, heads=3, concat=False) for i in range(steps - 1)] )
self.pooling = GlobalPooling()
self.steps = steps
def forward(self, x, edge_attr, edge_index, batch_ind, num_graphs, data_lens):
x_att = torch.zeros(len(x), config.emb_dim, device=config.device) # this can encode context
edge_complete = complete_graph(data_lens)
for i in range(self.steps):
x = self.gnns[i](x, edge_attr, edge_index, x_att, batch_ind)
if i < self.steps - 1:
x_att = leaky_relu( self.att[i](x, edge_complete) )
x_global = self.pooling(x, batch_ind)
return x, x_global
# ----------------------------------------------------------------------------------------
class MultiMessagePassingWithTransformer(Module):
def __init__(self, steps):
super().__init__()
self.gnns = ModuleList( [GraphNet() for i in range(steps)] )
self.att = ModuleList( [TransformerConv(config.emb_dim, config.emb_dim, heads=3, concat=False) for i in range(steps - 1)] )
self.pooling = GlobalPooling()
self.steps = steps
def forward(self, x, edge_attr, edge_index, batch_ind, num_graphs, data_lens):
x_att = torch.zeros(len(x), config.emb_dim, device=config.device) # this can encode context
edge_complete = complete_graph(data_lens)
for i in range(self.steps):
x = self.gnns[i](x, edge_attr, edge_index, x_att, batch_ind)
if i < self.steps - 1:
x_att = leaky_relu( self.att[i](x, edge_complete) )
x_global = self.pooling(x, batch_ind)
return x, x_global
# ----------------------------------------------------------------------------------------
class MultiMessagePassingWithGlobalNode(Module):
def __init__(self, steps):
super().__init__()
self.gnns = ModuleList( [GraphNet() for i in range(steps)] )
self.pools = ModuleList( [GlobalNode() for i in range(steps)] )
# self.layer_norm = ModuleList( [LayerNorm(config.emb_dim) for i in range(steps)] )
self.steps = steps
def forward(self, x, xg_init, edge_attr, edge_index, batch_ind, num_graphs, data_lens):
if xg_init is None:
x_global = torch.zeros(num_graphs, config.emb_dim, device=config.device)
else:
x_global = x_init
for i in range(self.steps):
x = self.gnns[i](x, edge_attr, edge_index, x_global, batch_ind)
# x = self.layer_norm[i](x)
x_global = self.pools[i](x_global, x, batch_ind)
return x, x_global
# ----------------------------------------------------------------------------------------
class MultiMessagePassingWithGlobalNodeAndLastGRUGlobal(Module): # GRU in the global node
def __init__(self, steps):
super().__init__()
self.gnns = ModuleList( [GraphNet() for i in range(steps)] )
self.pools = ModuleList( [GlobalNode() for i in range(steps)] )
# self.layer_norm = ModuleList( [LayerNorm(config.emb_dim) for i in range(steps)] )
self.gru = GRU(input_size=config.emb_dim, hidden_size=config.emb_dim)
self.hidden = None
self.steps = steps
def forward(self, x, xg_init, edge_attr, edge_index, batch_ind, num_graphs, data_lens):
if xg_init is None:
x_global = torch.zeros(num_graphs, config.emb_dim, device=config.device)
else:
x_global = x_init
# optionally initialize the hidden state
if self.hidden is None:
self.hidden = torch.zeros((1, num_graphs, config.emb_dim), device=config.device)
for i in range(self.steps):
x = self.gnns[i](x, edge_attr, edge_index, x_global, batch_ind)
# x = self.layer_norm[i](x)
x_global = self.pools[i](x_global, x, batch_ind)
if i == self.steps - 2: # pre-last
x_global, self.hidden = self.gru(x_global.unsqueeze(0), self.hidden)
x_global = x_global.squeeze(0)
return x, x_global
def reset_state(self, batch_mask):
if self.hidden is not None:
if batch_mask is None: # reset all
self.hidden = None
else:
# reset_idx = np.where(batch_mask)
zeros = torch.zeros_like(self.hidden, device=config.device)
condition = torch.zeros_like(self.hidden, dtype=torch.bool, device=config.device) # TODO: not optimal...
condition[0, batch_mask] = True
self.hidden = torch.where(condition, zeros, self.hidden) # reset the marked places
# def detach_state(self):
# if self.hidden is not None:
# self.hidden = self.hidden.detach()
def clone_state(self, other):
if other.hidden is None:
self.hidden = None
else:
self.hidden = other.hidden.clone().detach()
# ----------------------------------------------------------------------------------------
class MultiMessagePassingWithGlobalNodeAndAllGRUGlobal(Module): # GRU in the global node
def __init__(self, steps):
super().__init__()
self.gnns = ModuleList( [GraphNet() for i in range(steps)] )
self.pools = ModuleList( [GlobalNode() for i in range(steps)] )
# self.layer_norm = ModuleList( [LayerNorm(config.emb_dim) for i in range(steps)] )
self.grus = ModuleList( [GRU(input_size=config.emb_dim, hidden_size=config.emb_dim) for i in range(steps)] )
self.hiddens = [None for i in range(steps)]
# self.gru = GRU(input_size=config.emb_dim, hidden_size=config.emb_dim)
# self.hidden = None
self.steps = steps
def forward(self, x, xg_init, edge_attr, edge_index, batch_ind, num_graphs, data_lens):
if xg_init is None:
x_global = torch.zeros(num_graphs, config.emb_dim, device=config.device)
else:
x_global = x_init
# optionally initialize the hidden state
if self.hiddens[0] is None:
for i in range(self.steps):
self.hiddens[i] = torch.zeros((1, num_graphs, config.emb_dim), device=config.device)
for i in range(self.steps):
x = self.gnns[i](x, edge_attr, edge_index, x_global, batch_ind)
# x = self.layer_norm[i](x)
x_global = self.pools[i](x_global, x, batch_ind)
x_global, self.hiddens[i] = self.grus[i](x_global.unsqueeze(0), self.hiddens[i])
x_global = x_global.squeeze(0)
# if i == self.steps - 2: # pre-last
# if i == 0: # first
# x_global, self.hidden = self.gru(x_global.unsqueeze(0), self.hidden)
# x_global = x_global.squeeze(0)
return x, x_global
def reset_state(self, batch_mask):
if self.hiddens[0] is not None:
if batch_mask is None: # reset all
for i in range(self.steps):
self.hiddens[i] = None
else:
for i in range(self.steps):
# reset_idx = np.where(batch_mask)
zeros = torch.zeros_like(self.hiddens[i], device=config.device)
condition = torch.zeros_like(self.hiddens[i], dtype=torch.bool, device=config.device) # TODO: not optimal...
condition[0, batch_mask] = True
self.hiddens[i] = torch.where(condition, zeros, self.hiddens[i]) # reset the marked places
# def detach_state(self):
# if self.hidden is not None:
# self.hidden = self.hidden.detach()
def clone_state(self, other):
if other.hiddens[0] is None:
for i in range(self.steps):
self.hiddens[i] = None
else:
for i in range(self.steps):
self.hiddens[i] = other.hiddens[i].clone().detach()
# ----------------------------------------------------------------------------------------
class GlobalNode(Module):
def __init__(self):
super().__init__()
att_mask = Linear(config.emb_dim, 1)
att_feat = Sequential( Linear(config.emb_dim, config.emb_dim), LeakyReLU() )
self.glob = GlobalAttention(att_mask, att_feat)
self.tranform = Sequential( Linear(config.emb_dim + config.emb_dim, config.emb_dim), LeakyReLU() )
def forward(self, xg_prev, x, batch_ind):
xg = self.glob(x, batch_ind)
xg = torch.cat([xg, xg_prev], dim=1)
xg = self.tranform(xg) + xg_prev # skip connection
return xg
# ----------------------------------------------------------------------------------------
class GlobalPooling(Module):
def __init__(self):
super().__init__()
att_mask = Linear(config.emb_dim, 1)
att_feat = Sequential( Linear(config.emb_dim, config.emb_dim), LeakyReLU() )
self.glob = GlobalAttention(att_mask, att_feat)
self.transform = Sequential( Linear(config.emb_dim, config.emb_dim), LeakyReLU() )
def forward(self, x, batch_ind):
x = self.glob(x, batch_ind)
x = self.transform(x)
return x
# ----------------------------------------------------------------------------------------
class GraphNet(MessagePassing):
def __init__(self):
super().__init__(aggr='max')
# self.f_mess = Sequential( Linear(config.emb_dim + config.edge_dim, config.emb_dim), LeakyReLU(),
# Linear(config.emb_dim, config.emb_dim), LeakyReLU(),
# Linear(config.emb_dim, config.emb_dim), LeakyReLU())
# self.f_agg = Sequential( Linear(config.emb_dim + config.emb_dim + config.emb_dim, config.emb_dim), LeakyReLU(),
# Linear(config.emb_dim, config.emb_dim), LeakyReLU(),
# Linear(config.emb_dim, config.emb_dim), LeakyReLU())
self.f_mess = Sequential( Linear(config.emb_dim + config.edge_dim, config.emb_dim), LeakyReLU() )
self.f_agg = Sequential( Linear(config.emb_dim + config.emb_dim + config.emb_dim, config.emb_dim), LeakyReLU() )
def forward(self, x, edge_attr, edge_index, xg, batch_ind):
xg = xg[batch_ind] # expand
return self.propagate(edge_index, x=x, edge_attr=edge_attr, xg=xg)
def message(self, x_j, edge_attr):
if edge_attr is not None:
z = torch.cat([x_j, edge_attr], dim=1)
else:
z = x_j
z = self.f_mess(z)
return z
def update(self, aggr_out, x, xg):
z = torch.cat([x, xg, aggr_out], dim=1)
z = self.f_agg(z) + x # skip connection
return z
# ------------------------------------------------------------------------------------------
class MultiSequential(Sequential):
def forward(self, *inputs):
for module in self._modules.values():
if type(inputs) == tuple:
inputs = module(*inputs)
else:
inputs = module(inputs)
return inputs
# ------------------------------------------------------------------------------------------
class MultiGATv2(Module):
def __init__(self, input_size, output_size, edge_size, layers):
super().__init__()
self.layers = []
for l_id in range(layers):
if l_id == 0:
in_size = input_size
else:
in_size = mid_size
self.layers.append(MultiSequential(
GATv2Conv(in_size, output_size),
LeakyReLU()
))
self.layers = ModuleList(self.layers)
def forward(self, x, edge_index, edge_attr):
for l in self.layers:
x = l(x, edge_index, edge_attr)
return x
# ------------------------------------------------------------------------------------------
class MultiTransformer(Module):
def __init__(self, input_size, output_size, edge_size, layers, heads, mean_at_end=False):
super().__init__()
self.layers = []
mid_size = output_size * heads
for l_id in range(layers):
if l_id == 0:
in_size = input_size
else:
in_size = mid_size
concat = True
if l_id == layers - 1: # last
concat = not mean_at_end
self.layers.append(MultiSequential(
TransformerConv(in_size, output_size, heads, concat=concat, beta=True, edge_dim=edge_size),
LayerNorm(mid_size),
LeakyReLU()
))
self.layers = ModuleList(self.layers)
def forward(self, x, edge_index, edge_attr):
for l in self.layers:
x = l(x, edge_index, edge_attr)
return x
# ------------------------------------------------------------------------------------------
def complete_matrix(data_lens):
size = sum(data_lens)
complete_adj = torch.zeros(size, size)
start = 0
for l in data_lens:
complete_adj[start:start+l,start:start+l] = 1
start += l
# complete_adj -= torch.eye(size) # remove self-connections
return complete_adj.unsqueeze(0)
def complete_graph(data_lens):
complete_adj = complete_matrix(data_lens)
edge_index_complete, _ = torch_geometric.utils.dense_to_sparse(complete_adj)
edge_index_complete = edge_index_complete.to(config.device)
return edge_index_complete
# ------------------------------------------------------------------------------------------
class MultiAlternatingTransformer(Module):
def __init__(self, input_size, output_size, edge_size, layers, heads, mean_at_end=False):
super().__init__()
self.layers = []
mid_size = output_size * heads
for l_id in range(layers):
if l_id == 0:
in_size = input_size
else:
in_size = mid_size
concat = True
if l_id == layers - 1: # last
concat = not mean_at_end
edge_dim = edge_size if l_id % 2 == 0 else None
self.layers.append(MultiSequential(
TransformerConv(in_size, output_size, heads, concat=concat, beta=True, edge_dim=edge_dim),
LayerNorm(mid_size),
LeakyReLU()
))
self.layers = ModuleList(self.layers)
def forward(self, x, edge_index, edge_attr, data_lens):
# inefficient way
edge_index_complete = complete_graph(data_lens)
for l_id, l in enumerate(self.layers):
if l_id % 2 == 0:
x = l(x, edge_index, edge_attr)
else:
x = l(x, edge_index_complete, None)
return x
# # ------------------------------------------------------------------------------------------
# from custom_lstm import LayerNormLSTMCell
# class MultiTransformerLSTM(Module):
# def __init__(self, input_size, output_size, edge_size, layers, heads):
# super().__init__()
# self.layers = []
# mid_size = output_size * heads
# for i in range(layers):
# if i == 0:
# self.layers.append(
# MultiSequential(TransformerConv(input_size, output_size, heads, concat=True, beta=True, edge_dim=edge_size),
# LayerNorm(mid_size), LeakyReLU()))
# else:
# self.layers.append(
# MultiSequential(TransformerConv(mid_size, output_size, heads, concat=True, beta=True, edge_dim=edge_size),
# LayerNorm(mid_size), LeakyReLU()))
# self.layers = ModuleList(self.layers)
# self.lstm = LayerNormLSTMCell(input_size=mid_size, hidden_size=output_size) # yes, it's reversed here
# self.hc = None
# def forward(self, x, edge_index, edge_attr):
# for l in self.layers:
# x = l(x, edge_index, edge_attr)
# if self.hc is None:
# self.hc = (torch.zeros(len(x), self.lstm.hidden_size, device=config.device), torch.zeros(len(x), self.lstm.hidden_size, device=config.device))
# x, self.hc = self.lstm(x, self.hc)
# return x
# def reset_state(self, batch_mask):
# if self.hc is not None:
# if batch_mask is None: # reset all
# self.hc = None
# else:
# batch_mask = np.repeat(batch_mask, config.box_num_obj + 1) # TODO: dynamically sized graphs?
# reset_idx = np.where(batch_mask)
# self.hc[0][reset_idx] = 0 # h_n, idx-batch-sample
# self.hc[1][reset_idx] = 0 # c_n
# def detach_state(self):
# if self.hc is not None:
# self.hc = (self.hc[0].detach(), self.hc[1].detach())
# def clone_state(self, other):
# if other.hc is None:
# self.hc = None
# else:
# self.hc = (other.hc[0].clone(), other.hc[1].clone())
if __name__ == '__main__':
pos = torch.arange(4)
pos_enc = positional_encoding(pos, dim=16)