-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.py
215 lines (188 loc) · 7.67 KB
/
layer.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
import torch
import torch.nn as nn
import torch.nn.functional as fn
class MPNNlayer(nn.Module):
"""
Message Passing Neural Network variant (Gilmer et al., ICML 2017)
"""
def __init__(self, device, enc_dim, edge_dim, latent_dim, bias=True, gru=False, actv=None):
super(MPNNlayer, self).__init__()
self.dev = device
self.zdim = enc_dim
self.hdim = latent_dim
self.edim = edge_dim
self.bias = bias
if actv is None:
self.message = nn.Sequential(
nn.Linear(2*enc_dim+edge_dim, latent_dim, bias=bias),
)
else:
self.message = nn.Sequential(
nn.Linear(2*enc_dim+edge_dim, latent_dim, bias=bias),
actv
)
# todo upgrade to pytorch 1.7 to use amax!!
self.aggregate = torch.max
if gru:
self.gru = nn.GRUCell(latent_dim, latent_dim, bias=bias)
else:
self.gru = None
if actv is None:
self.update = nn.Sequential(
nn.Linear(enc_dim+latent_dim, latent_dim, bias=bias),
)
else:
self.update = nn.Sequential(
nn.Linear(enc_dim+latent_dim, latent_dim, bias=bias),
actv
)
self.reset_parameters()
self.to(device)
def reset_parameters(self):
# TF Keras initializer
nn.init.xavier_uniform_(self.message[0].weight.data)
if self.bias:
nn.init.zeros_(self.message[0].bias.data)
nn.init.xavier_uniform_(self.update[0].weight.data)
if self.bias:
nn.init.zeros_(self.update[0].bias.data)
# TF Lattice initializer
# nn.init.uniform_(self.message[0].weight.data,-0.05,0.05)
# nn.init.uniform_(self.message[0].bias.data,-0.05,0.05)
# nn.init.uniform_(self.update[0].weight.data,-0.05,0.05)
# nn.init.uniform_(self.update[0].bias.data,-0.05,0.05)
def forward(self, z, e_feat, adj, h=None):
"""
z: BATCH x NUM_NODES x NUM_NODE_FEATURES (B x N x M)
e_feat: BATCH x NUM_NODES x NUM_NODES x NUM_EDGE_FEATURES (B x N x N x E)
adj: BATCH x NUM_NODES x NUM_NODES (B x N x N)
"""
z_i = z.unsqueeze(2).expand(-1,-1,adj.shape[1],-1)
z_j = z.unsqueeze(1).expand(-1,adj.shape[1],-1,-1)
# (z_ij: B x N x N x (2M+E))
if self.edim == 0:
z_ij = torch.cat([z_i, z_j], dim=-1)
else:
z_ij = torch.cat([z_i, z_j, e_feat], dim=-1)
shape = z_ij.shape
# temp. change to ( BNN x M ) to pass it through the message neural network
z_ij = z_ij.view(-1, shape[-1])
msgs = self.message(z_ij)
# mask value and operator depends on aggregator for max its float('-inf') with addition -> todo for other aggregators
adj_mask = adj.float().masked_fill((adj==0).bool(), float('-inf')).unsqueeze(-1)
adj_mask = adj_mask.masked_fill((adj==1).bool().unsqueeze(-1), 0.0)
# changing it back for aggregation (msgs: B x N x N x M)
msgs = msgs.view(*(shape[:-1]),self.hdim)
# (agg_msgs: B x N x M)
agg_msgs = self.aggregate(msgs+adj_mask, dim=2)[0]
# (out_z: B x N x (2M))
out_h = torch.cat([z, agg_msgs], dim=-1)
# compactifying the new representation to the original size
out_h = out_h.view(-1, self.zdim+self.hdim)
out_h = self.update(out_h)
if self.gru is not None:
out_h = self.gru(out_h, h.view(-1, self.hdim))
# final dimensions (out_z: B x N x M)
out_h = out_h.view(shape[0], shape[1], self.hdim)
return out_h
class MultiMPNN(nn.Module):
"""
Message Passing Neural Network variant (Gilmer et al., ICML 2017)
"""
def __init__(
self,
device,
enc_dim,
edge_dim,
latent_dim,
bias=True,
gru=False,
actv=None
):
super(MultiMPNN, self).__init__()
self.dev = device
self.zdim = enc_dim
self.hdim = latent_dim
self.edim = edge_dim
self.bias = bias
if actv is None:
self.message = nn.Sequential(
nn.Linear(2*enc_dim+edge_dim, latent_dim, bias=bias),
)
else:
self.message = nn.Sequential(
nn.Linear(2*enc_dim+edge_dim, latent_dim, bias=bias),
actv
)
self.aggregate = torch.max
if gru:
self.gru = nn.GRUCell(latent_dim, latent_dim, bias=bias)
else:
self.gru = None
if actv is None:
self.update = nn.Sequential(
nn.Linear(enc_dim+latent_dim, latent_dim, bias=bias),
)
else:
self.update = nn.Sequential(
nn.Linear(enc_dim+latent_dim, latent_dim, bias=bias),
actv
)
self.reset_parameters()
self.to(device)
def reset_parameters(self):
# TF Keras initializer
nn.init.xavier_uniform_(self.message[0].weight.data)
if self.bias:
nn.init.zeros_(self.message[0].bias.data)
nn.init.xavier_uniform_(self.update[0].weight.data)
if self.bias:
nn.init.zeros_(self.update[0].bias.data)
# # Identity initializer
# nn.init.eye_(self.message[0].weight.data)
# if self.bias:
# nn.init.zeros_(self.message[0].bias.data)
# nn.init.eye_(self.update[0].weight.data)
if self.bias:
nn.init.zeros_(self.update[0].bias.data)
# TF Lattice initializer
# nn.init.uniform_(self.message[0].weight.data,-0.05,0.05)
# nn.init.uniform_(self.message[0].bias.data,-0.05,0.05)
# nn.init.uniform_(self.update[0].weight.data,-0.05,0.05)
# nn.init.uniform_(self.update[0].bias.data,-0.05,0.05)
def forward(self, zs, e_feat, adj, encoders, h=None):
"""
z: BATCH x NUM_NODES x NUM_NODE_FEATURES (B x N x M)
e_feat: BATCH x NUM_NODES x NUM_NODES x NUM_EDGE_FEATURES (B x N x N x E)
adj: BATCH x NUM_NODES x NUM_NODES (B x N x N)
"""
out = []
for z, enc in zip(zs, encoders):
z_i = z.unsqueeze(2).expand(-1,-1,adj.shape[1],-1)
z_j = z.unsqueeze(1).expand(-1,adj.shape[1],-1,-1)
# (z_ij: B x N x N x (2M+E))
z_ij = torch.cat([z_i, z_j, e_feat], dim=-1)
shape = z_ij.shape
# temp. change to ( BNN x M ) to pass it through the message neural network
z_ij = z_ij.view(-1, shape[-1])
if enc is not None:
z_ij = enc(z_ij)
msgs = self.message(z_ij)
# mask value and operator depends on aggregator for max its float('-inf') with addition -> todo for other aggregators
adj_mask = adj.float().masked_fill((adj==0).bool(), float('-inf')).unsqueeze(-1)
adj_mask = adj_mask.masked_fill((adj==1).bool().unsqueeze(-1), 0.0)
# changing it back for aggregation (msgs: B x N x N x M)
msgs = msgs.view(*(shape[:-1]),self.hdim)
# (agg_msgs: B x N x M)
agg_msgs = self.aggregate(msgs+adj_mask, dim=2)[0]
# (out_z: B x N x (2M))
out_h = torch.cat([z[:,:,:self.zdim], agg_msgs], dim=-1)
# compactifying the new representation to the original size
out_h = out_h.view(-1, self.zdim+self.hdim)
out_h = self.update(out_h)
if self.gru is not None:
out_h = self.gru(out_h, h.view(-1, self.hdim))
# final dimensions (out_z: B x N x M)
out_h = out_h.view(shape[0], shape[1], self.hdim)
out.append(out_h)
return out