AttentionalAggregation
¶
AttentionalAggregation(gate_nn: Module, nn: Optional[Module] = None)
Bases: Module
Soft attention pooling (global attention) from "Gated Graph Sequence Neural Networks" paper.
where \(\mathcal{V}_g\) are the nodes of graph \(g\),
\(h_{\textrm{gate}}\) (gate_nn) computes the attention scores and
\(h_{\mathbf{\Theta}}\) (nn) optionally transforms the node features
before pooling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gate_nn
|
Module
|
Callable |
required |
nn
|
Optional[Module]
|
Optional callable |
None
|
Example:
import mlx.core as mx
import mlx.nn as nn
from mlx_graphs.nn.pooling.attentional_aggregation import (
AttentionalAggregation,
)
pool = AttentionalAggregation(gate_nn=nn.Linear(16, 1), nn=nn.Linear(16, 32))
node_features = mx.random.uniform(shape=(5, 16))
batch_indices = mx.array([0, 0, 0, 1, 1])
>>> pool(node_features, batch_indices).shape
(2, 32)
Methods:
| Name | Description |
|---|---|
__call__ |
Pools the node features of each graph with attention. |
Source code in mlx_graphs/nn/pooling/attentional_aggregation.py
__call__
¶
__call__(
node_features: array,
batch_indices: Optional[array] = None,
num_graphs: Optional[int] = None,
) -> array
Pools the node features of each graph with attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features
|
array
|
Node features array. |
required |
batch_indices
|
Optional[array]
|
Batch array of shape (node_features.shape[0]), indicating for each node its batch index. If not provided, all nodes belong to a single graph. |
None
|
num_graphs
|
Optional[int]
|
Number of graphs in the batch. By default, inferred as
|
None
|
Returns:
| Type | Description |
|---|---|
array
|
An array of shape (num_graphs, out_features_dim) with the pooled |
array
|
features of each graph, (1, out_features_dim) if |
array
|
None. Graphs without nodes get zeros. |
Source code in mlx_graphs/nn/pooling/attentional_aggregation.py
from mlx_graphs.nn import AttentionalAggregation · mlx_graphs/nn/pooling/attentional_aggregation.py