-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorch_classes.py
304 lines (250 loc) · 12.3 KB
/
torch_classes.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
from torch_geometric.data import Data
import torch
import torch.nn as nn
from torch.nn import Linear, LeakyReLU , Sequential
import torch.nn.functional as F
from torch_geometric.nn import GATConv, MLP, GCNConv, NNConv, MessagePassing, BatchNorm
from torch_scatter import scatter_mean
class GraphData(Data):
def __init__(self, edge_index=None, x=None, edge_attr= None,y=None):
super().__init__()
self.edge_index = edge_index
self.edge_attr = edge_attr
self.x = x
self.y = y
def __inc__(self, key, value, *args, **kwargs):
if key == 'edge_index':
return self.x.size(0)
else:
return super().__inc__(key, value, *args, **kwargs)
class GraphPair(Data):
def __init__(self, edge_index_s=None, x_s=None, edge_attr_s= None,
edge_index_t=None, x_t=None, edge_attr_t= None,y=None):
super().__init__()
self.edge_index_s = edge_index_s
self.edge_attr_s = edge_attr_s
self.x_s = x_s
self.edge_index_t = edge_index_t
self.edge_attr_t = edge_attr_t
self.x_t = x_t
self.y = y
def __inc__(self, key, value, *args, **kwargs):
if key == 'edge_index_s':
return self.x_s.size(0)
if key == 'edge_index_t':
return self.x_t.size(0)
else:
return super().__inc__(key, value, *args, **kwargs)
class GraphAggregator(torch.nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels):
super(GraphAggregator, self).__init__()
self.in_channels = in_channels
self.hidden_channels = hidden_channels
self.out_channels = out_channels
self._build_model()
def _build_model(self):
layer = []
layer.append(nn.Linear(self.in_channels, self.out_channels))
layer.append(nn.PReLU())
self.mlp = nn.Sequential(*layer)
layer = []
layer.append(nn.Linear(self.in_channels, self.out_channels))
self.mlp_gate = nn.Sequential(*layer)
layer = []
layer.append(nn.Linear(self.out_channels, self.hidden_channels))
layer.append(nn.LeakyReLU())
layer.append(nn.Linear(self.hidden_channels, self.out_channels))
self.mlp_final = nn.Sequential(*layer)
def forward(self, x):
x_states = self.mlp(x)
x_gates = F.softmax(self.mlp_gate(x), dim=1)
x_states = x_states * x_gates
x_states = self.mlp_final(x_states)
return x_states
class GCNGAT(torch.nn.Module):
def __init__(self):
super(GCNGAT, self).__init__()
self.gcnconv = GCNConv(in_channels=3, out_channels=64)
self.gatconv = GATConv(in_channels=64, out_channels=64, edge_dim=1)
self.aggregator = GraphAggregator(in_channels=64, hidden_channels=48, out_channels=32)
self.classification = MLP(in_channels=64, hidden_channels=32, out_channels=2, num_layers=3)
def compute_embedding(self, x, edge_index, x_batch, edge_attr):
x = self.gcnconv(x, edge_index)
x = self.gatconv(x, edge_index, edge_attr)
x = self.aggregator(x)
x = scatter_mean(x, x_batch, dim=0)
return x
def forward(self, data):
x_s, x_t = data.x_s, data.x_t
edge_index_s, edge_index_t = data.edge_index_s, data.edge_index_t
edge_attr_s, edge_attr_t = data.edge_attr_s, data.edge_attr_t
x_s_batch, x_t_batch = data.x_s_batch, data.x_t_batch
embed_s = self.compute_embedding(x_s, edge_index_s, x_s_batch, edge_attr_s)
embed_t = self.compute_embedding(x_t, edge_index_t, x_t_batch, edge_attr_t)
out = torch.cat((embed_s, embed_t), 1)
out = out.reshape(out.size(0), -1)
out = self.classification(out)
return out
class GCNGCN(torch.nn.Module):
def __init__(self):
super(GCNGCN, self).__init__()
self.gcnconv = GCNConv(in_channels=3, out_channels=64)
self.gcnconv1 = GCNConv(in_channels=64, out_channels=64)
self.aggregator = GraphAggregator(in_channels=64, hidden_channels=48, out_channels=32)
self.classification = MLP(in_channels=64, hidden_channels=32, out_channels=2, num_layers=3)
def compute_embedding(self, x, edge_index, x_batch, edge_attr):
x = self.gcnconv(x, edge_index)
x = self.gcnconv1(x, edge_index)
x = self.aggregator(x)
x = scatter_mean(x, x_batch, dim=0)
return x
def forward(self, data):
x_s, x_t = data.x_s, data.x_t
edge_index_s, edge_index_t = data.edge_index_s, data.edge_index_t
edge_attr_s, edge_attr_t = data.edge_attr_s, data.edge_attr_t
x_s_batch, x_t_batch = data.x_s_batch, data.x_t_batch
embed_s = self.compute_embedding(x_s, edge_index_s, x_s_batch, edge_attr_s)
embed_t = self.compute_embedding(x_t, edge_index_t, x_t_batch, edge_attr_t)
out = torch.cat((embed_s, embed_t), 1)
out = out.reshape(out.size(0), -1)
out = self.classification(out)
return out
class NNConvGCN(torch.nn.Module):
def __init__(self):
super(NNConvGCN, self).__init__()
self.nnconv1 = NNConv(in_channels=3, out_channels=64,
nn=Sequential(Linear(1, 32), LeakyReLU(0.01), Linear(32, 3*64)), aggr='mean')
self.gcnconv1 = GCNConv(in_channels=64, out_channels=64)
self.aggregator = GraphAggregator(in_channels=64, hidden_channels=48, out_channels=32)
self.classification = MLP(in_channels=64, hidden_channels=32, out_channels=2, num_layers=3)
def compute_embedding(self, x, edge_index, x_batch, edge_attr):
x = self.nnconv1(x, edge_index, edge_attr)
x = self.gcnconv1(x, edge_index)
x = self.aggregator(x)
x = scatter_mean(x, x_batch, dim=0)
return x
def forward(self, data):
x_s, x_t = data.x_s, data.x_t
edge_index_s, edge_index_t = data.edge_index_s, data.edge_index_t
edge_attr_s, edge_attr_t = data.edge_attr_s, data.edge_attr_t
x_s_batch, x_t_batch = data.x_s_batch, data.x_t_batch
embed_s = self.compute_embedding(x_s, edge_index_s, x_s_batch, edge_attr_s)
embed_t = self.compute_embedding(x_t, edge_index_t, x_t_batch, edge_attr_t)
out = torch.cat((embed_s, embed_t), 1)
out = out.reshape(out.size(0), -1)
out = self.classification(out)
return out
class NNConvNNConv(torch.nn.Module):
def __init__(self):
super(NNConvNNConv, self).__init__()
self.nnconv1 = NNConv(in_channels=3, out_channels=64,
nn=Sequential(Linear(1, 32), LeakyReLU(0.01), Linear(32, 3*64)), aggr='mean')
self.nnconv2 = NNConv(in_channels=64, out_channels=3,
nn=Sequential(Linear(1, 32), LeakyReLU(0.01), Linear(32, 64*3)), aggr='mean')
self.aggregator = GraphAggregator(in_channels= 3, hidden_channels=48, out_channels=32)
self.classification = MLP(in_channels=64, hidden_channels=32, out_channels=2, num_layers=3)
def compute_embedding(self, x, edge_index, x_batch, edge_attr):
x = self.nnconv1(x, edge_index, edge_attr)
x = self.nnconv2(x, edge_index,edge_attr)
x = self.aggregator(x)
x = scatter_mean(x, x_batch, dim=0)
return x
def forward(self, data):
x_s, x_t = data.x_s, data.x_t
edge_index_s, edge_index_t = data.edge_index_s, data.edge_index_t
edge_attr_s, edge_attr_t = data.edge_attr_s, data.edge_attr_t
x_s_batch, x_t_batch = data.x_s_batch, data.x_t_batch
embed_s = self.compute_embedding(x_s, edge_index_s, x_s_batch, edge_attr_s)
embed_t = self.compute_embedding(x_t, edge_index_t, x_t_batch, edge_attr_t)
out = torch.cat((embed_s, embed_t), 1)
out = out.reshape(out.size(0), -1)
out = self.classification(out)
return out
class NNConvGAT(torch.nn.Module):
def __init__(self):
super(NNConvGAT, self).__init__()
self.nnconv = NNConv(in_channels=3, out_channels=64,
nn=Sequential(Linear(1, 32), LeakyReLU(0.01), Linear(32, 3 * 64)), aggr='mean')
self.gatconv = GATConv(in_channels=64, out_channels=64, edge_dim=1)
self.aggregator = GraphAggregator(in_channels=64, hidden_channels=48, out_channels=32)
self.classification = MLP(in_channels=64, hidden_channels=32, out_channels=2, num_layers=3)
def compute_embedding(self, x, edge_index, x_batch, edge_attr):
x = self.nnconv(x, edge_index, edge_attr)
x = self.gatconv(x, edge_index, edge_attr)
x = self.aggregator(x)
x = scatter_mean(x, x_batch, dim=0)
return x
def forward(self, data):
x_s, x_t = data.x_s, data.x_t
edge_index_s, edge_index_t = data.edge_index_s, data.edge_index_t
edge_attr_s, edge_attr_t = data.edge_attr_s, data.edge_attr_t
x_s_batch, x_t_batch = data.x_s_batch, data.x_t_batch
embed_s = self.compute_embedding(x_s, edge_index_s, x_s_batch, edge_attr_s)
embed_t = self.compute_embedding(x_t, edge_index_t, x_t_batch, edge_attr_t)
out = torch.cat((embed_s, embed_t), 1)
out = out.reshape(out.size(0), -1)
out = self.classification(out)
return out
class GEmbedLayer(MessagePassing):
def __init__(self, in_channels, edge_channels, hidden_channels, out_channels):
super().__init__(aggr='add') # "Add" aggregation.
self.in_chs = in_channels
self.ed_chs = edge_channels
self.hid_chs = hidden_channels
self.out_chs = out_channels
self._build_model()
self.batch_norm = BatchNorm(self.out_chs)
def _build_model(self):
layer = []
layer.append(nn.Linear(self.in_chs, self.out_chs))
layer.append(nn.LeakyReLU())
self.mlp_node = nn.Sequential(*layer)
layer = []
layer.append(nn.Linear(self.ed_chs, self.out_chs))
layer.append(nn.LeakyReLU())
self.mlp_edges = nn.Sequential(*layer)
layer = []
layer.append(nn.Linear(3 * self.out_chs, self.hid_chs))
layer.append(nn.LeakyReLU())
layer.append(nn.Linear(self.hid_chs, self.out_chs))
self.mlp_msg = nn.Sequential(*layer)
layer = []
layer.append(nn.Linear(self.in_chs + self.out_chs, self.out_chs))
layer.append(nn.LeakyReLU())
self.mlp_upd = nn.Sequential(*layer)
def forward(self, x, edge_index, edge_attr):
x_encoded = self.mlp_node(x)
edge_encoded = self.mlp_edges(edge_attr)
return self.propagate(edge_index, x=x_encoded, edge_attr=edge_encoded, x_original=x, edge_original=edge_attr)
def message(self, x_i, x_j, edge_attr):
tmp = torch.cat([x_i, x_j,edge_attr], dim=1)
return self.mlp_msg(tmp)
def update(self, aggr_out, x, edge_attr, x_original, edge_original):
temp = torch.cat((x_original, aggr_out), dim=1)
aggr_out = self.mlp_upd(temp)
aggr_out = self.batch_norm(aggr_out)
return aggr_out
class GENGAT(torch.nn.Module):
def __init__(self):
super(GENGAT, self).__init__()
self.gembed = GEmbedLayer(in_channels=3, edge_channels=1, hidden_channels=32, out_channels=64)
self.gatconv = GATConv(in_channels=64, out_channels=64, edge_dim=1)
self.aggregator = GraphAggregator(in_channels=64, hidden_channels=48, out_channels=32)
self.classification = MLP(in_channels=64, hidden_channels=32, out_channels=2, num_layers=3)
def compute_embedding(self, x, edge_index, x_batch, edge_attr):
x = self.gembed(x, edge_index, edge_attr)
x = self.gatconv(x, edge_index, edge_attr)
x = self.aggregator(x)
x = scatter_mean(x, x_batch, dim=0)
return x
def forward(self, data):
x_s, x_t = data.x_s, data.x_t
edge_index_s, edge_index_t = data.edge_index_s, data.edge_index_t
edge_attr_s, edge_attr_t = data.edge_attr_s, data.edge_attr_t
x_s_batch, x_t_batch = data.x_s_batch, data.x_t_batch
embed_s = self.compute_embedding(x_s, edge_index_s, x_s_batch, edge_attr_s)
embed_t = self.compute_embedding(x_t, edge_index_t, x_t_batch, edge_attr_t)
out = torch.cat((embed_s, embed_t), 1)
out = out.reshape(out.size(0), -1)
out = self.classification(out)
return out