-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph_refiner.py
74 lines (53 loc) · 2.07 KB
/
graph_refiner.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from hypergraph_refiner import DeepSet
class MLPAdjacency(nn.Module):
def __init__(self, dim):
super().__init__()
self.proj_x = nn.Linear(dim, dim)
self.proj_i = nn.Linear(1, dim)
self.proj_s = nn.Linear(dim, 1)
def forward(self, x, inc):
x = self.proj_x(x)
inc = self.proj_i(inc.unsqueeze(3))
s = F.relu(x.unsqueeze(1) + x.unsqueeze(2) + inc)
return torch.sigmoid(self.proj_s(s))
class GraphRefiner(nn.Module):
def __init__(self, dim):
super().__init__()
self.mlp_n = DeepSet(3*dim, [dim, dim])
self.norm_pre_n = nn.LayerNorm(3*dim)
self.norm_n = nn.LayerNorm(dim)
self.mlp_incidence = MLPAdjacency(dim)
def forward(self, inputs, n_t, i_t):
i_t = self.mlp_incidence(n_t, i_t).squeeze(3)
updates_n = torch.einsum("ben,bed->bnd", i_t, n_t)
n_t = self.norm_n(n_t + self.mlp_n(self.norm_pre_n(torch.cat([inputs, n_t, updates_n], dim=-1))))
pred = i_t
return pred, n_t, i_t
class IterativeRefiner(nn.Module):
def __init__(self, d_in, d_hid, T):
super().__init__()
self.T = T
self.d_in = d_in
self.d_hid = d_hid
self.proj_inputs = nn.Linear(d_in, d_hid)
self.refiner = GraphRefiner(d_hid)
def get_initial(self, inputs):
b, n_v, _, device = *inputs.shape, inputs.device
v_t = self.proj_inputs(inputs)
i_t = torch.zeros(b, n_v, n_v, device=device)
return v_t, i_t
def forward(self, inputs, v_t, i_t, t_skip=None, t_bp=None):
t_skip = 0 if t_skip is None else t_skip
t_bp = self.T if t_bp is None else t_bp
inputs = self.proj_inputs(inputs)
pred_bp = []
with torch.no_grad():
for _ in range(t_skip):
p, v_t, i_t = self.refiner(inputs, v_t, i_t)
for _ in range(t_skip, t_skip+t_bp):
p, v_t, i_t = self.refiner(inputs, v_t, i_t)
pred_bp.append(p)
return pred_bp, v_t, i_t