TL;DR
Neighbourhood aggregation is the step where a node in a Graph gathers the feature vectors of its neighbours and combines them into one vector. The gathering uses an operation that does not depend on the order of the neighbours, such as a mean, a sum or a maximum. Repeating the step over several layers lets information travel across the graph one edge per layer, so each node's final vector reflects the structure around it. Most graph neural networks are built from this one operation.
Builds on: Graph Neural Network, Vector · Leads to: Graph Convolutional Network, Message Passing Neural Network, GraphSAGE
Where it's used
Predicting the properties of molecules. A molecule is a graph: atoms are nodes and chemical bonds are edges. Each atom starts with a small feature vector, for example its element and how many bonds it has. Each atom then aggregates the features of the atoms bonded to it. After a few rounds, the atom vectors are pooled into one vector for the whole molecule, and a classifier predicts a property such as solubility. The Message Passing Neural Network family of models works this way.
Recommending products. In a graph of users and items, a user node aggregates the items it has bought, and an item node aggregates the users who bought it. Two items bought by many of the same people end up with similar vectors, so a shopper who looks at one can be shown the other.
Very large graphs. GraphSAGE (Hamilton, Ying and Leskovec, 2017) was built for graphs too large to process in full. For each node it samples a fixed number of neighbours, at most 25 per node in its experiments, and aggregates only those. The cost of a training batch then stays fixed, however large the graph grows. The authors compared a mean aggregator, an LSTM aggregator and a max-pooling aggregator. Across their six experimental settings, the pooling aggregator beat the mean aggregator by a margin they called marginal (Wilcoxon p = 0.10), and the LSTM and pooling aggregators did not differ significantly (p = 0.46).
How it works
Take one tiny graph with four nodes, A, B, C and D. The edges are A–B, A–C and A–D, so A is the only node with three neighbours. Each node has one number as its feature: A = 1, B = 2, C = 4 and D = 9. We want to compute a new value for node A.
- Find the neighbours. The neighbours of A are B, C and D. Their feature values are 2, 4 and 9. Check: A has three neighbours, and the list holds three numbers.
- Aggregate them into one number. Combine the three numbers with an operation that ignores their order. We use the mean: (2 + 4 + 9) / 3 = 15 / 3 = 5. Check: if the list came in a different order, say 9, 2, 4, the mean would still be 5.
- Combine the aggregate with A's own feature. A's own value is 1. Multiply it by a weight of 0.5, multiply the aggregate by a weight of 0.5, and add: 0.5 × 1 + 0.5 × 5 = 0.5 + 2.5 = 3. Check: the two weights are fixed numbers here. In a real model they are learned during training.
- Apply a nonlinearity. A ReLU keeps positive numbers and turns negative numbers into zero. ReLU(3) = 3, so the new feature of A is 3. Check: the result is a single number, ready to feed into the next layer.
- Do the same for every node. B has one neighbour, A, so its aggregate is just A's value, 1. Its new value is ReLU(0.5 × 2 + 0.5 × 1) = ReLU(1.5) = 1.5. All nodes update at the same time, using the old values. Check: after one layer, A has seen B, C and D, but not the neighbours of B, C and D.
The diagram shows the same pipeline for any node.
flowchart LR N["Neighbour features"] --> G["Gather the neighbour set"] G --> AG["Aggregate: mean, sum or max"] AG --> C["Combine with own feature"] C --> R["Nonlinearity, such as ReLU"] R --> O["New feature of the node"]
The mean is one choice. On the same list of neighbour values, the sum is 15 and the maximum is 9. The walkthrough uses the mean because it does not grow with the number of neighbours, a point we return to below.
The math
Here is the node being updated, is the set of its neighbours, and is the feature vector of neighbour . In the walkthrough the features are single numbers, so we write without the bold.
Step 1 in symbols: the neighbour set
The neighbours of A form the set , with feature values , and .
Step 2 in symbols: the aggregate
The three common choices are
For node A, , so . Each of the three formulas is unchanged when the terms are reordered, because addition, averaging and taking a maximum all ignore order. That is what permutation invariance means. An LSTM, which reads its inputs one after another, does not have this property. GraphSAGE handles that by feeding the LSTM the neighbours in a random order.
Step 3 in symbols: the update
Here and are learned weights and is the nonlinearity. For node A, with and :
The whole layer at once
Stack all node features as the rows of a matrix . The graph convolutional network of Kipf and Welling writes the layer as
where is the adjacency matrix, is the identity matrix, and is the diagonal matrix of row sums of . The term adds self-connections, so each node keeps its own feature in the sum, as the term does above. The degree factors scale each contribution so that the size of the sum does not grow with the number of neighbours.
Worked examples
- Node X has feature 2. Its two neighbours have features 3 and 7.
- Mean of the neighbours: (3 + 7) / 2 = 5.
- Sum of the neighbours: 3 + 7 = 10.
- Maximum of the neighbours: 7.
- Combine with the mean, using weights 1 and 1: 1 × 2 + 1 × 5 = 7. ReLU(7) = 7.
Interpretation: the sum is twice the mean because there are two neighbours. The mean stays on the same scale as the original features, and the sum does not.
- Node Y has feature −4. Its neighbours have features 1 and 3.
- Mean of the neighbours: (1 + 3) / 2 = 2. Sum: 4.
- Combine the mean with weights 0.5 and 0.5: 0.5 × (−4) + 0.5 × 2 = −2 + 1 = −1.
- Apply ReLU: ReLU(−1) = 0. The node's new feature is 0, because ReLU removes negative values.
- Now give Y two more neighbours, each with feature 2, so the neighbours are 1, 3, 2 and 2.
- Mean: (1 + 3 + 2 + 2) / 4 = 2. The mean is unchanged.
- Sum: 1 + 3 + 2 + 2 = 8. The sum doubled, although the neighbour values are similar.
- With the sum and the same weights: 0.5 × (−4) + 0.5 × 8 = −2 + 4 = 2, and ReLU(2) = 2. The answer now depends on how many neighbours Y has, not only on what they look like.
Interpretation: the mean measures what a typical neighbour looks like. The sum measures the total, which grows with the degree of the node.
Paper P has the topic vector (1, 0). Four papers cite it, with topic vectors (2, 0), (0, 2), (4, 2) and (2, 0). We use the mean aggregator, weights 0.5 and 0.5, and ReLU.
- Mean of the four neighbour vectors, one coordinate at a time. First coordinate: (2 + 0 + 4 + 2) / 4 = 2. Second coordinate: (0 + 2 + 2 + 0) / 4 = 1. So the mean is (2, 1).
- Maximum of the neighbour vectors, coordinate by coordinate, as a check: (4, 2). Max-pooling would give this instead.
- Combine the own vector with the mean: 0.5 × (1, 0) + 0.5 × (2, 1) = (0.5 + 1, 0 + 0.5) = (1.5, 0.5).
- Apply ReLU to each coordinate. Both are already positive, so the new vector is (1.5, 0.5).
Interpretation: the new vector of P keeps its own topic in the first coordinate and moves toward the topics of the papers that cite it. Try it: a node has feature 2, and its neighbours have features 0, 4, 8 and 6. Use the mean, weights 0.5 for the node and 0.25 for the aggregate, and ReLU. What is the new feature of the node? Answer: the mean is (0 + 4 + 8 + 6) / 4 = 4.5. Then 0.5 × 2 + 0.25 × 4.5 = 1 + 1.125 = 2.125. ReLU(2.125) = 2.125.
Common mistakes and tradeoffs
It is tempting to think that a node learns only from its direct neighbours, so the far side of a graph is invisible to it. After one layer that is true. After layers, however, a node's vector depends on every node within edges, because each neighbour's vector already includes that neighbour's own neighbours.
It is tempting to use the sum, because it keeps every neighbour's contribution. In fact the sum grows with the number of neighbours, so a node with many neighbours can receive a much larger input than a node with one. Training then becomes unstable, and high-degree nodes dominate. Use the mean when neighbourhood sizes vary a lot, or normalise the sum by degree as the GCN does.
It is tempting to feed the neighbours into a sequence model such as an LSTM, because it can represent richer patterns. In fact an LSTM depends on the order of its inputs, so the same set of neighbours listed in two orders can give two different vectors. The fix is either a symmetric function, or a random ordering with the model trained on many orderings, which is what GraphSAGE does.
It is tempting to drop a node's own feature and aggregate only its neighbours. Then a node forgets itself after a layer. Most models keep the node's own feature, either as a separate term, as in the walkthrough, or as a self-loop, as in the GCN's .
Stacking many layers is not automatically better. As layers are added, the vectors of nearby nodes tend to become similar, an effect called over-smoothing, and the model loses the ability to tell nodes apart. Use neighbourhood aggregation when the graph's local structure carries the signal and the neighbours have no meaningful order. Use a different design when the graph is so dense that gathering every neighbour is too costly, which is when sampling helps, or when some neighbours should count far more than others, which is when learned attention weights help, as in the graph attention network.
History
Researchers working on graphs in the 2000s wanted neural networks that could take a graph as input, but a neighbourhood has no fixed size and no fixed order, so ordinary networks did not fit. In 2005, Gori, Monfardini and Scarselli proposed a neural model for graph domains. In 2009, Scarselli and co-authors published the graph neural network model in IEEE Transactions on Neural Networks, in which nodes repeatedly exchange information with their neighbours until their states settle.
Kipf and Welling, in 2016 (published at ICLR 2017), showed that a simple layer which averages neighbour features with degree normalisation works well for classifying nodes in citation networks, even with few labels. Hamilton, Ying and Leskovec, in 2017, made the aggregation inductive: it uses only node features and sampled neighbours, so it can embed nodes that were not in the training graph. Gilmer and co-authors, also in 2017, described many of these models as one shared recipe, message passing, which is the framework behind MPNNs.
Recap
- A node gathers the feature vectors of its neighbours.
- An order-free aggregation, the mean, sum or max, turns the set of neighbour vectors into one vector.
- The node combines the aggregate with its own feature using learned weights and a nonlinearity.
- Stacking layers lets information travel one edge further per layer.
- Read next: Graph Convolutional Network for the normalised version used in most textbooks, Message Passing Neural Network for the general recipe, GraphSAGE for sampling at scale, and graph attention networks for learned neighbour weights.
Check yourself
QuestionRecall: what property must an aggregation function have so that the order of a node's neighbours does not matter?Show answer
QuestionComputation: node Z has feature 5. Its neighbours have features 1, 2 and 6. Use the mean, weights 0.5 for Z and 0.5 for the aggregate, and ReLU. What is the new feature of Z?Show answer
QuestionWhy: after two layers, why can a node's vector reflect nodes two edges away, even though each layer only looks at direct neighbours?Show answer
QuestionApplied: a recommender graph links users and items. One popular item has 5000 buyers, while most items have a handful. An engineer chooses the sum as the aggregator. What goes wrong, and how would you fix it?Show answer
Sources
- Hamilton, Ying, Leskovec (2017), "Inductive Representation Learning on Large Graphs": https://arxiv.org/abs/1706.02216
- Kipf and Welling (2016), "Semi-Supervised Classification with Graph Convolutional Networks": https://arxiv.org/abs/1609.02907
- Gilmer et al. (2017), "Neural Message Passing for Quantum Chemistry": https://arxiv.org/abs/1704.01212
- "Graph neural network" (Wikipedia): https://en.wikipedia.org/wiki/Graph_neural_network