Foundations to Frontier
Tests
E. Deep Learning › E14 Graph Neural Networks

Graph representation and message passing

Many kinds of data are graphs: molecules, road and power networks, meshes, social networks, robot scene graphs. Graph neural networks process them by message passing. In each layer, every node collects messages from its neighbours, aggregates them with a permutation-invariant function (sum, mean or max) and updates its own state. After k layers, each node has seen its k-hop neighbourhood. This page covers adjacency matrices and edge lists, the message-passing framework, permutation equivariance and oversmoothing. In the exercises you implement a message-passing layer and check that it is permutation-equivariant. You then run parameter-free mean aggregation on a two-community graph with weak node features. Two rounds lifted community accuracy from 0.80 to 0.96. After 32 rounds, node features had shrunk to about 1/10,000 of their original spread. At 128 rounds, a perturbation of 0.01 erased the signal entirely (accuracy 0.45): oversmoothing.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Nodes that talk to their neighbours

A graph is a set of nodes joined by edges: atoms joined by bonds, cities joined by roads. A graph neural network gives every node a vector of numbers. In each layer, every node listens to its neighbours, combines what it hears, and updates its vector. After two layers a node knows about its neighbours’ neighbours; after k layers, everything within k steps. The demo lights up how far information has travelled.

Graph neural network lab
01234567891011
After 2 rounds, node 0 has received information from 6 of 12 nodes: its 2-hop neighbourhood. A GNN with k layers cannot see further.
Why engineers care

Power grids, communication networks, molecules, meshes for physics simulation and multi-robot teams are all graphs. GNNs learn on them directly, whatever their size or shape.

📝 Quick check (Level 0)
0/3 answered
  1. 1. In message passing, each node combines information from…
  2. 2. After k layers a node has seen…
  3. 3. Molecules can be modelled as graphs of…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Representation:
    • an adjacency matrix A (n × n, A_ij = 1 if i and j are joined), or an edge list;
    • node features X (n × d), and optionally edge features.
  • Degrees and edges:
    • degree d_i = Σ_j A_ij, and Σ_i d_i = 2m for m undirected edges (the handshake lemma);
    • density m / (n(n − 1)/2): 3 edges on 4 nodes give 0.5.
  • Message passing (one layer): h_i’ = UPDATE(h_i, AGG_(j ∈ N(i)) MSG(h_i, h_j, e_ij)).
    • AGG must not depend on neighbour order: sum, mean or max;
    • the mean of node 1 (value 2) and its neighbours (values 1, 3) is 2.
  • Receptive field: (A^k)_ij counts walks of length k, so k layers reach exactly the nodes within k hops. On a 10-node path, node 5 sees 5 nodes after 2 layers.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Degrees 1, 2, 2, 1 imply how many edges?
  2. 2. Which aggregation is NOT permutation-invariant?
  3. 3. (A²)_ij counts…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Message passing in code

Exercise 1.

  • Mean and sum aggregation gave the expected values on a 4-node path.
  • Permuting the nodes permuted the layer’s outputs identically (equivariance).

Practice.

  • PyTorch Geometric (MessagePassing, GCNConv, SAGEConv, GATConv) and DGL store graphs as sparse edge indices and use scatter operations to aggregate.
  • Batch many small graphs by joining them into one disconnected graph.
  • Add self-loops, normalisation and residual connections. Keep depth modest (2–4 layers) unless you counter oversmoothing.
from torch_geometric.nn import MessagePassing
class MeanLayer(MessagePassing):
    def __init__(self, d_in, d_out):
        super().__init__(aggr="mean"); self.lin_self = nn.Linear(d_in, d_out); self.lin_nb = nn.Linear(d_in, d_out)
    def forward(self, x, edge_index):
        return F.relu(self.lin_self(x) + self.propagate(edge_index, x=self.lin_nb(x)))   # mean over neighbours
🐍 Exercise 1: a message-passing layer
Build an adjacency matrix, implement a message-passing layer with mean or sum aggregation, and check permutation equivariance.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Graph libraries aggregate with…
  2. 2. Many small graphs are batched as…
  3. 3. Typical GNN depth:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Permutation symmetry.

  • Node numbering is arbitrary, so a graph function should not depend on it.
  • Message-passing layers are permutation-equivariant (relabelling nodes relabels outputs).
  • Graph-level readouts (sum or mean over nodes) are permutation-invariant.

The framework.

  • Gilmer and colleagues (2017) showed that GCN, GraphSAGE, GAT, gated GNNs and interaction networks are all instances of message passing, differing only in their message, aggregation and update functions.

Oversmoothing.

  • Mean aggregation is a step of a random walk. Repeated, it converges to the walk’s stationary state, where node features depend only on the connected component (Li and colleagues, 2018; Oono and Suzuki, 2020).
  • Community differences decay like λ₂^k, where λ₂ is the second-largest eigenvalue of the aggregation matrix. Deep GNNs therefore lose node-level information unless they use residual connections, normalisation or attention.
📝 Quick check (Level 3)
0/3 answered
  1. 1. A graph-level sum readout is…
  2. 2. Repeated mean aggregation converges to…
  3. 3. The MPNN framework was proposed by…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How deep should message passing go?

History.

  • F. Scarselli et al., “The graph neural network model” (IEEE TNN 2009), introduced recurrent message passing to a fixed point.
  • J. Gilmer, S. Schoenholz, P. Riley, O. Vinyals and G. Dahl, “Neural message passing for quantum chemistry” (ICML 2017), unified the field.
  • Q. Li, Z. Han and X.-M. Wu (AAAI 2018) identified oversmoothing.

Exercise 2.

  • Graph: a two-community random graph (100 nodes, within-community edge probability 0.1, between 0.01).
  • Features: 8-dimensional noise with a weak community shift of ±0.25.
  • Method: k rounds of parameter-free mean aggregation (self plus neighbours).
  • Measurements:
    • the receptive field (fraction of node pairs connected within k hops);
    • leave-one-out nearest-centroid accuracy for the community, with and without a tiny perturbation (standard deviation 0.01) added after aggregation;
    • the features’ spread across nodes.
rounds k receptive field accuracy accuracy, perturbed feature spread
0 0.010 0.80 0.80 8.19
1 0.063 0.94 0.94 1.74
2 0.277 0.96 0.96 0.78
4 0.941 0.97 0.97 0.33
16 1.000 0.97 0.97 0.022
32 1.000 0.97 0.96 0.00088
64 1.000 0.97 0.53 0.00000
128 1.000 0.97 0.45 0.00000
  • A few rounds help a lot: pooling neighbours’ noisy evidence lifts accuracy from 0.80 to 0.96 in 2 rounds and 0.97 in 4, by which point the receptive field covers 94% of node pairs.
  • Many rounds collapse the features: the spread falls by a factor of about 10⁴ by 32 rounds and to zero (at the printed precision) by 64.
  • The signal then becomes fragile: in exact arithmetic the community difference survives as a vanishing component (decaying like 0.904^k, the aggregation matrix’s second eigenvalue). A perturbation of only 0.01 overwhelms it, giving chance accuracy at 64–128 rounds. With seed 1 the pattern was the same (0.74 → 0.98, then 0.57 and 0.45 perturbed).
🐍 Exercise 2 (research): how deep should message passing go?
Run k rounds of mean aggregation on a two-community graph and track receptive field, accuracy, robustness and feature spread.

Message passing in AI, robotics and aerospace

  • Infrastructure: fault detection and load forecasting on power grids and communication networks.
  • Multi-robot systems: robots exchange messages with neighbours to coordinate, a natural fit for GNN policies.
  • Physics and design: simulation on meshes (MeshGraphNets) and property prediction for molecules and materials.
  • Design lesson: match depth to the range of interactions you need; beyond that, extra layers mostly blur node information.

References

  1. J. Gilmer, S. S. Schoenholz, P. F. Riley, O. Vinyals and G. E. Dahl (2017). “Neural message passing for quantum chemistry.” Proc. ICML.
  2. F. Scarselli et al. (2009). “The graph neural network model.” IEEE Transactions on Neural Networks 20.
  3. Q. Li, Z. Han and X.-M. Wu (2018). “Deeper insights into graph convolutional networks for semi-supervised learning.” Proc. AAAI.
  4. W. L. Hamilton (2020). Graph Representation Learning. Morgan and Claypool.