-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_node.py
More file actions
384 lines (322 loc) · 15.9 KB
/
Copy pathmodel_node.py
File metadata and controls
384 lines (322 loc) · 15.9 KB
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
import torch
from tell import LogicalLayer, Phi
def gumbel_sigmoid(logits, tau = 1, hard = False, threshold = 0.5, deterministic=False):
"""
Samples from the Gumbel-Sigmoid distribution and optionally discretizes.
The discretization converts the values greater than `threshold` to 1 and the rest to 0.
The code is adapted from the official PyTorch implementation of gumbel_softmax:
https://pytorch.org/docs/stable/_modules/torch/nn/functional.html#gumbel_softmax
Args:
logits: `[..., num_features]` unnormalized log probabilities
tau: non-negative scalar temperature
hard: if ``True``, the returned samples will be discretized,
but will be differentiated as if it is the soft sample in autograd
threshold: threshold for the discretization,
values greater than this will be set to 1 and the rest to 0
Returns:
Sampled tensor of same shape as `logits` from the Gumbel-Sigmoid distribution.
If ``hard=True``, the returned samples are descretized according to `threshold`, otherwise they will
be probability distributions.
"""
gumbels = (
-torch.empty_like(logits, memory_format=torch.legacy_contiguous_format).exponential_().log()
) # ~Gumbel(0, 1)
if deterministic: gumbels=0
gumbels = (logits + gumbels) / tau # ~Gumbel(logits, tau)
y_soft = gumbels.sigmoid()
if hard:
# Straight through.
indices = (y_soft > threshold).nonzero(as_tuple=True)
y_hard = torch.zeros_like(logits, memory_format=torch.legacy_contiguous_format)
y_hard[indices[0], indices[1]] = 1.0
ret = y_hard - y_soft.detach() + y_soft
else:
# Reparametrization trick.
ret = y_soft
return ret
class GumbelSigmoidLayer(torch.nn.Module):
def __init__(self, tau=1.0, hard=False, threshold=0.5, deterministic=False):
super().__init__()
self.tau = tau
self.hard = hard
self.threshold = threshold
self.deterministic = deterministic
def forward(self, logits):
return gumbel_sigmoid(
logits,
tau=self.tau,
hard=self.hard,
threshold=self.threshold,
deterministic=self.deterministic
)
def set(self, tau, hard, deterministic):
"""Permette di aggiornare la temperatura a runtime"""
self.tau = tau
self.hard = hard
self.deterministic = deterministic
from torch_geometric.nn import MessagePassing
class CustomGraphConv(MessagePassing):
def __init__(self, nn_0, nn_1, negative_concatenate=False, **kwargs):
super().__init__(aggr='add', **kwargs)
self.nn_0 = nn_0
self.nn_1 = nn_1
self.messages = None
self.negative_concatenate = negative_concatenate
def forward(self, x, edge_index, edge_attr):
out = self.propagate(edge_index, x=x, edge_attr=edge_attr, size=None)
out = self.nn_1(out)
#print("out shape after nn_1:", out.shape)
return out, self.messages
def message(self, x_i, x_j, edge_attr):
#print("x_i shape:", x_i.shape, "x_j shape:", x_j.shape, "edge_attr shape:", edge_attr.shape if edge_attr is not None else None)
if edge_attr is None:
out = torch.cat([x_i, x_j], dim=1)
else:
out = torch.cat([x_i, x_j, edge_attr], dim=1)
#print("out shape before nn_0:", out.shape)
out = self.nn_0(out)
#print("out shape after nn_0:", out.shape)
self.messages = out
if self.nn_0.__class__.__name__ == "LogicalLayer" and self.negative_concatenate == 2:
out = torch.cat([out, 1-out], dim=1)
return out
def update(self, aggr_out):
return aggr_out
class GIN(torch.nn.Module):
def __init__(self, num_features, num_features_edge, num_classes, num_layers=3, hidden_dim=64, dropout=0.15, nogumbel=False, edge_once = False, layer_double = False):
super(GIN, self).__init__()
self.num_features, self.num_features_edge, self.num_classes = num_features, num_features_edge, num_classes
self.convs = torch.nn.ModuleList()
self.edge_once = edge_once
self.layer_double = layer_double
for i in range(num_layers):
in_dim = None
if i == 0:
in_dim = 2 * num_features + num_features_edge
elif edge_once:
in_dim = 2 * hidden_dim
else:
in_dim = 2 * hidden_dim + num_features_edge
if layer_double == False:
conv = CustomGraphConv(
nn_0 = torch.nn.Sequential(
torch.nn.Linear(in_dim, hidden_dim),
GumbelSigmoidLayer() if not nogumbel else torch.nn.ReLU()
),
nn_1 = torch.nn.Sequential(
torch.nn.Linear(hidden_dim, hidden_dim),
GumbelSigmoidLayer() if not nogumbel else torch.nn.ReLU()
)
)
else:
conv = CustomGraphConv(
nn_0 = torch.nn.Sequential(
torch.nn.Linear(in_dim, hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, hidden_dim),
GumbelSigmoidLayer() if not nogumbel else torch.nn.ReLU()
),
nn_1 = torch.nn.Sequential(
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, hidden_dim),
GumbelSigmoidLayer() if not nogumbel else torch.nn.ReLU()
)
)
self.convs.append(conv)
self.fc1 = torch.nn.Linear(num_layers*hidden_dim, hidden_dim)
self.fc2 = torch.nn.Linear(hidden_dim, num_classes)
self.dropout = torch.nn.Dropout(dropout)
self.nogumbel=nogumbel
def forward(self, x, edge_index, edge_attr, tau=1, deterministic=False):
xs = []
for i, conv in enumerate(self.convs):
if self.layer_double == False and not self.nogumbel:
conv.nn_0[1].set(tau, hard=True, deterministic=deterministic)
conv.nn_1[1].set(tau, hard=True, deterministic=deterministic)
elif self.layer_double == True and not self.nogumbel:
conv.nn_0[3].set(tau, hard=True, deterministic=deterministic)
conv.nn_1[3].set(tau, hard=True, deterministic=deterministic)
if i == 0 and edge_attr is not None:
x, _ = conv(x, edge_index, edge_attr)
elif i!=0 and edge_attr is not None and self.edge_once == False:
x, _ = conv(x, edge_index, edge_attr)
else:
x, _ = conv(x, edge_index, None)
xs.append(x)
x = self.dropout(x)
x = torch.hstack(xs)
x = self.fc1(x)
x = torch.nn.functional.relu(x)
x = torch.sigmoid(self.fc2(x))
return x
def forward_e(self, x, edge_index, edge_attr, tau=1, deterministic=False):
ret_x = []
ret_y_interm = []
ret_y = []
xs = []
for i, conv in enumerate(self.convs):
if self.layer_double == False and not self.nogumbel:
conv.nn_0[1].set(tau, hard=True, deterministic=deterministic)
conv.nn_1[1].set(tau, hard=True, deterministic=deterministic)
elif self.layer_double == True and not self.nogumbel:
conv.nn_0[3].set(tau, hard=True, deterministic=deterministic)
conv.nn_1[3].set(tau, hard=True, deterministic=deterministic)
ret_x.append(x)
if i == 0 and edge_attr is not None:
x, x_interm = conv(x, edge_index, edge_attr)
elif i!=0 and edge_attr is not None and self.edge_once == False:
x, x_interm = conv(x, edge_index, edge_attr)
else:
x, x_interm = conv(x, edge_index, None)
xs.append(x)
ret_y.append(x)
ret_y_interm.append(x_interm)
x = torch.hstack(xs)
ret_x.append(x)
x = self.fc1(x)
x = torch.nn.functional.relu(x)
x = torch.sigmoid(self.fc2(x))
ret_y.append(x)
return ret_x, ret_y, ret_y_interm
class GINTELL(torch.nn.Module):
def __init__(self, num_features, num_features_edge, num_classes, num_layers=3, hidden_dim=64, edge_again=False, negative_concatenate=False, input_binary=True, edge_binary=True ):
super(GINTELL, self).__init__()
self.num_features, self.num_features_edge, self.num_classes = num_features, num_features_edge, num_classes
self.convs = torch.nn.ModuleList()
self.edge_again = edge_again
self.negative_concatenate = negative_concatenate
print("edge again:", edge_again, "negative concatenate:", negative_concatenate)
if not edge_again and negative_concatenate==2:
for i in range(num_layers):
conv = CustomGraphConv(
nn_0 = LogicalLayer(2*2*num_features + 2*num_features_edge if i==0 else 2*2*hidden_dim, hidden_dim, use_phi=False),
nn_1 = LogicalLayer(2*hidden_dim, hidden_dim, use_phi=True),
negative_concatenate=negative_concatenate
)
self.convs.append(conv)
self.fc = LogicalLayer(2*num_layers*hidden_dim, num_classes, use_phi=False)
elif edge_again and negative_concatenate==0:
for i in range(num_layers):
conv = CustomGraphConv(
nn_0 = LogicalLayer(2*num_features + num_features_edge if i==0 else 2*hidden_dim + num_features_edge, hidden_dim, use_phi=False),
nn_1 = LogicalLayer(hidden_dim, hidden_dim, use_phi=True),
negative_concatenate=negative_concatenate
)
self.convs.append(conv)
self.fc = LogicalLayer(num_layers*hidden_dim, num_classes, use_phi=False)
elif edge_again and negative_concatenate==2:
for i in range(num_layers):
conv = CustomGraphConv(
nn_0 = LogicalLayer(2*2*num_features + 2*num_features_edge if i==0 else 2*2*hidden_dim + 2*num_features_edge, hidden_dim, use_phi=False),
nn_1 = LogicalLayer(2*hidden_dim, hidden_dim, use_phi=True),
negative_concatenate=negative_concatenate
)
self.convs.append(conv)
self.fc = LogicalLayer(2*num_layers*hidden_dim, num_classes, use_phi=False)
elif not edge_again and negative_concatenate==0:
for i in range(num_layers):
conv = CustomGraphConv(
nn_0 = LogicalLayer(2*num_features + num_features_edge if i==0 else 2*hidden_dim, hidden_dim, use_phi=False),
nn_1 = LogicalLayer(hidden_dim, hidden_dim, use_phi=True),
negative_concatenate=negative_concatenate
)
self.convs.append(conv)
self.fc = LogicalLayer(num_layers*hidden_dim, num_classes, use_phi=False)
elif edge_again and negative_concatenate==1:
for i in range(num_layers):
conv = CustomGraphConv(
nn_0 = LogicalLayer(2*2*num_features + 2*num_features_edge if i==0 else 2*hidden_dim + 2*num_features_edge, hidden_dim, use_phi=False),
nn_1 = LogicalLayer(hidden_dim, hidden_dim, use_phi=True),
negative_concatenate=negative_concatenate
)
self.convs.append(conv)
self.fc = LogicalLayer(num_layers*hidden_dim, num_classes, use_phi=False)
else:
for i in range(num_layers):
conv = CustomGraphConv(
nn_0 = LogicalLayer(2*2*num_features + 2*num_features_edge if i==0 else 2*hidden_dim, hidden_dim, use_phi=False),
nn_1 = LogicalLayer(hidden_dim, hidden_dim, use_phi=True),
negative_concatenate=negative_concatenate
)
self.convs.append(conv)
self.fc = LogicalLayer(num_layers*hidden_dim, num_classes, use_phi=False)
self.input_binary = input_binary
self.edge_binary = edge_binary
print("input_binary", input_binary, "edge_binary", edge_binary)
self.phi_node = Phi(num_features) if not input_binary else None
self.phi_edge = Phi(num_features_edge) if not edge_binary else None
def forward(self, x, edge_index, edge_attr, discrete=False, *args, **kwargs):
xs = []
if edge_attr is not None and self.phi_edge is not None:
edge_attr = self.phi_edge(edge_attr)
if edge_attr is not None and self.negative_concatenate!=0:
edge_attr = torch.hstack([edge_attr, 1-edge_attr])
for i, conv in enumerate(self.convs):
if i == 0 and not self.input_binary:
x = self.phi_node(x)
if self.negative_concatenate == 2 or (self.negative_concatenate==1 and i==0):
x = torch.hstack([x, 1-x])
if i == 0 and edge_attr is not None:
x, _ = conv(x, edge_index, edge_attr)
elif i!=0 and edge_attr is not None and self.edge_again:
x, _ = conv(x, edge_index, edge_attr)
else:
x, _ = conv(x, edge_index, None)
if discrete:
indices = (x > 0.5).nonzero(as_tuple=True)
x_hard = torch.zeros_like(x, memory_format=torch.legacy_contiguous_format)
x_hard[indices[0], indices[1]] = 1.0
x = x_hard - x.detach() + x
xs.append(x)
x = torch.hstack(xs)
if discrete:
indices = (x > 0.5).nonzero(as_tuple=True)
x_hard = torch.zeros_like(x, memory_format=torch.legacy_contiguous_format)
x_hard[indices[0], indices[1]] = 1.0
x = x_hard - x.detach() + x
if self.negative_concatenate==2:
x = torch.hstack([x, 1-x])
x = self.fc(x)
return x
def forward_e(self, x, edge_index, edge_attr=None, discrete=False, *args, **kwargs):
ret_x = []
ret_y = []
ret_y_interm = []
xs = []
if edge_attr is not None and self.phi_edge is not None:
edge_attr = self.phi_edge(edge_attr)
if edge_attr is not None and self.negative_concatenate!=0:
edge_attr = torch.hstack([edge_attr, 1-edge_attr])
for i, conv in enumerate(self.convs):
ret_x.append(x)
if i == 0 and not self.input_binary:
x = self.phi_node(x)
if self.negative_concatenate == 2 or (self.negative_concatenate==1 and i==0):
x = torch.hstack([x, 1-x])
if i == 0 and edge_attr is not None:
x, x_interm = conv(x, edge_index, edge_attr)
elif i!=0 and edge_attr is not None and self.edge_again:
x, x_interm = conv(x, edge_index, edge_attr)
else:
x, x_interm = conv(x, edge_index, None)
if discrete:
indices = (x > 0.5).nonzero(as_tuple=True)
x_hard = torch.zeros_like(x, memory_format=torch.legacy_contiguous_format)
x_hard[indices[0], indices[1]] = 1.0
x = x_hard - x.detach() + x
xs.append(x)
ret_y.append(x)
ret_y_interm.append(x_interm)
x = torch.hstack(xs)
if discrete:
indices = (x > 0.5).nonzero(as_tuple=True)
x_hard = torch.zeros_like(x, memory_format=torch.legacy_contiguous_format)
x_hard[indices[0], indices[1]] = 1.0
x = x_hard - x.detach() + x
if self.negative_concatenate==2:
x = torch.hstack([x, 1-x])
ret_x.append(x)
x = self.fc(x)
ret_y.append(x)
return ret_x, ret_y, ret_y_interm