Search

Search papers and lessons

Graph Readout

Level 3 · 10 min read

A graph readout combines the learned vectors of all nodes into one vector for the whole graph, in a way that ignores the order the nodes are listed in.

On this page

  • Colour = topic mix
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Architectures
  • Calculus
  • ReadReady to readNot yet

TL;DR

A graph neural network gives every node its own learned vector. A graph readout turns that set of node vectors into one vector that describes the whole graph, which is what a prediction about the whole graph needs. The readout must not depend on the order in which the nodes are listed, because a graph has no natural order. Sum, mean and max are the common choices.

Builds on: Graph Neural Network, Max Pooling · Leads to: Message Passing Neural Network

Where it's used

Predicting a property of a molecule. A molecule is a Graph whose nodes are atoms and whose edges are bonds. A network can learn a vector for each atom, but the quantity a chemist wants, such as the energy of the whole molecule, belongs to the molecule as a whole. The readout step collapses the atom vectors into one molecule vector, and a final layer turns that into the predicted number. Gilmer and colleagues applied this kind of framework to predicting properties of molecules in 2017.

Classifying a social network or a document graph. A graph of users and friendships, or of words and their neighbours, may need one label for the whole graph, such as whether a community is a spam ring. The same readout step gives one vector for the whole graph, whatever its size.

Comparing graphs of different sizes. A road network, a protein and a small chemical all have different numbers of nodes. A readout produces a fixed-length vector for each, so the same downstream classifier can be applied to all of them.

How it works

Take a tiny graph with three nodes. After the graph neural network has run, each node has a scalar value. We keep scalars for now so the arithmetic is easy to follow.

  1. Start with the node values. Node A has value 1, node B has value 2 and node C has value 6. Check: there are three values and no order is implied by the graph.
  2. List them in some order. Suppose the list is A, B, C, so it is (1,2,6)(1, 2, 6). Check: this is one of six possible orders of the same three values.
  3. Combine the list into one number. Use the sum. Add the values: 1+2+6=91 + 2 + 6 = 9. Check: the answer is one number, whatever the size of the graph.
  4. Try a different order. List them as C, A, B, so it is (6,1,2)(6, 1, 2). Add again: 6+1+2=96 + 1 + 2 = 9. Check: the result is the same, so the list order did not matter.
  5. Try the mean and the max. The mean is 9/3=39 / 3 = 3. The max is 66. Check: each one is a single number that depends only on the set of values, not on their order.
  6. Feed the result to a predictor. Suppose the prediction is the graph readout value times 0.5. With the sum, the prediction is 0.5×9=4.50.5 \times 9 = 4.5. Check: the same graph always gives the same prediction, however its nodes were numbered in the input file.

The step that matters is step 4. A readout that gave a different answer for different orderings would make the prediction depend on how the graph happened to be stored, which is not a property of the graph.

The math

Definition: Graph readout

Let {hv}v∈V\{\mathbf{h}_v\}_{v \in V} be the final vectors of the nodes of a graph G=(V,E)G = (V, E), where each hv\mathbf{h}_v is a Vector of fixed length. A readout is a function RR that maps this set of vectors to one vector

hG=R({hv:v∈V}),\mathbf{h}_G = R\big(\{\mathbf{h}_v : v \in V\}\big),

and it is permutation invariant: relabelling the nodes does not change hG\mathbf{h}_G.

The braces {⋅}\{\cdot\} denote a set, so the notation already says that the order of the nodes is not used. The readout is defined on sets of vectors, not on lists.

Step 1 in symbols: the node values

In the walkthrough the node vectors are the scalars hA=1h_A = 1, hB=2h_B = 2, hC=6h_C = 6. In general each hv\mathbf{h}_v is a vector with many entries, produced by the graph network's last layer.

Step 2 in symbols: an ordering is not part of the input

Any order of the three values is a permutation π\pi of the set. The readout is permutation invariant when

R({hπ(1),hπ(2),hπ(3)})=R({h1,h2,h3})R\big(\{h_{\pi(1)}, h_{\pi(2)}, h_{\pi(3)}\}\big) = R\big(\{h_1, h_2, h_3\}\big)

for every permutation π\pi. Writing the input with braces is what makes this condition meaningful.

Step 3 in symbols: sum, mean and max

Three readouts meet this condition, the mean among them:

Rsum=∑v∈Vhv,Rmean=1∣V∣∑v∈Vhv,Rmax=max⁡v∈Vhv,R_{\text{sum}} = \sum_{v \in V} \mathbf{h}_v, \qquad R_{\text{mean}} = \frac{1}{|V|} \sum_{v \in V} \mathbf{h}_v, \qquad R_{\text{max}} = \max_{v \in V} \mathbf{h}_v,

where the maximum is taken entry by entry. Addition is commutative, so the sum does not depend on order. The max and the mean depend only on the set of values.

For the walkthrough, ∣V∣=3|V| = 3, so Rsum=1+2+6=9R_{\text{sum}} = 1 + 2 + 6 = 9, Rmean=9/3=3R_{\text{mean}} = 9 / 3 = 3 and Rmax=6R_{\text{max}} = 6.

Step 4 in symbols: the prediction

The readout vector feeds a learned function ff, for example y^=f(hG)\hat{y} = f(\mathbf{h}_G). In the walkthrough ff is a Linear Function, f(x)=0.5xf(x) = 0.5x, so with the sum y^=4.5\hat{y} = 4.5. Changing the order of the nodes changes nothing in this chain, because RR has already removed the order.

Worked examples

Example
Example 1: sum of three numbers

Take the node values 11, 22 and 66, and use the sum readout.

  1. Write the values as a set: {1,2,6}\{1, 2, 6\}.
  2. Add them: 1+2=31 + 2 = 3, then 3+6=93 + 6 = 9.
  3. Reorder to {6,2,1}\{6, 2, 1\} and add again: 6+2=86 + 2 = 8, then 8+1=98 + 1 = 9.
  4. Both orders give 9.

Interpretation: the sum is a valid readout because it gives the same answer for any order of the nodes.

Example
Example 2: a molecule with four atoms and two features

A small molecule has four atoms. After the graph network runs, each atom has a two-entry vector, and the readout is the mean. The atoms have vectors (1,0)(1, 0), (0,1)(0, 1), (1,1)(1, 1) and (2,0)(2, 0).

  1. Add the first entries: 1+0+1+2=41 + 0 + 1 + 2 = 4. Add the second entries: 0+1+1+0=20 + 1 + 1 + 0 = 2. The sum is (4,2)(4, 2).
  2. Divide by the number of atoms, which is 4: the mean is (4/4,2/4)=(1,0.5)(4 / 4, 2 / 4) = (1, 0.5).
  3. Take the max entry by entry instead: the first entries give max⁡(1,0,1,2)=2\max(1, 0, 1, 2) = 2, and the second entries give max⁡(0,1,1,0)=1\max(0, 1, 1, 0) = 1. The max is (2,1)(2, 1).
  4. Apply a linear predictor y^=w⋅hG\hat{y} = \mathbf{w} \cdot \mathbf{h}_G with w=(1,−1)\mathbf{w} = (1, -1) to the mean: y^=1×1+(−1)×0.5=0.5\hat{y} = 1 \times 1 + (-1) \times 0.5 = 0.5.
  5. Check: the value 0.50.5 is the same for any numbering of the four atoms, because the mean does not depend on order.

Interpretation: the readout turns four vectors into one, and the predictor works on that single vector. The choice of readout changes what the predictor can see, so the same network with a different readout can give a different answer.

Example
Example 3: sum versus mean for graphs of different sizes

A chemist models the total energy of two molecules. Both molecules have atoms with the same learned value 2. Molecule X has 3 atoms and molecule Y has 6 atoms.

  1. With the sum readout, molecule X gives 3×2=63 \times 2 = 6 and molecule Y gives 6×2=126 \times 2 = 12.
  2. With the mean readout, molecule X gives 22 and molecule Y gives 22.
  3. Check: the sum is larger for the bigger molecule, and the mean is the same for both.
  4. A total energy grows with the number of atoms, so a sum fits this target. A quantity such as the average charge per atom does not grow with size, so a mean fits that target.
  5. Choose the readout by asking whether the quantity should scale with the number of nodes.

Interpretation: the readout encodes an assumption about the target. Using the mean for a quantity that really adds up over atoms would make the model blind to molecule size. Try it: take node values 11, 55 and 33. What do the sum, mean and max readouts give? Answer: the sum is 99, the mean is 33 and the max is 55.

Common mistakes and tradeoffs

It is tempting to think a graph readout can be a concatenation of the node vectors in their listed order. In fact concatenation depends on the numbering of the nodes, so two copies of the same graph with different file orders would get different predictions. A readout must be built from operations that ignore order, such as sum, mean or max.

It is tempting to think the mean is always the safer choice. In fact the mean hides the size of the graph, which matters for quantities that add up, such as total energy. The sum keeps size but grows with the graph, which can give very large values on large graphs. Pick the readout from the target, not from habit.

It is tempting to think the max readout captures the whole graph. In fact the max keeps only the largest value in each entry, so it discards most of the information about the other nodes. It is useful when one strong node decides the answer, such as the presence of a single active site.

The readout is a fixed rule in the simple case, which keeps the model easy to analyse. More elaborate readouts learn a weighted combination of nodes, for example with attention, which can be stronger but adds parameters and must still be permutation invariant. Simple sum, mean and max are the right first choice when the graph's size is not the issue.

History

Before graph networks were common, models for whole-graph predictions often used hand-built summaries of the graph, such as counts of small substructures. Learned models needed a way to turn a variable number of node values into a fixed output, and the readout step is the answer to that problem.

Gilmer and colleagues in 2017 described a Message Passing Neural Network forward pass in two phases: a message passing phase over several steps, then a readout phase. They required the readout to be permutation invariant, so the graph's prediction would not depend on node order, and they compared readouts, including a set-based model designed for sets, against a plain sum.

The idea is closely related to Max Pooling in image networks. Pooling collapses a window of values into one, and a graph readout collapses a set of node values into one. The difference is that a graph has no grid, so the operation must work on a set.

Recap

  • A graph readout turns a set of node vectors into one vector for the whole graph.
  • The readout must be permutation invariant, so the order of the nodes does not change the answer.
  • Sum, mean and max are the standard readouts; each one suits a different kind of target.
  • The sum keeps graph size, the mean removes it, and the max keeps only the largest value in each entry.
  • Read next: Message Passing Neural Network, which puts message passing and a readout together into one framework.

Check yourself

QuestionRecall: what does it mean for a graph readout to be permutation invariant?Show answer
It means the output does not change when the nodes are relabelled or listed in a different order. The readout depends only on the set of node vectors, not on how they are arranged.
QuestionComputation: the node values are 4, 0 and 2. What are the sum, the mean and the max readouts?Show answer
The sum is 4+0+2=64 + 0 + 2 = 6. The mean is 6/3=26 / 3 = 2. The max is 44.
QuestionWhy: why is concatenating node vectors in the order they appear in a file not a valid readout?Show answer
Because the order of the nodes in a file is an accident of how the graph was stored, not a property of the graph. Concatenation would give different outputs for the same graph under different numberings, so the prediction would depend on the file format rather than the graph.
QuestionApplied: a model predicts whether a molecule is toxic, which does not depend on how big the molecule is. Would you choose a sum or a mean readout, and why?Show answer
A mean readout, because the target should not grow with the number of atoms. A sum would tie the prediction to molecule size, which is not part of what the model should learn here. In practice, the choice should be checked against the data.

Sources