Skip to content

dense_diff_pool

dense_diff_pool(
    adj: array,
    node_features: array,
    s: array,
    mask: Optional[array] = None,
    normalize: bool = True,
) -> Tuple[array, array, array, array]

The differentiable pooling operator from the "Hierarchical Graph Representation Learning with Differentiable 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 auxiliary link prediction loss

\[ \mathcal{L}_{LP} = {\| \mathbf{A} - \mathrm{softmax}(\mathbf{S}) {\mathrm{softmax}(\mathbf{S})}^{\top} \|}_F, \]

divided by the number of entries of \(\mathbf{A}\) if normalize, and the entropy regularization \(\mathcal{L}_E = \frac{1}{B N} \sum_{b, n} H(\mathbf{S}_{b, n})\).

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
normalize bool

Whether to divide the link loss by the number of entries of adj. Default: True

True

Returns:

Type Description
array

A tuple (node_features, adj, link_loss, entropy_loss) with the

array

pooled features [batch_size, num_clusters, num_features] and

array

adjacency [batch_size, num_clusters, num_clusters].

Example:

import mlx.core as mx
from mlx_graphs.nn import dense_diff_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, link_loss, ent_loss = dense_diff_pool(adj, x, s)
>>> x.shape, adj.shape
((2, 3, 16), (2, 3, 3))
Source code in mlx_graphs/nn/pooling/dense_pool.py
def dense_diff_pool(
    adj: mx.array,
    node_features: mx.array,
    s: mx.array,
    mask: Optional[mx.array] = None,
    normalize: bool = True,
) -> Tuple[mx.array, mx.array, mx.array, mx.array]:
    r"""The differentiable pooling operator from the `"Hierarchical Graph
    Representation Learning with Differentiable Pooling"
    <https://arxiv.org/abs/1806.08804>`_ 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 auxiliary link
    prediction loss

    .. math::
        \mathcal{L}_{LP} = {\| \mathbf{A} - \mathrm{softmax}(\mathbf{S})
        {\mathrm{softmax}(\mathbf{S})}^{\top} \|}_F,

    divided by the number of entries of :math:`\mathbf{A}` if ``normalize``,
    and the entropy regularization
    :math:`\mathcal{L}_E = \frac{1}{B N} \sum_{b, n} H(\mathbf{S}_{b, n})`.

    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``
        normalize: Whether to divide the link loss by the number of entries of
            ``adj``. Default: ``True``

    Returns:
        A tuple ``(node_features, adj, link_loss, entropy_loss)`` with the
        pooled features ``[batch_size, num_clusters, num_features]`` and
        adjacency ``[batch_size, num_clusters, num_clusters]``.

    Example:

    .. code-block:: python

        import mlx.core as mx
        from mlx_graphs.nn import dense_diff_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, link_loss, ent_loss = dense_diff_pool(adj, x, s)
        >>> x.shape, adj.shape
        ((2, 3, 16), (2, 3, 3))
    """
    adj, x, s = _batched(adj, node_features, s)
    batch_size, num_nodes, _ = x.shape

    s = mx.softmax(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

    link_loss = _frobenius(adj - s @ s_t)
    if normalize:
        link_loss = link_loss / adj.size

    ent_loss = (-s * mx.log(s + EPS)).sum(axis=-1).mean()
    return out, out_adj, link_loss, ent_loss

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