Message Passing
A graph has nodes joined by edges. Its adjacency matrix has when an edge runs from node to node . In the weighted case its entries are real numbers. An undirected graph has a symmetric . Each node carries a feature vector , and these vectors are the rows of . Edges can carry vectors , and the whole graph can carry a global vector . Graphs differ by direction and by the labels on their edges:
- Finite labels: every edge has a label from a fixed finite set. With directed edges, one weight matrix per label handles this case the way a CNN does.
- Infinite labels: every edge carries a real number or a vector, such as a scalar weight .
- No labels: all edges look alike. An undirected edge counts as two directed edges with the same label.
A graph neural network (GNN) solves problems at three levels:
- Graph level: one output per graph, such as the toxicity of a molecule. It is the counterpart of image classification.
- Node level: one output per node, such as the group of each person in a network. It is the counterpart of semantic segmentation.
- Edge level: one output per pair of nodes, such as whether two people should be linked.
A message-passing layer updates each node from its own vector and the vectors of its neighbors:
The function turns each neighbor into a message, and combines the summed messages with the node’s own vector. Both can be MLPs, and every node uses the same parameters and . The neighbors have no natural order, so any replacement for the sum must also ignore order:
- Sum: keeps the number of neighbors visible in the result.
- Mean: divides the sum by .
- Max or min: takes the largest or smallest value of each channel.
- Norms: for example the root mean square of each channel.
- Pairwise messages: lets each message depend on the receiving node.
- Attention: weights each message by a learned similarity score, normalized with a softmax. A transformer is a GNN of this kind on a complete graph.
With any of these aggregators, each layer is permutation equivariant. Relabeling the nodes relabels the outputs in the same way. A graph-level readout that sums or averages over all nodes is permutation invariant.
After layers, each node has received information from its -hop neighborhood, the nodes within edges of it. The entry counts the walks of length between and . Every node sees the whole graph once reaches the diameter, the largest distance between two nodes. A global node linked to every node cuts any distance to at most .
Not Taught in Lecture
Lecture worked with node vectors only. Edge vectors follow Prince §13.2. The global vector follows the distill.pub article by Sanchez-Lengeling et al. (2021). The walk count comes from Prince §13.2.1.
Intuition
A CNN works because every pixel has the same eight neighbors in the same eight directions. One kernel then fits every position. The convolution can be read as message passing on a directed graph with pixels as nodes. Each pixel receives edges labeled from its neighbors. Its own vector arrives through a ninth label, self. The receiving pixel multiplies each message by the matrix for its label , sums the results and adds a bias :
The figure shows the nine messages that reach one pixel. In this view the empty pixel grid is a graph. An image is a set of vectors placed on that graph.
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\begin{tikzpicture}[x=1.8cm,y=1.8cm,px/.style={circle,draw,thick,minimum size=7mm,inner sep=0pt},msg/.style={-{Stealth[length=2.2mm]},thick,cold}]
\node[px,hot,very thick,minimum size=9mm] (c) at (0,0) {$\mathbf h_0$};
\foreach \x/\y/\n in {-1/1/NW,0/1/N,1/1/NE,-1/0/W,1/0/E,-1/-1/SW,0/-1/S,1/-1/SE} {
\node[px] (p\n) at (\x,\y) {};
\draw[msg] (p\n) -- (c);
\node at (1.38*\x,1.38*\y) {$\mathrm{\n}$};
}
\draw[-{Stealth[length=2.2mm]},thick,hot] (c.-84) to[out=-84,in=-62,looseness=16] (c.-62);
\node[hot,anchor=west] at (0.36,-0.86) {self};
\end{tikzpicture}
\end{document}A general graph has no directions. A person in a social network may have two friends or two hundred, and none of them is “north”. Giving every edge the same dummy label turns the convolution into , with one matrix for all neighbors. A node knows its own vector and its number of neighbors . It also receives the unordered set of neighbor vectors. The node may treat and in special ways. Its output must not depend on the order in which the neighbors are listed. The update meets this rule and still shares its weights across all nodes. Weight sharing, channels, residual connections, dropout and stochastic depth carry over from CNNs to GNNs. Pooling and upsampling need a fixed grid, so they do not carry over.
Properties
(Equivariance From Shared Updates) A layer that applies the same and at every node and aggregates without order is permutation equivariant.
Write the whole layer as , the matrix of updated node vectors. A relabeling of the nodes is a permutation matrix . It reorders the rows of to and turns the adjacency matrix into . Each node keeps the same neighbors under its new index, and the aggregate over them ignores their order. Each output row is therefore the old output row, moved to the new index:
A layer that concatenates neighbor vectors in index order loses this property. A layer with separate weights for each node index loses it too. A fully connected layer on an image has the same problem. A readout that sums the output rows is invariant, because .
(Receptive Field From Powers of A) In an undirected graph, layers link node to node only if lies within edges.
Here is symmetric. A linear layer that sums the neighbors computes , since row of is . In a directed graph, node receives from the nodes with , and the layer computes instead. Stacking such layers gives . Each step of a walk adds one factor of . The entry therefore counts the walks of length from to . A walk may revisit nodes, and a path may not. Some sources call these counts paths, but walks such as are included. With self-loops, the entry of is nonzero exactly when the distance is at most . The figure shows the neighborhoods of node 1 in a five-node graph. Two layers reach node 4, and node 5 needs a third layer. The diameter of this graph is .
\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{grn}{HTML}{2F9E44}
\definecolor{prp}{HTML}{8E6BD6}
\begin{tikzpicture}[x=1.5cm,y=1.5cm,nd/.style={circle,draw,very thick,minimum size=8mm,inner sep=0pt}]
\node[nd,hot] (a) at (0,0.75) {$1$};
\node[nd,cold] (b) at (0,-0.75) {$2$};
\node[nd,cold] (c) at (1.3,0) {$3$};
\node[nd,grn] (d) at (2.6,0) {$4$};
\node[nd,prp] (e) at (3.9,0) {$5$};
\draw[thick] (a) -- (b) (a) -- (c) (b) -- (c) (c) -- (d) (d) -- (e);
\node[hot,anchor=west] at (5,0.9) {target node};
\node[cold,anchor=west] at (5,0.3) {1 hop: layer 1};
\node[grn,anchor=west] at (5,-0.3) {2 hops: layer 2};
\node[prp,anchor=west] at (5,-0.9) {3 hops: layer 3};
\end{tikzpicture}
\end{document}(What Each Aggregator Can Tell Apart) The sum separates neighborhoods that the mean or the max cannot tell apart.
Not Taught in Lecture
The comparison of what each aggregator can tell apart follows Xu et al. (2019), cited in the chapter notes of Prince. The argument for the mean comes from Prince §13.8.3. Lecture listed the aggregators without comparing them.
The mean of equals the mean of , so mean aggregation cannot count neighbors. The max of equals the max of , so max aggregation sees only which values occur. The sum separates both pairs whenever the message is nonzero. A node can also use its degree as a separate input, which restores the count for the mean. The mean helps when features matter more than structure, because its scale does not grow with . The max suits detection, where one strong neighbor should decide the output.
(Oversmoothing) Repeated averaging drives every node of a connected graph toward the same vector.
Each averaging step replaces a node’s vector by a weighted mean of its own vector and its neighbors’ vectors. Differences between neighbors shrink at every step, and deep stacks erase the details that separate nodes. A stack of linear layers is one linear map, so adding linear layers cannot restore these differences. Without edge labels, every neighbor also enters through the same matrix. A deep GNN without edge labels therefore needs nonlinear message or update functions to keep nodes distinct. Nonlinearity alone does not guarantee it, and residual connections help by carrying each node’s earlier vector forward. An example in 6.1.2. Graph Convolutional Networks computes the decay on a small graph.
(Oversquashing) Information from distant nodes passes through fixed-size vectors, and much of it is lost.
The -hop neighborhood can grow exponentially with . In a tree where every node has children, the third hop below a node already holds nodes. All of their content must reach that node through a few vectors of length . Content from far away passes through the most intermediate nodes and is compressed the most. A global node shortens every route to two hops, but then all long-range information passes through one vector. Graph pooling, covered in 6.1.3. Graph Pooling, shrinks the graph step by step, as pooling does in a CNN.
(Normalization Without Batch Statistics) The batch-by-nodes-by-channels block that BatchNorm normalizes does not exist when graphs have different numbers of nodes.
In a CNN, BatchNorm averages each channel over the batch and over all positions. Graphs in a batch have different node counts and no shared positions. LayerNorm still works, because it normalizes over all nodes and channels of one graph. An easier choice normalizes each node’s vector across its channels. Every node then uses the same learned scale and shift , as it uses the same weights. Neither option looks at the other graphs in the batch, so a batch can mix graphs of any size.
(Masked Nodes for Held-Out Data) A node with an unknown value keeps its edges and receives a learnable mask vector in place of its features.
Deleting a held-out node would also delete its edges and break the routes that pass through it. The network instead sets the features of that node to a learned vector . Messages still flow through the node, and the network still predicts its label. Training must show the network this situation so that becomes useful. Each training step therefore masks a random set of labeled nodes and keeps the loss on their predictions. The network learns to predict a node’s label from its neighborhood alone.
(Shortest Paths by Message Passing) A min aggregator turns message passing into the relaxation step of a shortest-path algorithm.
Not Taught in Lecture
This connection comes from a discussion problem, not from lecture or Prince. The relaxation step is standard algorithms material.
Each node holds an estimate of its distance from a source , starting from and elsewhere. One round updates every node at once:
Here is the set of neighbors of , and is the length of edge . Node sends the message , and node aggregates with a minimum. After rounds, every distance is correct whose shortest path uses at most edges. Updating all nodes in synchronous rounds is the Bellman-Ford schedule. Dijkstra’s algorithm, known in search as uniform cost search, relaxes one node at a time in order of distance. The hard minimum passes gradient to only one input. The smooth minimum with passes gradient to every input and approaches the minimum as .
Examples
(Easy) Why the Aggregator Must Ignore Order
A node has two neighbors with scalar features and . One candidate layer concatenates the neighbor features in index order and applies the weights . A second candidate sums the features and multiplies the sum by .
Compute both outputs for the two possible orderings of the neighbors. for the order and for the order . Both orderings describe the same graph, yet the outputs differ. The sum layer gives for either order. The concatenation layer also expects exactly two neighbors, so it cannot run on a node with three. The sum layer works for any number of neighbors with one weight.
The concatenation layer gives
(Medium) Sum, Mean and Max on Three Neighborhoods
In a social network, a chess player has the feature vector and a football player has . Person P has the friends {chess, chess, football}. Person Q has {chess, football}. Person R has {chess, chess, football, football}.
Aggregate each neighborhood with the sum, the mean and the max. Which aggregators confuse which people? for P, for Q and for R, so the sum separates all three. The means are , and . The mean confuses Q and R, because R's friends are Q's friends twice over. The max is for all three people. It records only that both kinds of friend occur. A task that depends on the number of friends needs the sum or an explicit degree input. A task about the mix of friends, such as the share of chess players, can use the mean.
The sums are
(Medium) Counting Walks With Powers of A
Take the five-node graph from the receptive-field figure, with edges , , , and .
Compute , read off the walks from node 1, and explain the diagonal of . Squaring the adjacency matrix gives Row 1 of shows two walks of length from node 1 back to itself, and . Each diagonal entry of equals the degree of the node. One walk, , reaches node 4, and none reaches node 5. Two sum layers therefore carry no information from node 5 to node 1. The cube has , from the walks , and . No path of length joins nodes 1 and 2, since each of these walks repeats a node. The diagonal of is . Each triangle gives each of its corners two closed walks, one in each direction. Nodes 1, 2 and 3 therefore share one triangle.
(Hard) A Convolution That Loses Its Labels
A single-channel layer applies the kernel below by cross-correlation , which responds to vertical edges. It sees two patches with a dark center. Patch has a bright right column, and patch has a bright bottom row:
Write the kernel as label-specific weights and compute both outputs. Then give every edge one shared label and repeat. , , and . Patch sends along the NE, E and SE edges, so the output is . Patch sends along SW, S and SE, so the output is . The layer detects the vertical edge and ignores the horizontal one.
Each kernel entry is the weight for the neighbor in that direction. The weights are
With one shared label, the output is for two scalars and . Both patches have and a neighbor sum of , so both give for every choice of weights. A bright left column gives as well, while the labeled kernel gives . The unlabeled layer measures only how much brightness surrounds the pixel.
See Also
- 6.1.2. Graph Convolutional Networks, a concrete layer built from this update with a normalized adjacency matrix.
- 6.1.3. Graph Pooling, which shrinks graphs to fight oversquashing and to produce graph-level outputs.
- 5.2.1a. Receptive Field, the CNN version of the -hop neighborhood.
- 3.2 Walks, for walks, paths and cycles in graph theory.
- 7.1. Attention, attention as message passing on a complete graph.