DiffPool: Learning Hierarchical Graph Pooling

9 minute read

Published:

TL;DR: DiffPool (Ying et al., 2018) uses a second GNN to learn soft cluster assignments โ€” each node is assigned fractionally to each cluster. The coarsened graph is then passed to another GNN, building a hierarchy from fine-grained nodes to coarse super-nodes. It's end-to-end differentiable and captures multi-scale structure that flat pooling misses.
DiffPool hierarchical pooling
DiffPool: differentiable graph pooling with soft cluster assignments (Ying et al., 2018)

Intuition First: Hierarchical Pooling as a Convolutional Pyramid

In image CNNs, max-pooling after each conv layer progressively coarsens the spatial grid: \(32 \times 32 \to 16 \times 16 \to 8 \times 8 \to 1 \times 1\). This captures multi-scale features โ€” edges at fine scale, shapes at coarser scale, objects at the coarsest. DiffPool brings this pyramid idea to graphs: \(\text{nodes} \to \text{clusters} \to \text{super-clusters} \to \text{graph}\). The key challenge is that unlike pixels, graph nodes have no fixed spatial ordering, so the pooling must be learned and permutation-invariant.

Key Insight: DiffPool's assignment matrix \(S\) is the graph analogue of a pooling kernel. But instead of a fixed stride-2 window, \(S\) is learned โ€” the model discovers which nodes should be clustered together based on both features and graph structure, without any external supervision on the clustering.
Level 0 (8 nodes) Sโฝโฐโพ Level 1 (4 clusters) Sโฝยนโพ Level 2 (2) global pool โ†’ h_G
DiffPool hierarchically coarsens the graph: 8 nodes โ†’ 4 clusters โ†’ 2 super-clusters โ†’ graph embedding. Each level learns which nodes to merge via a soft assignment matrix S.

The Limitation of Flat Global Pooling

Global mean/sum/max pooling jumps directly from \(N\) node embeddings to a single graph embedding. For graphs with hierarchical structure โ€” like molecules (atoms โ†’ functional groups โ†’ whole molecule) or social networks (people โ†’ communities โ†’ factions) โ€” this skips all intermediate scales.

CNNs solve this with hierarchical pooling (max-pool after each conv layer). DiffPool brings the same idea to graphs, with the key challenge: graph pooling must be permutation-invariant and must handle variable numbers of nodes.

The DiffPool Architecture

DiffPool processes each pooling level \(l\) with two GNNs, both run on the same inputs โ€” the levelโ€™s adjacency \(A^{(l)} \in \mathbb{R}^{N_l \times N_l}\) and its node feature matrix \(X^{(l)} \in \mathbb{R}^{N_l \times d}\) (at the first level, \(X^{(0)} = X\) and \(A^{(0)} = A\)).

1. Embedding GNN โ€” computes node embeddings:

\[ Z^{(l)} \;=\; \mathrm{GNN}_{l,\,\text{embed}}\bigl(A^{(l)},\, X^{(l)}\bigr) \;\in\; \mathbb{R}^{N_l \times d} \]

2. Pooling GNN โ€” computes cluster assignments:

\[ S^{(l)} \;=\; \operatorname{softmax}\Bigl(\mathrm{GNN}_{l,\,\text{pool}}\bigl(A^{(l)},\, X^{(l)}\bigr)\Bigr) \;\in\; \mathbb{R}^{N_l \times k_l} \]

\(S^{(l)}\) is the soft assignment matrix: \(S^{(l)}_{ij}\) is the probability that node \(i\) at level \(l\) belongs to cluster \(j\) at level \(l+1\). The softmax is applied row-wise, so every row sums to one โ€” each node distributes a unit of โ€œmassโ€ across the \(k_l\) clusters. Note that the two GNNs share an architecture but not weights: one is trained to produce features, the other to produce a clustering.

Coarsening: the assignment matrix is then used twice โ€” once to pool features, once to pool the graph:

\[ X^{(l+1)} \;=\; S^{(l)\top} Z^{(l)} \;\in\; \mathbb{R}^{k_l \times d}, \qquad A^{(l+1)} \;=\; S^{(l)\top} A^{(l)} S^{(l)} \;\in\; \mathbb{R}^{k_l \times k_l} \]

\(X^{(l+1)}\) is the input feature matrix of the next level: each clusterโ€™s embedding is the assignment-weighted sum of its member nodesโ€™ embeddings. The new adjacency \(A^{(l+1)}\) is the cluster-to-cluster connectivity โ€” entry \((a,b)\) accumulates the edge weight between clusters \(a\) and \(b\) over all pairs of constituent nodes. Because \(S^{(l)}\) is dense, \(A^{(l+1)}\) is a dense weighted adjacency even when \(A^{(l)}\) was sparse.

Why Soft Assignment?

Hard assignment (each node assigned to exactly one cluster) would require an \(\arg\max\) โ€” not differentiable. Soft assignment (each node fractionally assigned to all clusters) allows end-to-end gradient flow.

The assignment \(S^{(l)}\) is learned jointly with the rest of the network. The model discovers which nodes should be clustered together โ€” without any external supervision on the clustering.

The analogy to attention: DiffPool's soft assignment is conceptually similar to attention in Transformers โ€” it learns a soft selection over items. Here, each cluster "attends" to nodes, and the resulting cluster embedding is a weighted sum of node embeddings, \(S^{\top} Z\). The difference: DiffPool reduces the number of tokens (nodes coarsened to clusters), while attention preserves the token count.

Auxiliary Loss Terms

Nothing in the architecture so far forces \(S^{(l)}\) to be a sensible clustering โ€” a degenerate assignment that spreads every node uniformly over all clusters is perfectly differentiable and perfectly useless. DiffPool therefore adds two auxiliary losses, minimised alongside the task loss at every pooling layer.

Auxiliary link prediction loss: encourages nodes connected by an edge to be assigned to the same cluster:

\[ L_{\mathrm{LP}} \;=\; \bigl\lVert A^{(l)} - S^{(l)} S^{(l)\top} \bigr\rVert_{F} \]

\(\bigl(S^{(l)}S^{(l)\top}\bigr)_{ij}\) is the probability that nodes \(i\) and \(j\) land in the same cluster. Pushing that matrix towards \(A^{(l)}\) makes the clustering agree with the observed edges, so clusters correspond to densely connected regions rather than arbitrary groups of nodes. The name is a little misleading: no link prediction task is being solved โ€” it is a regulariser that reads the adjacency as a target.

Entropy loss: encourages each nodeโ€™s assignment to be concentrated rather than uniformly spread:

\[ L_{E} \;=\; \frac{1}{N_l}\sum_{i=1}^{N_l} H\bigl(S^{(l)}_i\bigr), \qquad H(p) = -\sum_{j} p_j \log p_j \]

where \(S^{(l)}_i\) is row \(i\) of the assignment matrix. Minimising \(L_E\) drives each row towards a one-hot vector, so the soft assignment approaches a hard clustering as training progresses โ€” sharper clusters, and a coarsened adjacency that is less blurred.

Why these losses are not optional: the DiffPool paper reports that training the pooling GNN from the task gradient alone is unstable and often converges to a spurious local optimum. The two auxiliary terms are what make the clustering trainable in practice, at the cost of two extra loss weights to tune.

Concrete Worked Example: One DiffPool Step

Suppose we have 4 nodes and want to pool to 2 clusters. After the embedding GNN, the node embeddings are \(Z \in \mathbb{R}^{4 \times 3}\):

Z = [[1, 0, 1],   # node 0
     [1, 0, 0],   # node 1 โ€” similar to node 0
     [0, 1, 1],   # node 2
     [0, 1, 0]]   # node 3 โ€” similar to node 2

The pooling GNN outputs raw logits; the row-wise softmax turns them into the assignment \(S \in \mathbb{R}^{4 \times 2}\):

S = [[0.9, 0.1],   # node 0 โ†’ mostly cluster A
     [0.8, 0.2],   # node 1 โ†’ mostly cluster A
     [0.1, 0.9],   # node 2 โ†’ mostly cluster B
     [0.2, 0.8]]   # node 3 โ†’ mostly cluster B

New cluster features: \(X' = S^{\top} Z \in \mathbb{R}^{2 \times 3}\). Each row of \(X'\) is the column of \(S\) for that cluster, used as weights over the rows of \(Z\):

X'[A] = 0.9ร—[1,0,1] + 0.8ร—[1,0,0] + 0.1ร—[0,1,1] + 0.2ร—[0,1,0]
      = [1.7, 0.3, 1.0]   (cluster A = nodes 0+1)
X'[B] = 0.1ร—[1,0,1] + 0.2ร—[1,0,0] + 0.9ร—[0,1,1] + 0.8ร—[0,1,0]
      = [0.3, 1.7, 1.0]   (cluster B = nodes 2+3)

The soft assignment correctly merged structurally similar nodes (0,1) into cluster A and (2,3) into cluster B โ€” learned purely from features and graph structure. Note that the two clusters agree exactly in the third coordinate: that dimension was uninformative about the split, and the pooling reflects that rather than manufacturing a difference.

Computational Cost

DiffPool runs two GNNs at each level. For a graph with \(N\) nodes coarsened to \(k\) clusters:

  • Assignment: \(S \in \mathbb{R}^{N \times k}\) is dense โ€” \(O(Nk)\) entries, and every node has a nonzero weight on every cluster.
  • Coarsening the adjacency: \(S^{\top} A S\) costs \(O(N^2 k)\) with a dense \(A\), and produces a dense \(k \times k\) result.
  • Memory: \(O(N^2)\) โ€” the dominant term is the dense adjacency \(A^{(l)}\) itself, which DiffPool requires because \(A^{(l+1)}\) is dense from the second level onward regardless of how sparse the input graph was.

This \(O(N^2)\) memory cost is the defining practical constraint. It is not an implementation detail that a better sparse kernel could remove: the soft assignment genuinely connects every cluster to every node, so the coarsened graph is genuinely dense. Batching compounds it โ€” graphs in a batch must be padded to a common \(N_{\max}\). DiffPool is therefore practical for graphs with hundreds of nodes (molecules, proteins) but not for social networks or knowledge graphs with millions.

When DiffPool Helps

DiffPool outperforms flat pooling when:

  1. The task requires multi-scale understanding: molecular property prediction benefits from atom-level and functional-group-level representations simultaneously
  2. Graphs have natural hierarchical structure: trees, clustered communities, hierarchical molecules
  3. The graph is small enough: typically a few hundred to a thousand nodes

It remains a standard reference point on the TUDataset graph classification benchmarks the original paper used โ€” ENZYMES, D&D, PROTEINS, COLLAB and REDDIT-MULTI-12K.

Limitations

  1. Quadratic memory: no scaling to large graphs
  2. Fixed number of clusters: \(k_l\) must be chosen per level before training, and since the same \(k_l\) applies to every graph in the dataset it is usually set from a percentage of the largest graph
  3. No guarantee of meaningful clusters: the auxiliary losses help but do not force semantically meaningful groupings
  4. Sensitive to the number of pooling levels: too many levels โ†’ over-compression; too few โ†’ flat pooling

Summary

ComponentRole
\(\mathrm{GNN}_{l,\text{embed}}\)Compute node representations at this scale
\(\mathrm{GNN}_{l,\text{pool}}\)Learn soft cluster assignments \(S^{(l)}\)
\(X^{(l+1)} = S^{(l)\top} Z^{(l)}\)Aggregate node embeddings into cluster embeddings
\(A^{(l+1)} = S^{(l)\top} A^{(l)} S^{(l)}\)Coarsen adjacency to cluster graph
\(L_{\mathrm{LP}} + L_{E}\)Encourage connectivity-aligned, sharp clusters

DiffPool introduced the idea of learned hierarchical pooling for graphs โ€” differentiable, end-to-end, and structure-aware. Its quadratic complexity limits scale, but for small-graph tasks (molecules, proteins), it remains a reference architecture.

References