Skip to content

GraphMultisetTransformer

GraphMultisetTransformer(
    channels: int,
    k: int,
    num_encoder_blocks: int = 1,
    heads: int = 1,
    layer_norm: bool = False,
    dropout: float = 0.0,
)

Bases: Aggregation

The Graph Multiset Transformer pooling operator from the "Accurate Learning of Graph Representations with Graph Multiset Pooling" paper, as PyG's GraphMultisetTransformer.

The elements of each set (e.g. node embeddings computed by a GNN) are pooled onto k seed points by Pooling by Multihead Attention (PMA), processed by num_encoder_blocks Set Attention Blocks (SAB), and pooled again onto a single seed point.

Parameters:

Name Type Description Default
channels int

Size of each input sample.

required
k int

Number of seed points of the first PMA.

required
num_encoder_blocks int

Number of SABs between the two PMAs. Default: 1

1
heads int

Number of attention heads. Default: 1

1
layer_norm bool

Whether to apply layer normalization. Default: False

False
dropout float

Dropout probability of the attention weights. Default: 0.0

0.0

Example:

import mlx.core as mx
from mlx_graphs.nn.aggr import GraphMultisetTransformer

aggr = GraphMultisetTransformer(16, k=4, heads=2)
x = mx.random.normal((6, 16))
batch_indices = mx.array([0, 0, 1, 1, 1, 2])
>>> aggr(x, batch_indices).shape
(3, 16)

Methods:

Name Description
aggregate

Aggregates x.

Source code in mlx_graphs/nn/aggr/set_transformer.py
def __init__(
    self,
    channels: int,
    k: int,
    num_encoder_blocks: int = 1,
    heads: int = 1,
    layer_norm: bool = False,
    dropout: float = 0.0,
):
    super().__init__()
    self.channels = channels
    self.k = k
    self.heads = heads
    self.pma1 = PoolingByMultiheadAttention(channels, k, heads, layer_norm, dropout)
    self.encoders = [
        SetAttentionBlock(channels, heads, layer_norm, dropout)
        for _ in range(num_encoder_blocks)
    ]
    self.pma2 = PoolingByMultiheadAttention(channels, 1, heads, layer_norm, dropout)

aggregate

aggregate(
    x: array,
    index: array,
    dim_size: int,
    max_num_elements: Optional[int] = None,
    **kwargs,
) -> array

Aggregates x.

Parameters:

Name Type Description Default
x array

Array of shape [num_elements, channels].

required
index array

Set of each element.

required
dim_size int

Number of sets.

required
max_num_elements Optional[int]

Only the first max_num_elements elements of each set (in input order) are used. Default: None

None

Returns:

Type Description
array

An array of shape [dim_size, channels].

Source code in mlx_graphs/nn/aggr/set_transformer.py
def aggregate(
    self,
    x: mx.array,
    index: mx.array,
    dim_size: int,
    max_num_elements: Optional[int] = None,
    **kwargs,
) -> mx.array:
    """Aggregates ``x``.

    Args:
        x: Array of shape ``[num_elements, channels]``.
        index: Set of each element.
        dim_size: Number of sets.
        max_num_elements: Only the first ``max_num_elements`` elements of
            each set (in input order) are used. Default: ``None``

    Returns:
        An array of shape ``[dim_size, channels]``.
    """
    _check_2d(self, x)
    layout = DenseLayout(index, x.shape[0], dim_size, max_num_elements)
    x = self.pma1(layout.to_dense(x), layout.mask)
    for encoder in self.encoders:
        x = encoder(x)
    return self.pma2(x)[:, 0]

from mlx_graphs.nn.aggr import GraphMultisetTransformer · mlx_graphs/nn/aggr/set_transformer.py