Give five nodes the same features. A GNN that averages its neighbours, or takes their maximum, gives all five the same vector at every depth: 1 colour. A GNN that sums them, GIN, splits the five into 4 colours by layer 2. That is the most the Weisfeiler–Leman test can find, and no message-passing GNN can beat it.
Press Play to run the layers. Then pick mean, max or sum and play again. Then we build it piece by piece: computation trees, multisets, the blind spots of the mean and the max, the sum, the WL test, GIN and its ceiling.
You need Lecture 4's message and aggregation, a little Python and what a one-hot vector is. We build the rest from zero.
Lecture 6, "Theory of GNNs", of Stanford CS224W, Machine Learning with Graphs (Fall 2026, Jure Leskovec, with guest instructor Charilaos Kanatsoulis), slides. Previous: Lecture 5, GNN Augmentation and Training. Next: Lecture 7, Designing Powerful Graph Encoders.
Illustrative toy on the lecture's graph, with edges 1–2, 1–5, 2–5, 3–4 and 4–5. Every node has the same input (yellow). Each layer keeps the node's own colour and aggregates its neighbours' colours by mean, max or sum.
The update after the aggregation is ideal: a lookup that gives every different input a new colour. So when two nodes merge, the aggregation alone is to blame. Sum with this update is the WL test (dashed line), which is also an ideal GIN.
Chapter 0
Ask which nodes a GNN can tell apart, before asking how to train one
GCN, GraphSAGE and GAT give a node a vector by reading its neighbours. Lecture 4 built them. Now a harder question: are there two nodes that a GNN must give the same vector, whatever the training?
Look at the lecture's graph of five nodes. Every node carries the same feature, so only the edges differ. A model that gives two nodes one vector must also give them one prediction. The expressive power of a GNN is the set of nodes, or graphs, it can tell apart at all.
Pick a pair. Guess whether some GNN could give the two nodes different vectors. The yellow numbers that appear are each node's neighbour count.
The lecture's graph and its three example pairs. Exact: the reasons are counts you can check by eye.
The three pairs grow harder. Node 1 has 2 neighbours and node 5 has 3, so one layer that counts neighbours separates them. Nodes 1 and 4 both have 2 neighbours. But node 1's neighbours have 2 and 3 neighbours of their own, and node 4's have 1 and 3. A second layer can see that.
Nodes 1 and 2 are different. Swap their labels and you get the same graph: they are mirror images. No GNN can separate them, because every layer sees the same thing from each.
This lecture finds the exact line between the second and third kind. It answers two questions. Which GNNs reach that line? And which pairs of different nodes or graphs lie beyond it for every message-passing GNN?
Symptom: nodes in different positions get the same vector, so the model cannot learn to predict different labels for them.
Measure: in the hero, a mean GNN gives all 5 nodes one vector, though they have 1, 2 or 3 neighbours.
Fix: an aggregation that keeps the count of each neighbour (Chapter 4), as GIN does (Chapter 6).
Chapter 1
Unfold each node's neighbourhood into the rooted tree that a K-layer GNN reads
Follow one node through two layers. In layer 2, node 1 reads its neighbours 2 and 5. Each of those built its own vector in layer 1 from its neighbours. Node 2 read 1 and 5; node 5 read 1, 2 and 4. Draw that and you get a tree with node 1 at the root.
This is node 1's computation graph: its neighbourhood unfolded into a rooted tree, one level per layer. The lecture also calls it the rooted subtree. A K-layer GNN computes from the leaves up to the root, and it reads nothing else.
Each tree is one node's view after K layers. Hide the IDs to see what the GNN sees: only shape and features. Press Aggregate to send the messages up.
Exact trees of the lecture graph. Two trees count as the same when they match as shapes, with children in any order: that is all a GNN can see.
Two facts follow. First, the same tree gives the same vector. The GNN does not see node IDs, so equal trees with equal features are equal inputs. Second, different trees give different vectors only if no layer throws information away.
With the device's numbers: at depth 1, nodes 1 and 4 both have a root and 2 children, 3 nodes each. At depth 2, node 1's tree has 8 nodes and node 4's has 7, because node 4's neighbour 3 has only one neighbour. The trees differ, so a GNN can tell 1 and 4 apart with 2 layers.
Nodes 1 and 2 have the same tree at every depth: 3, 8 and 19 nodes at depths 1, 2 and 3. More layers do not help them.
Symptom: adding layers never separates two nodes.
Measure: nodes 1 and 2 have equal trees at depths 1, 2 and 3 (3, 8 and 19 nodes each).
Fix: none inside message passing. Add features that differ between them, as Chapter 7 shows.
Chapter 2
Treat each aggregation as a function on multisets and ask that it keep every input distinct
A GNN fills the tree in level by level, from the leaves to the root. So the GNN keeps different trees apart if every layer keeps different inputs apart. What does one layer receive?
It receives the node's own vector and its neighbours' vectors. The neighbours have no order, and two of them can hold the same vector. That collection is a multiset: a set in which an element can appear more than once. {a, a, b} and {a, b} are different multisets.
Call node v's vector after layer k hv(k), and its neighbours N(v). One layer combines the neighbours' multiset with an aggregation AGG, then updates with a function φ.
h-v at layer k equals phi of two things: h-v at layer k minus one, and AGG of the multiset of h-u at layer k minus one, for u in N of v.
Collect the old vectors h-u of v's neighbours, repeats and all. Reduce them to one with AGG. Give phi that and v's own old vector h-v. The output is v's new vector.
The lecture graph, every input 1. In layer 1, node 1 reads the multiset {1, 1} and node 5 reads {1, 1, 1}. A sum gives 2 and 3, so the nodes split. A mean gives 1 and 1, so they merge.
A function is injective when different inputs always give different outputs. If AGG and φ are injective at every layer, different trees stay different all the way to the root. If one layer merges two inputs, no later layer can split them again.
Left: every multiset of 1 to 3 neighbours with features a or b. Right: what the aggregation gives each one. A red frame marks an output that two neighbourhoods share. Tap a row to trace it.
Exact. Feature a is the one-hot vector (1, 0) and b is (0, 1). The mean gives the share of each, the max gives which appear, the sum gives the count of each.
Symptom: a merge at one layer is never undone.
Measure: in the hero, the mean merges all 5 nodes at layer 1. Layers 2 and 3 still show 1 colour.
Fix: an injective aggregation at every layer. The sum gives 9 outputs for these 9 neighbourhoods.
Chapter 3
Find the multisets that GCN's mean and GraphSAGE's max send to one output
Write each neighbour's feature as a one-hot vector f(x). Feature a is (1, 0, 0), b is (0, 1, 0) and c is (0, 0, 1). Call the multiset of neighbour features X and its size |X|. GCN's aggregation is the mean.
Mean of X equals one over the size of X, times the sum over x in X of f of x.
Add the one-hot vectors f of x of all neighbours, then divide by the size of X. Each entry is the share of the neighbours with that feature.
X = {a, a, b, b}: the sum is (2, 2, 0) and the size is 4, so the mean is (0.5, 0.5, 0). X = {a, b} gives (1, 1, 0) / 2 = (0.5, 0.5, 0). Same output.
Dividing by |X| erases the count. Xu et al. (ICLR 2019) prove the exact blind spot: a mean separates two multisets only if their shares differ. So every mean merges {a, b} with {a, a, b, b}.
GraphSAGE's pooling version takes the largest entry in each position, written max(X).
Max of X, entry i, equals the max over x in X of f of x, entry i.
For each position i, look at entry i of every f of x and keep the largest. With one-hot vectors, entry i is 1 if any neighbour has feature i.
X = {a, b} gives (1, 1, 0). X = {a, a, b} gives (1, 1, 0) too. The max sees which features appear, never how many.
Each row of squares is one neighbour's one-hot vector (yellow). Below them, the three aggregates (purple). Start from a preset, or add and remove neighbours.
Exact, with one-hot features as on the lecture slides. Up to 6 neighbours on each side.
The two blind spots differ. The mean fails when shares match; the max fails when the set of features matches. Neither contains the other: {a, b} against {a, a, b} fools the max but not the mean.
Symptom: on graphs whose nodes carry no features, a mean GNN cannot learn anything.
Measure: on REDDIT-BINARY every node has the same feature. Xu et al. report 50.0% test accuracy for both mean models, a coin flip, against 92.4% for GIN.
Fix: a sum, which keeps counts (Chapter 4).
Chapter 4
Map each feature to a one-hot or a digit first, then add, and every multiset keeps its own total
Drop the division and add the one-hot vectors. {a, a, b} gives (2, 1, 0) and {a, b} gives (1, 1, 0). Each entry is a count, and two multisets with the same counts are the same multiset. Nothing is lost.
The order of the two steps matters. Adding raw values fails: {1, 3} and {2, 2} both sum to 4. The fix is a feature map f applied first, so that sums of different multisets cannot meet. Then any function of the multiset can be read off the total by a second function φ.
g of X equals phi of the sum over x in X of f of x.
Turn every element x into f of x. Add them all. Give the total to phi. If we choose f well, each multiset X has its own total, so phi can compute anything about X.
Take f(a) = 1, f(b) = 10, f(c) = 100. X = {a, a, b} sums to 12; X = {a, b} sums to 11. Read the digits right to left: two a's and one b, then one a and one b.
Xu et al. prove this as Lemma 5: for features from a countable set and multisets of bounded size, such an f exists. Their f is a digit trick, f(x) = N−Z(x), where Z numbers the features and N exceeds every multiset size. Deep Sets (Zaheer et al., 2017) did the same for sets.
Add neighbours with features a, b and c. With raw values, other multisets reach the same sum. With digits, each count gets its own place. Push one count to 10 and watch it carry.
Exact. We checked all 220 multisets of up to 9 elements over a, b and c: with digits, no two share a total. Up to 12 elements here, so the carry can show.
The carry is why Lemma 5 needs bounded size: ten a's sum to 10, the same as one b. Pick N above the largest degree and nothing carries. One-hot vectors give each feature its own position instead of its own digit.
The Code Lab runs all three aggregations over six pairs of multisets. You write the max and the sum.
Symptom: a sum GNN still merges different neighbourhoods.
Measure: raw features 1 and 3 against 2 and 2 both sum to 4. So do 1 and −1 against 0 and 0, a pair from Lecture 4, which both sum to 0.
Fix: apply a feature map f before the sum. In GIN an MLP learns it.
Chapter 5
Refine node colours by hashing each node's colour with the multiset of its neighbours' colours
Two graphs are isomorphic when relabelling the nodes of one gives the other. Nobody knows a polynomial-time algorithm that decides it, or a proof that it is NP-complete. In 1968 Weisfeiler and Leman (often spelled Lehman) described a fast test that often decides it.
The test is colour refinement. Call node v's colour after round k c(k)(v). Every node starts with the same colour, or with its feature. Each round, a node's new colour is a code for its own colour together with the multiset of its neighbours' colours. A HASH function turns each different pair into a different new colour.
c at round k plus one of v equals HASH of c at round k of v, and the multiset of c at round k of u, for u in N of v.
Write each node's colour c of v next to its neighbours' colours, sorted. Give every different line a new colour with HASH. Do this for all nodes at once, then repeat.
The lecture graph, all colour 1. Round 1: node 3 reads "1, 1" and gets 2. Nodes 1, 2 and 4 read "1, 1 1" and get 3. Node 5 reads "1, 1 1 1" and gets 4: one colour per neighbour count.
To compare two graphs, run the rounds on both with one shared hash table. Stop when the colours stop splitting. If the two graphs ever hold different counts of some colour, they are not isomorphic. If the counts stay equal, the test cannot decide: the graphs may still differ.
Both graphs share one hash table, shown under them as "own colour, neighbour colours → new colour". Step one round at a time and compare the colour counts.
Exact 1-WL on both graphs. New colours take the next free numbers, in sorted order of their lines, as on the lecture slides.
A GNN layer maps a node's vector and the multiset of its neighbours' vectors to a new vector: one WL round with vectors for colours. So a GNN separates two graphs only if WL does (Xu et al., Lemma 2). Morris et al. (AAAI 2019) proved the same bound independently.
The Code Lab writes the one line that is the round: the signature.
Symptom: WL reports equal colour counts for two graphs that differ.
Measure: two triangles against a hexagon: every node of both has 2 neighbours, so all 6 keep one colour at every round.
Fix: nothing inside WL. Chapter 7 adds features WL cannot compute.
Chapter 6
Replace the hash with an MLP over the node, scaled by one plus epsilon, plus the sum of its neighbours
WL's HASH is injective on a pair: the node's colour and its neighbours' multiset. Xu et al.'s Corollary 6 builds that from a sum: scale the node's own f term by 1 + ε and add the neighbours' terms. For every irrational ε, each pair gets its own total.
An MLP can approximate any continuous function (Hornik et al., 1989), so it can play f and φ. The Graph Isomorphism Network (GIN) uses one per layer, MLP(k).
h-v at layer k equals MLP at layer k of: one plus epsilon at layer k, times h-v at layer k minus one, plus the sum over u in N of v of h-u at layer k minus one.
Scale v's old vector h-v by one plus epsilon. Add every neighbour's old h-u. Pass the total through the MLP to get the new h-v.
With made-up numbers: h-v = (0.5, 0.3), neighbours (1.0, 0.2) and (0.7, 0.8), epsilon 0. The input to the MLP is (0.5, 0.3) + (1.7, 1.0) = (2.2, 1.3). With epsilon 0.1 it is (2.25, 1.33).
Why the 1 + ε? With ε = 0 the node's own term blends into the neighbours' sum. On an edge between features a and b, node a computes a + b and node b computes b + a. WL separates them by their own colours; GIN-0's sum does not.
A dark ring marks each node's own feature; its neighbours sit below. The bars are the MLP input. Slide ε until P and Q meet.
Exact, with a = (1, 0) and b = (0, 1). With one-hot features, a collision needs a whole-number 1 + ε.
On nine benchmarks, GIN-ε (learned) and GIN-0 fit the training sets equally well, and GIN-0 tested slightly but consistently better. The runs used 5 layers, 2-layer MLPs and batch normalization.
GIN's graph vector hG sums the node vectors after every layer and joins the sums.
h-G equals CONCAT, over k from zero to K, of the sum over v in G of h-v at layer k.
After each layer k, add up the vectors h-v of all nodes. Join the K + 1 sums into one long vector h-G.
The lecture graph, colours as one-hot vectors: layer 0 sums to (5) and layer 1 to (1, 3, 1). So h-G = (5 | 1, 3, 1), the WL colour counts.
In the Code Lab, you swap the mean for the sum on the mean's failure case.
Symptom: two different (node, neighbours) pairs give the MLP one input.
Measure: an edge a–b with ε = 0: both ends compute (1, 1).
Fix: make 1 + ε not whole: at ε = 0.5 the ends get (1.5, 1) and (1, 1.5).
Chapter 7
Meet the graphs that WL, and so every message-passing GNN, maps to one answer
GIN reaches the WL ceiling, and the ceiling is real. In a regular graph, every node has the same number of neighbours. Start every node with one colour, and round 1 gives every node the same pair: its colour and k copies of it. Nothing ever splits.
So two regular graphs of the same size and degree look identical to WL. Two triangles and a hexagon both have 6 nodes, each with 2 neighbours. One holds 2 triangles; the other holds none and forms one ring. Every GNN of this lecture gives them the same graph vector. The lecture puts it plainly: WL cannot count cycles or cliques.
Each pair differs, yet WL's colour counts match. Give the nodes a feature that WL cannot compute for itself and see which pairs split.
Exact 1-WL, run to a stable colouring, checked against NetworkX. We draw decalin and bicyclopentyl as carbon skeletons: 10 carbons and 11 bonds each, without hydrogens.
With numbers: K3,3 and the triangular prism both have 6 nodes of degree 3, so WL gives all 12 nodes one colour. The prism holds 2 triangles; K3,3 holds none. Count triangles first, use the count as the starting colour, and the pair splits at round 0.
Chemistry has the same trap. Decalin is two fused six-carbon rings; bicyclopentyl is two five-carbon rings joined by a bond. Both have two carbons with 3 bonds and eight with 2, and WL keeps their counts equal at every round. A triangle count does not help, since neither has a triangle. The size of the smallest ring through each atom, 6 against 5, splits them.
Higher-order tests go further. The k-WL test colours groups of k nodes instead of single nodes, and Morris et al. built k-GNNs on it, which are strictly more powerful. The price is a graph on about nk groups. Lecture 7 covers cheaper routes: position-aware and identity-aware GNNs, and structural features.
Symptom: a molecule model gives decalin and bicyclopentyl one vector.
Measure: 10 carbons and 11 bonds each, the same degrees, the same WL counts at every round.
Fix: add a ring-size or cycle feature, or a more expressive encoder (Lecture 7).
Chapter 8
Draw two graphs and find which aggregation tells them apart, and at which layer
Now put every piece in one place. Two graphs sit side by side. Choose the aggregation, the starting feature and the number of layers. Both graphs share one colour table, so one shade means one vector in either graph.
Pick a pair, or tap two nodes of one graph to add or remove the edge between them. The table under the graphs gives the first layer at which each aggregation separates them.
Illustrative toy with an ideal update after each aggregation, as in the hero. Sum is exact 1-WL; the mean keeps each colour's share; the max keeps which colours appear. We compare the graphs by their colour counts, a sum readout.
Three things to find. First, on triangle + tail against square + leaf, the sum separates the graphs at layer 2. Mean and max never do: with one shared feature, every neighbour looks alike to them.
Second, switch on Degree as feature. Now the mean and the max separate the same pair at layer 1, because the degrees already hold the counts they cannot take. Xu et al. point out that with degree features a mean can, in principle, recover a sum.
Third, two triangles against a hexagon stay together for every method, every layer and both features: every degree is 2. Then press Blank and draw a pair of your own that the sum separates and the mean does not.
Chapter 9
Keep the ranking, the numbers, the code and the road to Lecture 7
A K-layer GNN computes a node's vector from its depth-K computation tree, so it can separate two nodes only if their trees differ. It separates all such pairs only if every aggregation is injective on multisets. The mean keeps shares and the max keeps which features appear; a sum after a one-hot-like f keeps counts. The WL test is that injective process with a lookup table, and it bounds every message-passing GNN. GIN reaches the bound with an MLP over (1 + ε) times the node plus the sum of its neighbours. Regular graphs and cycle counts lie beyond it.
| Idea | Rule | This lesson's number |
|---|---|---|
| Computation tree | K layers read the depth-K tree | nodes 1 and 4: 3 and 3 nodes at depth 1, 8 and 7 at depth 2 |
| Layer | φ(hv, AGG {{hu}}) | layer 1: node 1 reads {1, 1}, node 5 reads {1, 1, 1} |
| Mean | (1/|X|) Σ f(x): shares | {a,a,b,b} and {a,b}: (0.5, 0.5, 0) both |
| Max | max f(x)i: which appear | {a,b} and {a,a,b}: (1, 1, 0) both |
| Sum | φ(Σ f(x)): counts | digits: {a,a,b} = 12, {a,b} = 11; 9 of 9 neighbourhoods kept apart |
| WL round | HASH(c(v), {{c(u)}}) | lecture graph: 1, 3, 4, 4 colours after rounds 0 to 3 |
| GIN | MLP((1 + ε) hv + Σ hu) | (2.2, 1.3) into the MLP; edge a–b collides at ε = 0 |
| Readout | CONCAT of Σv hv(k) | (5 | 1, 3, 1) after one layer |
| Ceiling | regular graphs, cycle counts | 2 triangles vs hexagon; decalin vs bicyclopentyl |
| Hero | same feature, 2 layers | mean 1, max 1, sum 4, WL 4 colours |
| Model | Aggregation | Keeps | Paper result (Xu et al., Table 1) |
|---|---|---|---|
| GCN (Kipf and Welling, 2017) | mean, then linear and ReLU | the share of each feature | REDDIT-BINARY 50.0% |
| GraphSAGE pool (Hamilton et al., 2017) | MLP, then element-wise max | which features appear | not run on REDDIT; MUTAG 85.1% |
| GIN-0 (Xu et al., 2019) | sum, then MLP | the count of each feature: as much as WL | REDDIT-BINARY 92.4%; MUTAG 89.4% |
| k-GNN (Morris et al., 2019) | over groups of k nodes | more than WL | costs about nk groups |
The hero in plain Python: three aggregations with an ideal hash. We ran it; it prints the hero's colour counts.
python from collections import Counter edges = [(1, 2), (1, 5), (2, 5), (3, 4), (4, 5)] # the lecture's five-node graph nbrs = {v: [] for v in range(1, 6)} for a, b in edges: nbrs[a].append(b); nbrs[b].append(a) def summary(own, colours, how): c = Counter(colours) if how == "sum": return (own, tuple(sorted(c.items()))) # counts: the multiset if how == "max": return (own, tuple(sorted(c))) # which colours appear return (own, tuple(sorted((k, round(n / len(colours), 9)) for k, n in c.items()))) # shares def refine(how, layers=3): colour = {v: 1 for v in nbrs} # same feature everywhere for k in range(1, layers + 1): sig = {v: summary(colour[v], [colour[u] for u in nbrs[v]], how) for v in nbrs} table = {s: i for i, s in enumerate(sorted(set(sig.values())))} # the hash colour = {v: table[sig[v]] for v in nbrs} print(how, "layer", k, "colours:", len(set(colour.values()))) for how in ("mean", "max", "sum"): refine(how) # mean: 1, 1, 1 max: 1, 1, 1 sum: 3, 4, 4
For real work, PyTorch Geometric ships the layer as GINConv(nn, eps=0.0, train_eps=False), where nn is the MLP. Set train_eps=True for GIN-ε.
| Lectures | Topic |
|---|---|
| 3 to 5 | Graph neural networks, a general perspective and augmentation and training: the layers this lesson ranks |
| 6 | Theory of GNNs: this lesson |
| 7 | Designing powerful graph encoders: features and models past the WL ceiling |
| 8 | Graph Transformers: attention over all nodes, with positional encodings |
Now teach it: press Present or Teach. Explain the computation tree, why the mean and the max merge neighbourhoods, how a one-hot sum counts, and why GIN equals WL. Then go back to the five nodes, pick the mean, and say how many colours you will see before you press Play.