Skip to content

global_add_pool

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

Sums all node features to obtain a global graph-level representation.

If batch_indices is provided, applies pooling for each graph in the batch. The returned shape is (1, node_features_dim) if batch_indices is None, otherise the shape is (num_batches, node_features_dim).

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.

None
num_graphs Optional[int]

Number of graphs in the batch, i.e. number of rows of the output. By default, inferred as batch_indices.max() + 1.

None

Returns:

Type Description
array

An array with summed node features for all provided graphs.

Source code in mlx_graphs/nn/pooling/global_pooling.py
def global_add_pool(
    node_features: mx.array,
    batch_indices: Optional[mx.array] = None,
    num_graphs: Optional[int] = None,
) -> mx.array:
    """Sums all node features to obtain a global graph-level representation.

    If `batch_indices` is provided, applies pooling for each graph in the batch.
    The returned shape is (1, node_features_dim) if `batch_indices` is None, otherise
    the shape is (num_batches, node_features_dim).

    Args:
        node_features: Node features array.
        batch_indices: Batch array of shape (node_features.shape[0]),
            indicating for each node its batch index.
        num_graphs: Number of graphs in the batch, i.e. number of rows of the
            output. By default, inferred as ``batch_indices.max() + 1``.

    Returns:
        An array with summed node features for all provided graphs.
    """
    if batch_indices is None:
        return node_features.sum(axis=0, keepdims=True)

    out_size = num_graphs if num_graphs is not None else batch_indices.max().item() + 1
    return scatter(node_features, batch_indices, out_size=out_size, axis=0, aggr="add")

from mlx_graphs.nn import global_add_pool · mlx_graphs/nn/pooling/global_pooling.py