Set2Set and Attention Readout: Order-Invariant Graph Summaries

8 minute read

Published:

TL;DR: Attention readout weights node embeddings by learned importance scores before summing — nodes that matter more for the task contribute more to the graph embedding. Set2Set extends this with an LSTM that makes T passes over the node set, each time computing a different attention query. This yields a richer, order-invariant graph summary.
Set2Set order-invariant readout
Set2Set: order-invariant sequence-to-sequence readout (Vinyals et al., 2016)

Beyond Uniform Pooling

Key Insight: Mean pooling computes the "average node" — it cannot distinguish a graph with one highly important node from one where all nodes are equally mediocre. Attention readout is the fix: it learns a per-node importance score during training, so the final graph embedding is dominated by the nodes that actually matter for the task.

Mean and sum pooling treat all nodes identically. But for most tasks, nodes differ greatly in importance:

  • In a molecule, the reactive functional group matters more than inert backbone atoms
  • In a social network, hubs matter more than peripheral nodes
  • In a citation graph, landmark papers matter more than derivative works

Attention readout learns these importance differences during training.

α₁α₂ α₃α₄ α₅ h_G Graph G Attention weights Embedding
Attention readout: the high-importance node (orange, large) receives a high attention weight \(\alpha_3\), dominating the graph embedding \(h_G\).

Attention Readout (Global Attention Pooling)

For each node \(v\), compute a scalar importance score:

\[ a_v \;=\; \mathrm{MLP}_{\text{gate}}\bigl(h_v\bigr) \;\in\; \mathbb{R} \]

Normalise the scores across the graph with a softmax:

\[ \alpha_v \;=\; \frac{\exp(a_v)}{\sum_{u \in V} \exp(a_u)} \]

Compute the graph embedding as a weighted sum:

\[ h_G \;=\; \sum_{v \in V} \alpha_v \, \mathrm{MLP}_{\text{out}}\bigl(h_v\bigr) \]

This is a single-pass soft attention over all nodes. The model learns which nodes to weight highly for the specific prediction task. (The original gated readout in Gated Graph Sequence Neural Networks used an unnormalised sigmoid gate rather than a softmax; the softmax-normalised form above is the variant most libraries implement, and it makes \(h_G\) a convex combination, so its scale does not grow with graph size.)

Properties:

  • Permutation-invariant: the softmax denominator is a sum over all nodes and the output is a sum of weighted terms, both unordered
  • Differentiable: all operations are smooth
  • Task-conditioned: \(\alpha_v\) depends on \(h_v\), which already encodes \(v\)’s local neighbourhood

Limitation: the logit \(a_v\) is computed from \(h_v\) alone. The softmax does couple the nodes, but only through a single global normaliser — it can rescale the weights, not change their relative order. So a node cannot be judged important because of what some other node contributes; the ranking of nodes is fixed before any comparison between them happens.

Set2Set (Vinyals et al., 2016)

Intuition first. Imagine reading a complex document by scanning it \(T\) times, each time looking for something different. On scan 1 you find the main claim; on scan 2 you look for supporting evidence; on scan 3 you check for caveats. Set2Set does the same for a graph: each LSTM step issues a different “query” that attends to a different subset of nodes, building a richer summary than any single pass could.

Set2Set produces a graph embedding using \(T\) steps of LSTM-driven attention. At each step \(t\) the LSTM emits a query vector \(q_t\), which is used to attend over all nodes:

Step \(t\):

\[ e_{v}^{t} = q_t^{\top} h_v, \qquad \alpha_v^{t} = \frac{\exp\bigl(e_v^{t}\bigr)}{\sum_{u \in V}\exp\bigl(e_u^{t}\bigr)}, \qquad m_t = \sum_{v \in V} \alpha_v^{t}\, h_v \]

The LSTM then consumes the read vector and produces the next query:

\[ \bigl(q_{t+1},\, c_{t+1}\bigr) \;=\; \mathrm{LSTM}\bigl([\,q_t \,;\, m_t\,],\; c_t\bigr) \]

After \(T\) steps, the final graph embedding is the concatenation of the last query and the last attended message:

\[ h_G \;=\; [\,q_T \,;\, m_T\,] \;\in\; \mathbb{R}^{2d} \]

Why an LSTM Readout Is Still Order-Invariant

An LSTM is the archetypal order-sensitive module, so its presence in a readout looks like a contradiction. It is not, and the reason is worth being precise about: the LSTM does not consume the nodes. It is unrolled over \(T\) processing steps, a number fixed as a hyperparameter and completely independent of \(N\). The nodes enter only through \(e_v^t\), the softmax, and the weighted sum \(m_t\) — and every one of those three is a symmetric function of the node set. Permute the nodes and each \(\alpha_v^t\) follows its node, \(m_t\) is unchanged, so \(q_{t+1}\) is unchanged, and by induction \(h_G\) is unchanged.

This is exactly the point of the paper’s title, Order Matters: feeding a set to a sequence model in some arbitrary order makes the output depend on that order, which is wrong. Set2Set’s fix is to let the recurrence run over reads of the set rather than over its elements.

Why multiple passes? At step \(t=1\) the query \(q_1\) comes from the initial LSTM state and carries no information about the graph, so attention is close to uniform. At step \(t=2\) the query has been conditioned on what step 1 read, and can direct attention elsewhere. By step \(T\) the LSTM has produced a sequence of queries, each "reading" a different aspect of the node set. This is analogous to multi-head attention reading different subspaces — with the difference that Set2Set's reads are sequential and conditioned on each other, whereas attention heads are computed in parallel and independently.

Worked Example: Set2Set on a 3-Node Graph

Consider a graph with 3 nodes and embeddings \(h_1 = [1, 0]\), \(h_2 = [0, 1]\), \(h_3 = [1, 1]\) (so \(d = 2\)). Run Set2Set with \(T = 2\) steps.

Step \(t=1\): initial query \(q_1 = [0.5, 0.5]\) (from the learned initial LSTM state)

  • Scores: \(e_1^1 = q_1^{\top}h_1 = 0.5\), \(e_2^1 = 0.5\), \(e_3^1 = 1.0\)
  • Softmax over \((0.5,\,0.5,\,1.0)\): \(\alpha^1 \approx [0.274,\; 0.274,\; 0.452]\) — node 3 wins, and nodes 1 and 2 are tied
  • Attended message: \(m_1 = 0.274\,[1,0] + 0.274\,[0,1] + 0.452\,[1,1] \approx [0.726,\; 0.726]\)
  • LSTM update: \((q_2, c_2) = \mathrm{LSTM}([q_1 ; m_1], c_1)\) — suppose it returns \(q_2 \approx [0.8, 0.2]\)

Step \(t=2\): the new query \(q_2 = [0.8, 0.2]\) emphasises the first dimension

  • Scores: \(e_1^2 = 0.8\), \(e_2^2 = 0.2\), \(e_3^2 = 1.0\)
  • Softmax over \((0.8,\,0.2,\,1.0)\): \(\alpha^2 \approx [0.361,\; 0.198,\; 0.441]\) — node 3 is still the largest, but node 1 has now overtaken node 2
  • Attended message: \(m_2 = 0.361\,[1,0] + 0.198\,[0,1] + 0.441\,[1,1] \approx [0.802,\; 0.639]\)

Final embedding: \(h_G = [q_2 ; m_2] \approx [0.8,\; 0.2,\; 0.80,\; 0.64]\), of dimension \(2d = 4\).

Notice what the second step bought: step 1 could not separate nodes 1 and 2 at all — its query was symmetric in the two coordinates, so they received identical weights. Step 2’s query, conditioned on what step 1 read, breaks that tie. A single attention pass with a symmetric query would have left nodes 1 and 2 indistinguishable in the summary.

Set2Set vs Attention Readout vs Sum

PropertySumAttention ReadoutSet2Set
Weights nodes uniformlyYesNoNo
Learns importanceNoYes (independently)Yes (iteratively)
Multiple passes over nodesNoNoYes (T passes)
Output dimension\(d\)\(d\)\(2d\)
Complexity\(O(Nd)\)\(O(Nd)\)\(O(TNd)\)
Permutation-invariantYesYesYes

When Set2Set Helps

Set2Set is particularly effective when:

  • Graph-level prediction requires integrating information from multiple disjoint node subsets
  • Different “aspects” of the graph matter for the prediction (Set2Set reads each in turn)
  • The graph size varies widely across the dataset (attention readout adapts better than fixed pooling)

Set2Set is the readout used in the MPNN of Gilmer et al. (2017) for molecular property prediction on QM9, chosen there over sum or mean precisely because it is both order-invariant and able to attend to several parts of a molecule in turn.

Multi-head Attention Readout

A simpler extension of attention readout: compute \(K\) independent attention heads, each with its own gate MLP, and concatenate:

\[ h_G \;=\; \Bigl[\; \sum_{v \in V} \alpha_v^{1} h_v \;;\; \dots \;;\; \sum_{v \in V} \alpha_v^{K} h_v \;\Bigr] \;\in\; \mathbb{R}^{Kd} \]

Each head learns to attend to a different subset of important nodes. This gives multi-aspect graph summarisation without the LSTM overhead of Set2Set.

Summary

MethodCore ideaStrength
Sum/MeanUniform aggregationSimple, fast
Attention readoutLearned per-node weightsTask-adaptive
Set2SetLSTM queries node set T timesRich multi-pass summary
Multi-head attentionMultiple independent attention poolsBalanced expressiveness/cost

For small graphs (molecules, proteins), Set2Set and multi-head attention are the readouts to reach for when a single weighted average is too blunt a summary. For large graphs, the \(O(TNd)\) cost of Set2Set and its inherently sequential \(T\) steps make single-pass attention readout the preferred choice.

References