Skip to content

EdgePooling

EdgePooling(
    node_features_dim: int,
    edge_score_method: Optional[Callable] = None,
    dropout: float = 0.0,
    add_to_edge_score: float = 0.5,
)

Bases: Module

The edge pooling operator from the "Towards Graph Pooling by Edge Contraction" and "Edge Contraction Pooling for Graph Neural Networks" papers.

Each edge gets a score from the concatenated features of its two nodes (a linear layer, followed by edge_score_method, plus add_to_edge_score). Edges are then contracted greedily by decreasing score, skipping the edges that touch an already contracted node. The features of a new node are the sum of the features of its merged nodes, multiplied by the score of the contracted edge (1 for the nodes that were not merged).

As in PyG, the greedy contraction runs on the host (numpy), and equal scores are ordered by edge index.

Parameters:

Name Type Description Default
node_features_dim int

Size of the input node features.

required
edge_score_method Optional[Callable]

Function (raw_scores, edge_index, num_nodes) computing the edge scores, e.g. compute_edge_score_softmax() (the default, a softmax over the incoming edges of each node), compute_edge_score_tanh() or compute_edge_score_sigmoid().

None
dropout float

Dropout probability of the raw edge scores. Default: 0.0

0.0
add_to_edge_score float

Value added to every edge score, so that the features of merged nodes are not scaled down too much. Default: 0.5

0.5

Example:

import mlx.core as mx
from mlx_graphs.nn import EdgePooling

pool = EdgePooling(16)
edge_index = mx.array([[0, 1, 1, 2, 3, 4], [1, 0, 2, 1, 4, 3]])
node_features = mx.random.normal((5, 16))
batch_indices = mx.array([0, 0, 0, 1, 1])

x, edge_index, batch_indices, unpool_info = pool(
    edge_index, node_features, batch_indices
)
>>> x.shape
(3, 16)
x, edge_index, batch_indices = pool.unpool(x, unpool_info)

Methods:

Name Description
compute_edge_score_softmax

Normalizes the scores with a softmax over the incoming edges of

compute_edge_score_tanh

Normalizes the scores with a tanh.

compute_edge_score_sigmoid

Normalizes the scores with a sigmoid.

__call__

Pools the graphs by contracting edges.

unpool

Unpools a pooled graph: every node gets the features of its

Source code in mlx_graphs/nn/pooling/edge_pool.py
def __init__(
    self,
    node_features_dim: int,
    edge_score_method: Optional[Callable] = None,
    dropout: float = 0.0,
    add_to_edge_score: float = 0.5,
):
    super().__init__()
    self.node_features_dim = node_features_dim
    if edge_score_method is None:
        edge_score_method = self.compute_edge_score_softmax
    self.compute_edge_score = edge_score_method
    self.add_to_edge_score = add_to_edge_score
    self.dropout = nn.Dropout(dropout) if dropout > 0.0 else None
    # PyTorch's default initialization, as in PyG
    self.lin = nn.Linear(2 * node_features_dim, 1)

compute_edge_score_softmax staticmethod

compute_edge_score_softmax(
    raw_edge_score: array, edge_index: array, num_nodes: int
) -> array

Normalizes the scores with a softmax over the incoming edges of each node.

Source code in mlx_graphs/nn/pooling/edge_pool.py
@staticmethod
def compute_edge_score_softmax(
    raw_edge_score: mx.array, edge_index: mx.array, num_nodes: int
) -> mx.array:
    """Normalizes the scores with a softmax over the incoming edges of
    each node."""
    return scatter_softmax(raw_edge_score, edge_index[1], num_nodes)

compute_edge_score_tanh staticmethod

compute_edge_score_tanh(
    raw_edge_score: array,
    edge_index: Optional[array] = None,
    num_nodes: Optional[int] = None,
) -> array

Normalizes the scores with a tanh.

Source code in mlx_graphs/nn/pooling/edge_pool.py
@staticmethod
def compute_edge_score_tanh(
    raw_edge_score: mx.array,
    edge_index: Optional[mx.array] = None,
    num_nodes: Optional[int] = None,
) -> mx.array:
    """Normalizes the scores with a tanh."""
    return mx.tanh(raw_edge_score)

compute_edge_score_sigmoid staticmethod

compute_edge_score_sigmoid(
    raw_edge_score: array,
    edge_index: Optional[array] = None,
    num_nodes: Optional[int] = None,
) -> array

Normalizes the scores with a sigmoid.

Source code in mlx_graphs/nn/pooling/edge_pool.py
@staticmethod
def compute_edge_score_sigmoid(
    raw_edge_score: mx.array,
    edge_index: Optional[mx.array] = None,
    num_nodes: Optional[int] = None,
) -> mx.array:
    """Normalizes the scores with a sigmoid."""
    return mx.sigmoid(raw_edge_score)

__call__

__call__(
    edge_index: array,
    node_features: array,
    batch_indices: Optional[array] = None,
) -> Tuple[array, array, array, UnpoolInfo]

Pools the graphs by contracting edges.

Parameters:

Name Type Description Default
edge_index array

Edges of shape [2, num_edges].

required
node_features array

Node features of shape [num_nodes, node_features_dim].

required
batch_indices Optional[array]

Graph of each node, of shape [num_nodes]. Default: None (a single graph).

None

Returns:

Type Description
array

A tuple (node_features, edge_index, batch_indices, unpool_info)

array

with the pooled graph (whose edges are coalesced, and include the

array

self-loops of the contracted edges) and the information needed by

UnpoolInfo

unpool().

Source code in mlx_graphs/nn/pooling/edge_pool.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: mx.array,
    batch_indices: Optional[mx.array] = None,
) -> Tuple[mx.array, mx.array, mx.array, UnpoolInfo]:
    """Pools the graphs by contracting edges.

    Args:
        edge_index: Edges of shape ``[2, num_edges]``.
        node_features: Node features of shape ``[num_nodes,
            node_features_dim]``.
        batch_indices: Graph of each node, of shape ``[num_nodes]``.
            Default: ``None`` (a single graph).

    Returns:
        A tuple ``(node_features, edge_index, batch_indices, unpool_info)``
        with the pooled graph (whose edges are coalesced, and include the
        self-loops of the contracted edges) and the information needed by
        :meth:`unpool`.
    """
    num_nodes = node_features.shape[0]
    if batch_indices is None:
        batch_indices = mx.zeros((num_nodes,), dtype=mx.int32)
    e = mx.concatenate(
        [node_features[edge_index[0]], node_features[edge_index[1]]], axis=-1
    )
    e = self.lin(e).reshape(-1)
    if self.dropout is not None:
        e = self.dropout(e)
    e = self.compute_edge_score(e, edge_index, num_nodes)
    e = e + self.add_to_edge_score
    return self._merge_edges(edge_index, node_features, batch_indices, e)

unpool

unpool(
    node_features: array, unpool_info: UnpoolInfo
) -> Tuple[array, array, array]

Unpools a pooled graph: every node gets the features of its cluster, divided by the score of the contracted edge.

Parameters:

Name Type Description Default
node_features array

Features of the pooled nodes.

required
unpool_info UnpoolInfo

The information returned by the pooling step.

required

Returns:

Type Description
array

A tuple (node_features, edge_index, batch_indices) of the

array

unpooled graph.

Source code in mlx_graphs/nn/pooling/edge_pool.py
def unpool(
    self, node_features: mx.array, unpool_info: UnpoolInfo
) -> Tuple[mx.array, mx.array, mx.array]:
    """Unpools a pooled graph: every node gets the features of its
    cluster, divided by the score of the contracted edge.

    Args:
        node_features: Features of the pooled nodes.
        unpool_info: The information returned by the pooling step.

    Returns:
        A tuple ``(node_features, edge_index, batch_indices)`` of the
        unpooled graph.
    """
    new_x = node_features / unpool_info.new_edge_score.reshape(-1, 1)
    new_x = new_x[unpool_info.cluster]
    return new_x, unpool_info.edge_index, unpool_info.batch_indices

from mlx_graphs.nn import EdgePooling · mlx_graphs/nn/pooling/edge_pool.py