Skip to content

SetTransformerAggregation

SetTransformerAggregation(
    channels: int,
    num_seed_points: int = 1,
    num_encoder_blocks: int = 1,
    num_decoder_blocks: int = 1,
    heads: int = 1,
    concat: bool = True,
    layer_norm: bool = False,
    dropout: float = 0.0,
)

Bases: Aggregation

Aggregation with a Set Transformer, from the "Set Transformer: A Framework for Attention-based Permutation-Invariant Neural Networks" paper, as PyG's SetTransformerAggregation.

The elements of each set are encoded with num_encoder_blocks Set Attention Blocks (SAB), pooled onto num_seed_points learnable seeds by Pooling by Multihead Attention (PMA), and decoded with num_decoder_blocks SABs. The seeds of empty sets attend to nothing.

Parameters:

Name Type Description Default
channels int

Size of each input sample.

required
num_seed_points int

Number of seed points. Default: 1

1
num_encoder_blocks int

Number of SABs in the encoder. Default: 1

1
num_decoder_blocks int

Number of SABs in the decoder. Default: 1

1
heads int

Number of attention heads. Default: 1

1
concat bool

If True, the outputs of the seed points are concatenated (output size num_seed_points * channels); otherwise they are averaged. Default: True

True
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 SetTransformerAggregation

aggr = SetTransformerAggregation(16, num_seed_points=2, heads=4)
x = mx.random.normal((6, 16))
index = mx.array([0, 0, 1, 1, 1, 2])
>>> aggr(x, index).shape
(3, 32)

Methods:

Name Description
aggregate

Aggregates x.

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

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
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``
    """
    _check_2d(self, x)
    layout = DenseLayout(index, x.shape[0], dim_size, max_num_elements)
    x, mask = layout.to_dense(x), layout.mask
    for encoder in self.encoders:
        x = encoder(x, mask)
    x = self.pma(x, mask)
    for decoder in self.decoders:
        x = decoder(x)
    x = mx.nan_to_num(x)
    return x.reshape(x.shape[0], -1) if self.concat else x.mean(axis=1)

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