Skip to content

Set2Set

Set2Set(node_features_dim: int, processing_steps: int)

Bases: Module

Set2Set global pooling from "Order Matters: Sequence to Sequence for Sets" paper.

Starting from \(\mathbf{q}^{*}_0 = \mathbf{0}\), each processing step \(t\) reads the nodes of a graph \(\mathcal{V}_g\) with attention:

\[ \mathbf{q}_t &= \textrm{LSTM}(\mathbf{q}^{*}_{t-1}) \alpha_{i,t} &= \textrm{softmax}_{i \in \mathcal{V}_g} \left( \mathbf{x}_i \cdot \mathbf{q}_t \right) \mathbf{r}_t &= \sum_{i \in \mathcal{V}_g} \alpha_{i,t} \mathbf{x}_i \mathbf{q}^{*}_t &= \mathbf{q}_t \, \Vert \, \mathbf{r}_t, \]

where \(\mathbf{q}^{*}_T\) is the output of the layer, with twice the size of the input features.

Parameters:

Name Type Description Default
node_features_dim int

Size of input node features

required
processing_steps int

Number of processing steps \(T\)

required

Example:

import mlx.core as mx
from mlx_graphs.nn.pooling.set2set import Set2Set

pool = Set2Set(16, processing_steps=3)

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 Set2Set.

Source code in mlx_graphs/nn/pooling/set2set.py
def __init__(self, node_features_dim: int, processing_steps: int):
    super().__init__()

    self.node_features_dim = node_features_dim
    self.out_features_dim = 2 * node_features_dim
    self.processing_steps = processing_steps

    self.lstm = nn.LSTM(self.out_features_dim, node_features_dim)

__call__

__call__(
    node_features: array,
    batch_indices: Optional[array] = None,
    num_graphs: Optional[int] = None,
) -> array

Pools the node features of each graph with Set2Set.

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 batch_indices.max() + 1.

None

Returns:

Type Description
array

An array of shape (num_graphs, 2 * node_features_dim) with the pooled

array

features of each graph, (1, 2 * node_features_dim) if batch_indices

array

is None.

Source code in mlx_graphs/nn/pooling/set2set.py
def __call__(
    self,
    node_features: mx.array,
    batch_indices: Optional[mx.array] = None,
    num_graphs: Optional[int] = None,
) -> mx.array:
    """Pools the node features of each graph with Set2Set.

    Args:
        node_features: Node features array.
        batch_indices: 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.
        num_graphs: Number of graphs in the batch. By default, inferred as
            ``batch_indices.max() + 1``.

    Returns:
        An array of shape (num_graphs, 2 * node_features_dim) with the pooled
        features of each graph, (1, 2 * node_features_dim) if `batch_indices`
        is None.
    """
    if batch_indices is None:
        batch_indices = mx.zeros((node_features.shape[0],), dtype=mx.int32)
        num_graphs = 1
    elif num_graphs is None:
        num_graphs = batch_indices.max().item() + 1

    dtype = node_features.dtype
    hidden = mx.zeros((num_graphs, self.node_features_dim), dtype=dtype)
    cell = mx.zeros((num_graphs, self.node_features_dim), dtype=dtype)
    q_star = mx.zeros((num_graphs, self.out_features_dim), dtype=dtype)

    for _ in range(self.processing_steps):
        # one LSTM step per graph: sequences of length 1
        all_hidden, all_cell = self.lstm(q_star[:, None, :], hidden, cell)
        hidden, cell = all_hidden[:, 0], all_cell[:, 0]
        query = hidden  # [num_graphs, node_features_dim]

        # attention of each node to the query of its graph
        energy = (node_features * query[batch_indices]).sum(axis=-1, keepdims=True)
        alpha = scatter(energy, batch_indices, num_graphs, aggr="softmax")
        readout = scatter(
            alpha * node_features, batch_indices, num_graphs, aggr="add"
        )

        q_star = mx.concatenate([query, readout], axis=-1)

    return q_star

from mlx_graphs.nn import Set2Set · mlx_graphs/nn/pooling/set2set.py