forked from IBM/Multi-GNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
236 lines (193 loc) · 9.71 KB
/
models.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
import torch.nn as nn
from torch_geometric.nn import GINEConv, BatchNorm, Linear, GATConv, PNAConv, RGCNConv
import torch.nn.functional as F
import torch
import logging
class GINe(torch.nn.Module):
def __init__(self, num_features, num_gnn_layers, n_classes=2,
n_hidden=100, edge_updates=False, residual=True,
edge_dim=None, dropout=0.0, final_dropout=0.5):
super().__init__()
self.n_hidden = n_hidden
self.num_gnn_layers = num_gnn_layers
self.edge_updates = edge_updates
self.final_dropout = final_dropout
self.node_emb = nn.Linear(num_features, n_hidden)
self.edge_emb = nn.Linear(edge_dim, n_hidden)
self.convs = nn.ModuleList()
self.emlps = nn.ModuleList()
self.batch_norms = nn.ModuleList()
for _ in range(self.num_gnn_layers):
conv = GINEConv(nn.Sequential(
nn.Linear(self.n_hidden, self.n_hidden),
nn.ReLU(),
nn.Linear(self.n_hidden, self.n_hidden)
), edge_dim=self.n_hidden)
if self.edge_updates: self.emlps.append(nn.Sequential(
nn.Linear(3 * self.n_hidden, self.n_hidden),
nn.ReLU(),
nn.Linear(self.n_hidden, self.n_hidden),
))
self.convs.append(conv)
self.batch_norms.append(BatchNorm(n_hidden))
self.mlp = nn.Sequential(Linear(n_hidden*3, 50), nn.ReLU(), nn.Dropout(self.final_dropout),Linear(50, 25), nn.ReLU(), nn.Dropout(self.final_dropout),
Linear(25, n_classes))
def forward(self, x, edge_index, edge_attr):
src, dst = edge_index
x = self.node_emb(x)
edge_attr = self.edge_emb(edge_attr)
for i in range(self.num_gnn_layers):
x = (x + F.relu(self.batch_norms[i](self.convs[i](x, edge_index, edge_attr)))) / 2
if self.edge_updates:
edge_attr = edge_attr + self.emlps[i](torch.cat([x[src], x[dst], edge_attr], dim=-1)) / 2
x = x[edge_index.T].reshape(-1, 2 * self.n_hidden).relu()
x = torch.cat((x, edge_attr.view(-1, edge_attr.shape[1])), 1)
out = x
return self.mlp(out)
class GATe(torch.nn.Module):
def __init__(self, num_features, num_gnn_layers, n_classes=2, n_hidden=100, n_heads=4, edge_updates=False, edge_dim=None, dropout=0.0, final_dropout=0.5):
super().__init__()
# GAT specific code
tmp_out = n_hidden // n_heads
n_hidden = tmp_out * n_heads
self.n_hidden = n_hidden
self.n_heads = n_heads
self.num_gnn_layers = num_gnn_layers
self.edge_updates = edge_updates
self.dropout = dropout
self.final_dropout = final_dropout
self.node_emb = nn.Linear(num_features, n_hidden)
self.edge_emb = nn.Linear(edge_dim, n_hidden)
self.convs = nn.ModuleList()
self.emlps = nn.ModuleList()
self.batch_norms = nn.ModuleList()
for _ in range(self.num_gnn_layers):
conv = GATConv(self.n_hidden, tmp_out, self.n_heads, concat = True, dropout = self.dropout, add_self_loops = True, edge_dim=self.n_hidden)
if self.edge_updates: self.emlps.append(nn.Sequential(nn.Linear(3 * self.n_hidden, self.n_hidden),nn.ReLU(),nn.Linear(self.n_hidden, self.n_hidden),))
self.convs.append(conv)
self.batch_norms.append(BatchNorm(n_hidden))
self.mlp = nn.Sequential(Linear(n_hidden*3, 50), nn.ReLU(), nn.Dropout(self.final_dropout),Linear(50, 25), nn.ReLU(), nn.Dropout(self.final_dropout),Linear(25, n_classes))
def forward(self, x, edge_index, edge_attr):
src, dst = edge_index
x = self.node_emb(x)
edge_attr = self.edge_emb(edge_attr)
for i in range(self.num_gnn_layers):
x = (x + F.relu(self.batch_norms[i](self.convs[i](x, edge_index, edge_attr)))) / 2
if self.edge_updates:
edge_attr = edge_attr + self.emlps[i](torch.cat([x[src], x[dst], edge_attr], dim=-1)) / 2
logging.debug(f"x.shape = {x.shape}, x[edge_index.T].shape = {x[edge_index.T].shape}")
x = x[edge_index.T].reshape(-1, 2 * self.n_hidden).relu()
logging.debug(f"x.shape = {x.shape}")
x = torch.cat((x, edge_attr.view(-1, edge_attr.shape[1])), 1)
logging.debug(f"x.shape = {x.shape}")
out = x
return self.mlp(out)
class PNA(torch.nn.Module):
def __init__(self, num_features, num_gnn_layers, n_classes=2,
n_hidden=100, edge_updates=True,
edge_dim=None, dropout=0.0, final_dropout=0.5, deg=None):
super().__init__()
n_hidden = int((n_hidden // 5) * 5)
self.n_hidden = n_hidden
self.num_gnn_layers = num_gnn_layers
self.edge_updates = edge_updates
self.final_dropout = final_dropout
aggregators = ['mean', 'min', 'max', 'std']
scalers = ['identity', 'amplification', 'attenuation']
self.node_emb = nn.Linear(num_features, n_hidden)
self.edge_emb = nn.Linear(edge_dim, n_hidden)
self.convs = nn.ModuleList()
self.emlps = nn.ModuleList()
self.batch_norms = nn.ModuleList()
for _ in range(self.num_gnn_layers):
conv = PNAConv(in_channels=n_hidden, out_channels=n_hidden,
aggregators=aggregators, scalers=scalers, deg=deg,
edge_dim=n_hidden, towers=5, pre_layers=1, post_layers=1,
divide_input=False)
if self.edge_updates: self.emlps.append(nn.Sequential(
nn.Linear(3 * self.n_hidden, self.n_hidden),
nn.ReLU(),
nn.Linear(self.n_hidden, self.n_hidden),
))
self.convs.append(conv)
self.batch_norms.append(BatchNorm(n_hidden))
self.mlp = nn.Sequential(Linear(n_hidden*3, 50), nn.ReLU(), nn.Dropout(self.final_dropout),Linear(50, 25), nn.ReLU(), nn.Dropout(self.final_dropout),
Linear(25, n_classes))
def forward(self, x, edge_index, edge_attr):
src, dst = edge_index
x = self.node_emb(x)
edge_attr = self.edge_emb(edge_attr)
for i in range(self.num_gnn_layers):
x = (x + F.relu(self.batch_norms[i](self.convs[i](x, edge_index, edge_attr)))) / 2
if self.edge_updates:
edge_attr = edge_attr + self.emlps[i](torch.cat([x[src], x[dst], edge_attr], dim=-1)) / 2
logging.debug(f"x.shape = {x.shape}, x[edge_index.T].shape = {x[edge_index.T].shape}")
x = x[edge_index.T].reshape(-1, 2 * self.n_hidden).relu()
logging.debug(f"x.shape = {x.shape}")
x = torch.cat((x, edge_attr.view(-1, edge_attr.shape[1])), 1)
logging.debug(f"x.shape = {x.shape}")
out = x
return self.mlp(out)
class RGCN(nn.Module):
def __init__(self, num_features, edge_dim, num_relations, num_gnn_layers, n_classes=2,
n_hidden=100, edge_update=False,
residual=True,
dropout=0.0, final_dropout=0.5, n_bases=-1):
super(RGCN, self).__init__()
self.num_features = num_features
self.num_gnn_layers = num_gnn_layers
self.n_hidden = n_hidden
self.residual = residual
self.dropout = dropout
self.final_dropout = final_dropout
self.n_classes = n_classes
self.edge_update = edge_update
self.num_relations = num_relations
self.n_bases = n_bases
self.node_emb = nn.Linear(num_features, n_hidden)
self.edge_emb = nn.Linear(edge_dim, n_hidden)
self.convs = nn.ModuleList()
self.bns = nn.ModuleList()
self.mlp = nn.ModuleList()
if self.edge_update:
self.emlps = nn.ModuleList()
self.emlps.append(nn.Sequential(
nn.Linear(3 * self.n_hidden, self.n_hidden),
nn.ReLU(),
nn.Linear(self.n_hidden, self.n_hidden),
))
for _ in range(self.num_gnn_layers):
conv = RGCNConv(self.n_hidden, self.n_hidden, num_relations, num_bases=self.n_bases)
self.convs.append(conv)
self.bns.append(nn.BatchNorm1d(self.n_hidden))
if self.edge_update:
self.emlps.append(nn.Sequential(
nn.Linear(3 * self.n_hidden, self.n_hidden),
nn.ReLU(),
nn.Linear(self.n_hidden, self.n_hidden),
))
self.mlp = nn.Sequential(Linear(n_hidden*3, 50), nn.ReLU(), nn.Dropout(self.final_dropout), Linear(50, 25), nn.ReLU(), nn.Dropout(self.final_dropout),
Linear(25, n_classes))
def reset_parameters(self):
for m in self.modules():
if isinstance(m, nn.Linear):
m.reset_parameters()
elif isinstance(m, RGCNConv):
m.reset_parameters()
elif isinstance(m, nn.BatchNorm1d):
m.reset_parameters()
def forward(self, x, edge_index, edge_attr):
edge_type = edge_attr[:, -1].long()
#edge_attr = edge_attr[:, :-1]
src, dst = edge_index
x = self.node_emb(x)
edge_attr = self.edge_emb(edge_attr)
for i in range(self.num_gnn_layers):
x = (x + F.relu(self.bns[i](self.convs[i](x, edge_index, edge_type)))) / 2
if self.edge_update:
edge_attr = (edge_attr + F.relu(self.emlps[i](torch.cat([x[src], x[dst], edge_attr], dim=-1)))) / 2
x = x[edge_index.T].reshape(-1, 2 * self.n_hidden).relu()
x = torch.cat((x, edge_attr.view(-1, edge_attr.shape[1])), 1)
x = self.mlp(x)
out = x
return x