-
Notifications
You must be signed in to change notification settings - Fork 30
/
attention.py
524 lines (457 loc) · 26.1 KB
/
attention.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# import keras_core as ks
from kgcnn.layers.gather import GatherNodesIngoing, GatherNodesOutgoing
from keras.layers import Dense, Concatenate, Activation, Average, Layer
from kgcnn.layers.aggr import AggregateLocalEdgesAttention
from keras import ops
import kgcnn.ops.activ
class AttentionHeadGAT(Layer): # noqa
r"""Computes the attention head according to `GAT <https://arxiv.org/abs/1710.10903>`__ .
The attention coefficients are computed by :math:`a_{ij} = \sigma(a^T W n_i || W n_j)`,
optionally by :math:`a_{ij} = \sigma( W n_i || W n_j || e_{ij})` with edges :math:`e_{ij}`.
The attention is obtained by :math:`\alpha_{ij} = \text{softmax}_j (a_{ij})`.
And the messages are pooled by :math:`m_i = \sum_j \alpha_{ij} W n_j`.
If the graph has no self-loops, they must be added beforehand or use external skip connections.
And optionally passed through an activation :math:`h_i = \sigma(\sum_j \alpha_{ij} W n_j)`.
An edge is defined by index tuple :math:`(i, j)` with the direction of the connection from :math:`j` to :math:`i`.
"""
def __init__(self,
units,
use_edge_features=False,
use_final_activation=True,
has_self_loops=True,
activation="kgcnn>leaky_relu2",
use_bias=True,
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
normalize_softmax: bool = False,
**kwargs):
"""Initialize layer.
Args:
units (int): Units for the linear trafo of node features before attention.
use_edge_features (bool): Append edge features to attention computation. Default is False.
use_final_activation (bool): Whether to apply the final activation for the output.
has_self_loops (bool): If the graph has self-loops. Not used here. Default is True.
activation (str): Activation. Default is "kgcnn>leaky_relu2".
use_bias (bool): Use bias. Default is True.
kernel_regularizer: Kernel regularization. Default is None.
bias_regularizer: Bias regularization. Default is None.
activity_regularizer: Activity regularization. Default is None.
kernel_constraint: Kernel constrains. Default is None.
bias_constraint: Bias constrains. Default is None.
kernel_initializer: Initializer for kernels. Default is 'glorot_uniform'.
bias_initializer: Initializer for bias. Default is 'zeros'.
"""
super(AttentionHeadGAT, self).__init__(**kwargs)
# Changes in keras serialization behaviour for activations in 3.0.2.
# Keep string at least for default. Also renames to prevent clashes with keras leaky_relu.
if activation in ["kgcnn>leaky_relu", "kgcnn>leaky_relu2"]:
activation = {"class_name": "function", "config": "kgcnn>leaky_relu2"}
self.use_edge_features = use_edge_features
self.use_final_activation = use_final_activation
self.has_self_loops = has_self_loops
self.normalize_softmax = normalize_softmax
self.units = int(units)
self.use_bias = use_bias
kernel_args = {"kernel_regularizer": kernel_regularizer,
"activity_regularizer": activity_regularizer, "bias_regularizer": bias_regularizer,
"kernel_constraint": kernel_constraint, "bias_constraint": bias_constraint,
"kernel_initializer": kernel_initializer, "bias_initializer": bias_initializer}
self.lay_linear_trafo = Dense(units, activation="linear", use_bias=use_bias, **kernel_args)
self.lay_alpha = Dense(1, activation=activation, use_bias=False, **kernel_args)
self.lay_gather_in = GatherNodesIngoing()
self.lay_gather_out = GatherNodesOutgoing()
self.lay_concat = Concatenate(axis=-1)
self.lay_pool_attention = AggregateLocalEdgesAttention(normalize_softmax=normalize_softmax)
if self.use_final_activation:
self.lay_final_activ = Activation(activation=activation)
def build(self, input_shape):
"""Build layer."""
super(AttentionHeadGAT, self).build(input_shape)
def call(self, inputs, **kwargs):
"""Forward pass.
Args:
inputs (list): of [node, edges, edge_indices]
- nodes (Tensor): Node embeddings of shape ([N], F)
- edges (Tensor): Edge or message embeddings of shape ([M], F)
- edge_indices (Tensor): Edge indices referring to nodes of shape (2, [M])
Returns:
Tensor: Embedding tensor of pooled edge attentions for each node.
"""
node, edge, edge_index = inputs
w_n = self.lay_linear_trafo(node, **kwargs)
wn_in = self.lay_gather_in([w_n, edge_index], **kwargs)
wn_out = self.lay_gather_out([w_n, edge_index], **kwargs)
if self.use_edge_features:
e_ij = self.lay_concat([wn_in, wn_out, edge], **kwargs)
else:
e_ij = self.lay_concat([wn_in, wn_out], **kwargs)
a_ij = self.lay_alpha(e_ij, **kwargs) # Should be dimension (batch, None,1)
h_i = self.lay_pool_attention([node, wn_out, a_ij, edge_index], **kwargs)
if self.use_final_activation:
h_i = self.lay_final_activ(h_i, **kwargs)
return h_i
def get_config(self):
"""Update layer config."""
config = super(AttentionHeadGAT, self).get_config()
config.update({"use_edge_features": self.use_edge_features, "use_bias": self.use_bias,
"units": self.units, "has_self_loops": self.has_self_loops,
"normalize_softmax": self.normalize_softmax,
"use_final_activation": self.use_final_activation})
conf_sub = self.lay_alpha.get_config()
for x in ["kernel_regularizer", "activity_regularizer", "bias_regularizer", "kernel_constraint",
"bias_constraint", "kernel_initializer", "bias_initializer", "activation"]:
if x in conf_sub:
config.update({x: conf_sub[x]})
return config
class AttentionHeadGATV2(Layer): # noqa
r"""Computes the modified attention head according to `GATv2 <https://arxiv.org/pdf/2105.14491.pdf>`__ .
The attention coefficients are computed by :math:`a_{ij} = a^T \sigma( W [n_i || n_j] )`,
optionally by :math:`a_{ij} = a^T \sigma( W [n_i || n_j || e_{ij}] )` with edges :math:`e_{ij}`.
The attention is obtained by :math:`\alpha_{ij} = \text{softmax}_j (a_{ij})`.
And the messages are pooled by :math:`m_i = \sum_j \alpha_{ij} e_{ij}`.
If the graph has no self-loops, they must be added beforehand or use external skip connections.
And optionally passed through an activation :math:`h_i = \sigma(\sum_j \alpha_{ij} e_{ij})`.
An edge is defined by index tuple :math:`(i, j)` with the direction of the connection from :math:`j` to :math:`i`.
"""
def __init__(self,
units,
use_edge_features=False,
use_final_activation=True,
has_self_loops=True,
activation="kgcnn>leaky_relu2",
use_bias=True,
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
normalize_softmax: bool = False,
**kwargs):
"""Initialize layer.
Args:
units (int): Units for the linear trafo of node features before attention.
use_edge_features (bool): Append edge features to attention computation. Default is False.
use_final_activation (bool): Whether to apply the final activation for the output.
has_self_loops (bool): If the graph has self-loops. Not used here. Default is True.
activation (str): Activation. Default is "kgcnn>leaky_relu2".
use_bias (bool): Use bias. Default is True.
kernel_regularizer: Kernel regularization. Default is None.
bias_regularizer: Bias regularization. Default is None.
activity_regularizer: Activity regularization. Default is None.
kernel_constraint: Kernel constrains. Default is None.
bias_constraint: Bias constrains. Default is None.
kernel_initializer: Initializer for kernels. Default is 'glorot_uniform'.
bias_initializer: Initializer for bias. Default is 'zeros'.
"""
super(AttentionHeadGATV2, self).__init__(**kwargs)
# Changes in keras serialization behaviour for activations in 3.0.2.
# Keep string at least for default. Also renames to prevent clashes with keras leaky_relu.
if activation in ["kgcnn>leaky_relu", "kgcnn>leaky_relu2"]:
activation = {"class_name": "function", "config": "kgcnn>leaky_relu2"}
self.use_edge_features = use_edge_features
self.use_final_activation = use_final_activation
self.has_self_loops = has_self_loops
self.units = int(units)
self.normalize_softmax = normalize_softmax
self.use_bias = use_bias
kernel_args = {"kernel_regularizer": kernel_regularizer,
"activity_regularizer": activity_regularizer, "bias_regularizer": bias_regularizer,
"kernel_constraint": kernel_constraint, "bias_constraint": bias_constraint,
"kernel_initializer": kernel_initializer, "bias_initializer": bias_initializer}
self.lay_linear_trafo = Dense(units, activation="linear", use_bias=use_bias, **kernel_args)
self.lay_alpha_activation = Dense(units, activation=activation, use_bias=use_bias, **kernel_args)
self.lay_alpha = Dense(1, activation="linear", use_bias=False, **kernel_args)
self.lay_gather_in = GatherNodesIngoing()
self.lay_gather_out = GatherNodesOutgoing()
self.lay_concat = Concatenate(axis=-1)
self.lay_pool_attention = AggregateLocalEdgesAttention(normalize_softmax=normalize_softmax)
if self.use_final_activation:
self.lay_final_activ = Activation(activation=activation)
def build(self, input_shape):
"""Build layer."""
super(AttentionHeadGATV2, self).build(input_shape)
def call(self, inputs, **kwargs):
"""Forward pass.
Args:
inputs (list): of [node, edges, edge_indices]
- nodes (Tensor): Node embeddings of shape ([N], F)
- edges (Tensor): Edge or message embeddings of shape ([M], F)
- edge_indices (Tensor): Edge indices referring to nodes of shape (2, [M])
Returns:
Tensor: Embedding tensor of pooled edge attentions for each node.
"""
node, edge, edge_index = inputs
w_n = self.lay_linear_trafo(node, **kwargs)
n_in = self.lay_gather_in([node, edge_index], **kwargs)
n_out = self.lay_gather_out([node, edge_index], **kwargs)
wn_out = self.lay_gather_out([w_n, edge_index], **kwargs)
if self.use_edge_features:
e_ij = self.lay_concat([n_in, n_out, edge], **kwargs)
else:
e_ij = self.lay_concat([n_in, n_out], **kwargs)
a_ij = self.lay_alpha_activation(e_ij, **kwargs)
a_ij = self.lay_alpha(a_ij, **kwargs)
h_i = self.lay_pool_attention([node, wn_out, a_ij, edge_index], **kwargs)
if self.use_final_activation:
h_i = self.lay_final_activ(h_i, **kwargs)
return h_i
def get_config(self):
"""Update layer config."""
config = super(AttentionHeadGATV2, self).get_config()
config.update({"use_edge_features": self.use_edge_features, "use_bias": self.use_bias,
"units": self.units, "has_self_loops": self.has_self_loops,
"normalize_softmax": self.normalize_softmax,
"use_final_activation": self.use_final_activation})
conf_sub = self.lay_alpha_activation.get_config()
for x in ["kernel_regularizer", "activity_regularizer", "bias_regularizer", "kernel_constraint",
"bias_constraint", "kernel_initializer", "bias_initializer", "activation"]:
if x in conf_sub:
config.update({x: conf_sub[x]})
return config
class MultiHeadGATV2Layer(Layer): # noqa
r"""Single layer for multiple Attention heads from :obj:`AttentionHeadGATV2` .
Uses concatenation or averaging of heads for final output.
"""
def __init__(self,
units: int,
num_heads: int,
activation: str = "kgcnn>leaky_relu2",
use_bias: bool = True,
concat_heads: bool = True,
use_edge_features=False,
use_final_activation=True,
has_self_loops=True,
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
normalize_softmax: bool = False,
**kwargs):
r"""Initialize layer.
Args:
units (int): Units for the linear trafo of node features before attention.
num_heads: Number of attention heads.
concat_heads: Whether to concatenate heads or average.
use_edge_features (bool): Append edge features to attention computation. Default is False.
use_final_activation (bool): Whether to apply the final activation for the output.
has_self_loops (bool): If the graph has self-loops. Not used here. Default is True.
activation (str): Activation. Default is "kgcnn>leaky_relu2".
use_bias (bool): Use bias. Default is True.
kernel_regularizer: Kernel regularization. Default is None.
bias_regularizer: Bias regularization. Default is None.
activity_regularizer: Activity regularization. Default is None.
kernel_constraint: Kernel constrains. Default is None.
bias_constraint: Bias constrains. Default is None.
kernel_initializer: Initializer for kernels. Default is 'glorot_uniform'.
bias_initializer: Initializer for bias. Default is 'zeros'.
"""
super(MultiHeadGATV2Layer, self).__init__(**kwargs)
# Changes in keras serialization behaviour for activations in 3.0.2.
# Keep string at least for default. Also renames to prevent clashes with keras leaky_relu.
if activation in ["kgcnn>leaky_relu", "kgcnn>leaky_relu2"]:
activation = {"class_name": "function", "config": "kgcnn>leaky_relu2"}
self.num_heads = num_heads
self.concat_heads = concat_heads
self.use_edge_features = use_edge_features
self.use_final_activation = use_final_activation
self.has_self_loops = has_self_loops
self.units = int(units)
self.normalize_softmax = normalize_softmax
self.use_bias = use_bias
kernel_args = {"kernel_regularizer": kernel_regularizer,
"activity_regularizer": activity_regularizer, "bias_regularizer": bias_regularizer,
"kernel_constraint": kernel_constraint, "bias_constraint": bias_constraint,
"kernel_initializer": kernel_initializer, "bias_initializer": bias_initializer}
self.head_layers = []
for _ in range(num_heads):
lay_linear = Dense(units, activation=activation, use_bias=use_bias, **kernel_args)
lay_alpha_activation = Dense(units, activation=activation, use_bias=use_bias, **kernel_args)
lay_alpha = Dense(1, activation='linear', use_bias=False, **kernel_args)
self.head_layers.append((lay_linear, lay_alpha_activation, lay_alpha))
self.lay_concat_alphas = Concatenate(axis=-2)
# self.lay_linear_trafo = Dense(units, activation="linear", use_bias=use_bias, **kernel_args)
# self.lay_alpha_activation = Dense(units, activation=activation, use_bias=use_bias, **kernel_args)
# self.lay_alpha = Dense(1, activation="linear", use_bias=False, **kernel_args)
self.lay_gather_in = GatherNodesIngoing()
self.lay_gather_out = GatherNodesOutgoing()
self.lay_concat = Concatenate(axis=-1)
self.lay_pool_attention = AggregateLocalEdgesAttention(normalize_softmax=normalize_softmax)
if self.use_final_activation:
self.lay_final_activ = Activation(activation=activation)
if self.concat_heads:
self.lay_combine_heads = Concatenate(axis=-1)
else:
self.lay_combine_heads = Average()
def build(self, input_shape):
"""Build layer."""
super(MultiHeadGATV2Layer, self).build(input_shape)
def call(self, inputs, **kwargs):
node, edge, edge_index = inputs
# "a_ij" is a single-channel edge attention logits tensor. "a_ijs" is consequently the list which
# stores these tensors for each attention head.
# "h_i" is a single-channel node embedding tensor. "h_is" is consequently the list which stores
# these tensors for each attention head.
a_ijs = []
h_is = []
for k, (lay_linear, lay_alpha_activation, lay_alpha) in enumerate(self.head_layers):
# Copied from the original class
w_n = lay_linear(node, **kwargs)
n_in = self.lay_gather_in([node, edge_index], **kwargs)
n_out = self.lay_gather_out([node, edge_index], **kwargs)
wn_out = self.lay_gather_out([w_n, edge_index], **kwargs)
if self.use_edge_features:
e_ij = self.lay_concat([n_in, n_out, edge], **kwargs)
else:
e_ij = self.lay_concat([n_in, n_out], **kwargs)
# a_ij: ([batch], [M], 1)
a_ij = lay_alpha_activation(e_ij, **kwargs)
a_ij = lay_alpha(a_ij, **kwargs)
# h_i: ([batch], [N], F)
h_i = self.lay_pool_attention([node, wn_out, a_ij, edge_index], **kwargs)
if self.use_final_activation:
h_i = self.lay_final_activ(h_i, **kwargs)
# a_ij after expand: ([batch], [M], 1, 1)
a_ij = ops.expand_dims(a_ij, axis=-2)
a_ijs.append(a_ij)
# h_i = tf.expand_dims(h_i, axis=-2)
h_is.append(h_i)
a_ijs = self.lay_concat_alphas(a_ijs)
h_is = self.lay_combine_heads(h_is)
# An important modification we need here is that this layer also returns the attention coefficients
# because in MEGAN we need those to calculate the edge attention values with!
# h_is: ([batch], [N], K * Vu) or ([batch], [N], Vu)
# a_ijs: ([batch], [M], K, 1)
return h_is, a_ijs
def get_config(self):
"""Update layer config."""
config = super(MultiHeadGATV2Layer, self).get_config()
config.update({"use_edge_features": self.use_edge_features, "use_bias": self.use_bias,
"units": self.units, "has_self_loops": self.has_self_loops,
"normalize_softmax": self.normalize_softmax,
"use_final_activation": self.use_final_activation})
if self.num_heads > 0:
conf_sub = self.head_layers[0][0].get_config()
for x in ["kernel_regularizer", "activity_regularizer", "bias_regularizer", "kernel_constraint",
"bias_constraint", "kernel_initializer", "bias_initializer", "activation"]:
if x in conf_sub:
config.update({x: conf_sub[x]})
config.update({
'num_heads': self.num_heads,
'concat_heads': self.concat_heads
})
return config
class AttentiveHeadFP(Layer):
r"""Computes the attention head for `Attentive FP <https://doi.org/10.1021/acs.jmedchem.9b00959>`__ model.
The attention coefficients are computed by :math:`a_{ij} = \sigma_1( W_1 [h_i || h_j] )`.
The initial representation :math:`h_i` and :math:`h_j` must be calculated beforehand.
The attention is obtained by :math:`\alpha_{ij} = \text{softmax}_j (a_{ij})`.
And finally pooled for context :math:`C_i = \sigma_2(\sum_j \alpha_{ij} W_2 h_j)`.
An edge is defined by index tuple :math:`(i, j)` with the direction of the connection from :math:`j` to :math:`i`.
"""
def __init__(self,
units,
use_edge_features=False,
activation="kgcnn>leaky_relu2",
activation_context="elu",
use_bias=True,
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
**kwargs):
"""Initialize layer.
Args:
units (int): Units for the linear trafo of node features before attention.
use_edge_features (bool): Append edge features to attention computation. Default is False.
activation (str): Activation. Default is "kgcnn>leaky_relu2".
activation_context (str): Activation function for context. Default is "elu".
use_bias (bool): Use bias. Default is True.
kernel_regularizer: Kernel regularization. Default is None.
bias_regularizer: Bias regularization. Default is None.
activity_regularizer: Activity regularization. Default is None.
kernel_constraint: Kernel constrains. Default is None.
bias_constraint: Bias constrains. Default is None.
kernel_initializer: Initializer for kernels. Default is 'glorot_uniform'.
bias_initializer: Initializer for bias. Default is 'zeros'.
"""
super(AttentiveHeadFP, self).__init__(**kwargs)
# Changes in keras serialization behaviour for activations in 3.0.2.
# Keep string at least for default. Also renames to prevent clashes with keras leaky_relu.
if activation in ["kgcnn>leaky_relu", "kgcnn>leaky_relu2"]:
activation = {"class_name": "function", "config": "kgcnn>leaky_relu2"}
self.use_edge_features = use_edge_features
self.units = int(units)
self.use_bias = use_bias
kernel_args = {"kernel_regularizer": kernel_regularizer,
"activity_regularizer": activity_regularizer, "bias_regularizer": bias_regularizer,
"kernel_constraint": kernel_constraint, "bias_constraint": bias_constraint,
"kernel_initializer": kernel_initializer, "bias_initializer": bias_initializer}
self.lay_linear_trafo = Dense(units, activation="linear", use_bias=use_bias, **kernel_args)
self.lay_alpha_activation = Dense(units, activation=activation, use_bias=use_bias, **kernel_args)
self.lay_alpha = Dense(1, activation="linear", use_bias=False, **kernel_args)
self.lay_gather_in = GatherNodesIngoing()
self.lay_gather_out = GatherNodesOutgoing()
self.lay_concat = Concatenate(axis=-1)
self.lay_pool_attention = AggregateLocalEdgesAttention()
self.lay_final_activ = Activation(activation=activation_context)
if use_edge_features:
self.lay_fc1 = Dense(units, activation=activation, use_bias=use_bias, **kernel_args)
self.lay_fc2 = Dense(units, activation=activation, use_bias=use_bias, **kernel_args)
self.lay_concat_edge = Concatenate(axis=-1)
def build(self, input_shape):
"""Build layer."""
super(AttentiveHeadFP, self).build(input_shape)
def call(self, inputs, **kwargs):
r"""Forward pass.
Args:
inputs (list): [node, edges, edge_indices]
- nodes (Tensor): Node embeddings of shape ([N], F)
- edges (Tensor): Edge or message embeddings of shape ([M], F)
- edge_indices (Tensor): Edge indices referring to nodes of shape ([M], 2)
Returns:
Tensor: Hidden tensor of pooled edge attentions for each node.
"""
node, edge, edge_index = inputs
if self.use_edge_features:
n_in = self.lay_gather_in([node, edge_index], **kwargs)
n_out = self.lay_gather_out([node, edge_index], **kwargs)
n_in = self.lay_fc1(n_in, **kwargs)
n_out = self.lay_concat_edge([n_out, edge], **kwargs)
n_out = self.lay_fc2(n_out, **kwargs)
else:
n_in = self.lay_gather_in([node, edge_index], **kwargs)
n_out = self.lay_gather_out([node, edge_index], **kwargs)
wn_out = self.lay_linear_trafo(n_out, **kwargs)
e_ij = self.lay_concat([n_in, n_out], **kwargs)
e_ij = self.lay_alpha_activation(e_ij, **kwargs) # Maybe uses GAT original definition.
# a_ij = e_ij
a_ij = self.lay_alpha(e_ij, **kwargs) # Should be dimension (None, 1) not fully clear in original paper.
n_i = self.lay_pool_attention([node, wn_out, a_ij, edge_index], **kwargs)
out = self.lay_final_activ(n_i, **kwargs)
return out
def get_config(self):
"""Update layer config."""
config = super(AttentiveHeadFP, self).get_config()
config.update({"use_edge_features": self.use_edge_features, "use_bias": self.use_bias,
"units": self.units})
conf_sub = self.lay_alpha_activation.get_config()
for x in ["kernel_regularizer", "activity_regularizer", "bias_regularizer", "kernel_constraint",
"bias_constraint", "kernel_initializer", "bias_initializer", "activation"]:
if x in conf_sub.keys():
config.update({x: conf_sub[x]})
conf_context = self.lay_final_activ.get_config()
config.update({"activation_context": conf_context["activation"]})
return config