Skip to content

MLPAggregation

MLPAggregation(
    node_features_dim: int,
    out_features_dim: int,
    max_num_elements: int,
    hidden_features_dim: Optional[int] = None,
    num_layers: int = 1,
    **mlp_kwargs: Any,
)

Bases: Aggregation

Aggregation with a multi-layer perceptron on the concatenated elements of each set, from the "Graph Neural Networks with Adaptive Readouts" paper, as PyG's MLPAggregation.

The elements of each set are padded with zeros (or truncated) to max_num_elements, flattened, and fed to an MLP.

Warning

The aggregation is not permutation-invariant: it depends on the order of the elements in each set.

Parameters:

Name Type Description Default
node_features_dim int

Size of each input sample.

required
out_features_dim int

Size of each output sample.

required
max_num_elements int

Number of elements per set fed to the MLP.

required
hidden_features_dim Optional[int]

Size of the hidden layers of the MLP. Required if num_layers > 1. Default: None

None
num_layers int

Number of layers of the MLP. Default: 1

1
**mlp_kwargs Any

Other arguments of MLP (dropout, act, norm, plain_last, bias). As in PyG's MLP, norm defaults to "batch".

{}

Example:

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

aggr = MLPAggregation(16, 32, max_num_elements=3, hidden_features_dim=64,
                      num_layers=2)
x = mx.random.normal((6, 16))
index = mx.array([0, 0, 1, 1, 1, 2])
>>> aggr(x, index).shape
(3, 32)
Source code in mlx_graphs/nn/aggr/misc.py
def __init__(
    self,
    node_features_dim: int,
    out_features_dim: int,
    max_num_elements: int,
    hidden_features_dim: Optional[int] = None,
    num_layers: int = 1,
    **mlp_kwargs: Any,
):
    super().__init__()
    from mlx_graphs.nn.models.basic_gnn import MLP

    if num_layers > 1 and hidden_features_dim is None:
        raise ValueError("`hidden_features_dim` is required if `num_layers > 1`")
    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.max_num_elements = max_num_elements
    dims = (
        [node_features_dim * max_num_elements]
        + [hidden_features_dim] * (num_layers - 1)
        + [out_features_dim]
    )
    mlp_kwargs.setdefault("norm", "batch")
    self.mlp = MLP(dims, **mlp_kwargs)

from mlx_graphs.nn.aggr import MLPAggregation · mlx_graphs/nn/aggr/misc.py