Skip to content

dense_mincut_pool

dense_mincut_pool(
    adj: array,
    node_features: array,
    s: array,
    mask: Optional[array] = None,
    temp: float = 1.0,
) -> Tuple[array, array, array, array]

The MinCut pooling operator from the "Spectral Clustering in Graph Neural Networks for Graph Pooling" paper.

\[ \mathbf{X}^{\prime} &= {\mathrm{softmax}(\mathbf{S})}^{\top} \cdot \mathbf{X} \mathbf{A}^{\prime} &= {\mathrm{softmax}(\mathbf{S})}^{\top} \cdot \mathbf{A} \cdot \mathrm{softmax}(\mathbf{S}) \]

based on the dense learned assignments \(\mathbf{S} \in \mathbb{R}^{B \times N \times C}\). Also returns the MinCut loss

\[ \mathcal{L}_c = - \frac{\mathrm{Tr}(\mathbf{S}^{\top} \mathbf{A} \mathbf{S})} {\mathrm{Tr}(\mathbf{S}^{\top} \mathbf{D} \mathbf{S})} \]

where \(\mathbf{D}\) is the degree matrix, and the orthogonality loss

\[ \mathcal{L}_o = {\left\| \frac{\mathbf{S}^{\top} \mathbf{S}} {{\|\mathbf{S}^{\top} \mathbf{S}\|}_F} -\frac{\mathbf{I}_C}{\sqrt{C}} \right\|}_F. \]

As in PyG, the returned adjacency has a zero diagonal and is symmetrically normalized by its degrees.

Parameters:

Name Type Description Default
adj array

Adjacency matrices of shape [batch_size, num_nodes, num_nodes] (or [num_nodes, num_nodes]).

required
node_features array

Node features of shape [batch_size, num_nodes, num_features] (or [num_nodes, num_features]).

required
s array

Assignment logits of shape [batch_size, num_nodes, num_clusters] (or [num_nodes, num_clusters]). The softmax is applied here.

required
mask Optional[array]

Optional boolean mask of shape [batch_size, num_nodes] of the real (non-padding) nodes. Default: None

None
temp float

Temperature of the softmax. Default: 1.0

1.0

Returns:

Type Description
Tuple[array, array, array, array]

A tuple (node_features, adj, mincut_loss, ortho_loss).

Example:

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

adj = (mx.random.uniform(shape=(2, 10, 10)) > 0.7).astype(mx.float32)
x = mx.random.normal((2, 10, 16))
s = mx.random.normal((2, 10, 3))
x, adj, mincut_loss, ortho_loss = dense_mincut_pool(adj, x, s)
Source code in mlx_graphs/nn/pooling/dense_pool.py
def dense_mincut_pool(
    adj: mx.array,
    node_features: mx.array,
    s: mx.array,
    mask: Optional[mx.array] = None,
    temp: float = 1.0,
) -> Tuple[mx.array, mx.array, mx.array, mx.array]:
    r"""The MinCut pooling operator from the `"Spectral Clustering in Graph
    Neural Networks for Graph Pooling" <https://arxiv.org/abs/1907.00481>`_
    paper.

    .. math::
        \mathbf{X}^{\prime} &= {\mathrm{softmax}(\mathbf{S})}^{\top} \cdot
        \mathbf{X}

        \mathbf{A}^{\prime} &= {\mathrm{softmax}(\mathbf{S})}^{\top} \cdot
        \mathbf{A} \cdot \mathrm{softmax}(\mathbf{S})

    based on the dense learned assignments :math:`\mathbf{S} \in
    \mathbb{R}^{B \times N \times C}`. Also returns the MinCut loss

    .. math::
        \mathcal{L}_c = - \frac{\mathrm{Tr}(\mathbf{S}^{\top} \mathbf{A}
        \mathbf{S})} {\mathrm{Tr}(\mathbf{S}^{\top} \mathbf{D}
        \mathbf{S})}

    where :math:`\mathbf{D}` is the degree matrix, and the orthogonality loss

    .. math::
        \mathcal{L}_o = {\left\| \frac{\mathbf{S}^{\top} \mathbf{S}}
        {{\|\mathbf{S}^{\top} \mathbf{S}\|}_F} -\frac{\mathbf{I}_C}{\sqrt{C}}
        \right\|}_F.

    As in PyG, the returned adjacency has a zero diagonal and is
    symmetrically normalized by its degrees.

    Args:
        adj: Adjacency matrices of shape ``[batch_size, num_nodes,
            num_nodes]`` (or ``[num_nodes, num_nodes]``).
        node_features: Node features of shape ``[batch_size, num_nodes,
            num_features]`` (or ``[num_nodes, num_features]``).
        s: Assignment logits of shape ``[batch_size, num_nodes,
            num_clusters]`` (or ``[num_nodes, num_clusters]``). The softmax
            is applied here.
        mask: Optional boolean mask of shape ``[batch_size, num_nodes]`` of
            the real (non-padding) nodes. Default: ``None``
        temp: Temperature of the softmax. Default: ``1.0``

    Returns:
        A tuple ``(node_features, adj, mincut_loss, ortho_loss)``.

    Example:

    .. code-block:: python

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

        adj = (mx.random.uniform(shape=(2, 10, 10)) > 0.7).astype(mx.float32)
        x = mx.random.normal((2, 10, 16))
        s = mx.random.normal((2, 10, 3))
        x, adj, mincut_loss, ortho_loss = dense_mincut_pool(adj, x, s)
    """
    adj, x, s = _batched(adj, node_features, s)
    batch_size, num_nodes, _ = x.shape

    s = mx.softmax(s / temp if temp != 1.0 else s, axis=-1)
    if mask is not None:
        mask = mask.reshape(batch_size, num_nodes, 1).astype(x.dtype)
        x, s = x * mask, s * mask

    s_t = s.swapaxes(1, 2)
    out = s_t @ x
    out_adj = (s_t @ adj) @ s

    mincut_num = _trace(out_adj)
    # Tr(S^T D S) with D = diag(rowsum(A))
    degrees = adj.sum(axis=-1)
    mincut_den = (degrees[..., None] * s * s).sum(axis=(1, 2))
    mincut_loss = (-(mincut_num / mincut_den)).mean()

    ortho_loss = _ortho_loss(s)
    return out, _normalize_pooled_adj(out_adj), mincut_loss, ortho_loss

from mlx_graphs.nn import dense_mincut_pool · mlx_graphs/nn/pooling/dense_pool.py