A General Perspective on GNNs

Every GNN layer sends messages along the edges and aggregates them, so each layer averages in more of the graph. On one fixed survey of the karate club, the answers alone place 26 of 34 members. Two layers of averaging place 33. Thirty-two layers place 17, because every member ends with nearly the same value. A shortcut back to the input holds 30 at any depth.

Press Play and add layers one at a time. Watch the colours sharpen, then fade to one shade. Then switch on a skip connection and play again. Then we build it piece by piece: the message, the aggregation, GCN, GraphSAGE, GAT, depth and skips.

You need Lecture 3's GCN layer, a little Python and what a weighted average is. We build the rest from zero.

Lecture 4, "A general perspective on GNNs", of Stanford CS224W, Machine Learning with Graphs (Fall 2026, Jure Leskovec, with guest instructor Charilaos Kanatsoulis), slides. Previous: Lecture 3, Graph Neural Networks. Next: Lecture 5, GNN Augmentation and Training.

Stack layers on the club

2 layers

Illustrative toy on real ties: Zachary's 34 members and 78 friendships (via NetworkX). Each member's input is one survey answer, +1 for Mr. Hi's side or −1 for the president's, wrong for 8 members (the series' fixed survey). The layers learn nothing: W = 1 and no σ.

Each layer replaces a member's value by the mean over itself and its friends, the lecture's GCN layer. "Skip" adds the previous layer and halves the sum to keep the scale. "Shortcut" mixes 0.9 of the mean with 0.1 of the survey, as in APPNP. The sign is the predicted side.

Averages over 1,000 random surveys at the same 30% error: 23.8 alone, 27.6 at two layers, 17.7 at 32 layers, and 25.9 for the shortcut at 32.

Chapter 0

Three Layers, One Recipe

Put GCN, GraphSAGE and GAT side by side at one node and see that only the weights change

Lecture 3 built one layer, the GCN. Since then, papers have proposed hundreds more: GraphSAGE, GAT, GIN and many others. Each one arrives with its own formula. How do you compare them, or design your own?

The lecture's answer comes from You, Ying and Leskovec (NeurIPS 2020). Every GNN layer does two things. Each neighbour computes a message from its vector. Then the node aggregates the messages into one new vector. Layers differ only in how they make the message and how they combine the messages.

Compare three layers at one node

Each node sits at its two feature values. Blue edges carry messages to v; their width is the weight. The purple dot is v's new vector. Pick a layer, then tap a neighbour.

Illustrative toy with W = I (the identity) and no σ, so each layer returns a weighted average of feature points. GraphSAGE uses made-up weights: half for v, half for the mean of its neighbours. GAT uses the attention vector a = (1, −1, 0.5, −0.5) of the Chapter 4 Code Lab, with v in its own neighbourhood as in the paper.

All three compute a weighted sum of vectors. GCN gives v and each neighbour the same weight. GraphSAGE gives v a slot of its own. GAT computes a different weight for each neighbour from the features.

Around the layer sit three more choices. Layer connectivity says how layers connect: stacked, or with skip connections. Graph augmentation turns the input graph into the computation graph. The learning objective says what the model learns from.

With the message and the aggregation, these five make the design space. You et al. defined 315,000 designs in it and tested them on 32 tasks. The best design changed from task to task.

With the device's numbers: v holds (1.0, 0.5). GCN averages v and its four neighbours and lands at (0.60, 0.60). GAT gives C and v the most weight, 0.248 each, and B the least, 0.136. It lands at (0.68, 0.55).

Symptom: an off-topic neighbour pulls a node as hard as a close one.

Measure: in the device, B sits 0.95 away from v and A only 0.14 away, yet GCN gives both 0.20.

Fix: keep the node's own vector apart (GraphSAGE, Chapter 3), or let the content set the weights (GAT, Chapter 4).

The road

  1. Message and aggregation: the two steps inside every layer.
  2. GCN: weights fixed by degree.
  3. GraphSAGE: the node's own vector kept apart.
  4. GAT: weights computed from the features.
  5. Heads and modules: several heads, normalization, dropout.
  6. Over-smoothing: why deep stacks blur every node together.
  7. Skip connections: a mixture of shallow and deep paths.
  8. Graph augmentation: change the graph, not the layer.
  9. Showcase: design a GNN on the club.
  10. Field guide: the numbers, the code and the road ahead.
What do GCN, GraphSAGE and GAT have in common?

Chapter 1

A Message, Then an Aggregation

Turn each neighbour's vector into a message, then combine the messages with a function that ignores order

Take one layer apart. Call node v's vector after layer l hv(l); layer 0 holds the input features. Each neighbour u turns its vector from the layer before into a message mu(l). The simplest message multiplies by a weight matrix W(l) that every node shares.

Say it aloud

m-u at layer l equals W at layer l times h-u at layer l minus one.

mu(l)
m-u at layer l the message neighbour u sends in layer l.
W(l)
W at layer l the layer's weight matrix. Every node uses the same one.
hu(l−1)
h-u at layer l minus one u's vector from the layer before; at layer 1 this is u's input features.
In plain words

Every neighbour u multiplies its old vector h-u by the shared matrix W. The result is the message m-u it sends to v.

With numbers

With made-up numbers and one number per node: W = 0.5, and v's three neighbours hold 0.2, 0.6 and 1.0. Their messages are 0.1, 0.3 and 0.5.

Next, an aggregation function AGG combines the messages of v's neighbours N(v): a sum, a mean or a max. So that v does not forget itself, the lecture adds v's own old vector times a second matrix B(l). A nonlinearity σ, such as ReLU, ends the layer.

Say it aloud

h-v at layer l equals sigma of: AGG over u in N of v of m-u at layer l, plus B at layer l times h-v at layer l minus one.

hv(l)
h-v at layer l node v's new vector.
σ
sigma the nonlinearity, here ReLU: negative entries become 0.
AGGu∈N(v)
AGG over u in N of v one function over the set of v's neighbours: sum, mean or max. It must ignore their order.
mu(l)
m-u at layer l neighbour u's message from the line above.
B(l)
B at layer l a second shared matrix, for v's own vector.
hv(l−1)
h-v at layer l minus one v's own vector from the layer before.
In plain words

Combine the neighbours' messages m-u with AGG. Add v's own message, B times h-v. Apply sigma. The result is v's new vector h-v.

With numbers

Messages 0.1, 0.3 and 0.5 from above; B = 1 and v holds 0.4. The mean is 0.3, so h-v = ReLU(0.3 + 0.4) = 0.7. The sum gives ReLU(0.9 + 0.4) = 1.3, and the max gives ReLU(0.5 + 0.4) = 0.9.

AGG receives a set, not a list. A node has no first neighbour, so the result must not depend on the order of the messages. Sum, mean and max all pass that test. An LSTM that reads the neighbours in a row does not.

Find what an aggregator cannot see

Nodes a and b receive different sets of messages (blue). Pick an aggregator and a pair of neighbourhoods. A red frame means the aggregator gives both nodes the same output. Shuffle to move the messages around.

Illustrative toy: one number per message, W = 1 and no B, so the output is AGG of the messages alone.

Each aggregator has a blind spot. The mean cannot count identical friends. The max ignores everyone below the top. The sum keeps the count. Lecture 6 turns this into a theorem.

Symptom: two nodes with different neighbourhoods get the same vector.

Measure: one neighbour sending 1 and two neighbours sending 1 both give a mean of 1.00 and a max of 1.00. Only the sum differs: 1 against 2.

Fix: use a sum where counts matter. In You et al.'s study, the sum was the most successful aggregator.

Why must AGG ignore the order of the messages?

Chapter 2

GCN Weighs by Degree

Read Lecture 3's layer as a message divided by the degree and a plain sum

The lecture rewrites GCN in the new terms. Treat every node as its own neighbour (a self-edge), so N(v) includes v. The message from u is W(l) times hu, divided by v's neighbour count |N(v)|. The aggregation is a plain sum, then σ.

Say it aloud

h-v at layer l equals sigma of the sum over u in N of v of W at layer l times h-u at layer l minus one, divided by the size of N of v.

hv(l)
h-v at layer l node v's new vector.
σ
sigma the nonlinearity.
Σu∈N(v)
the sum over u in N of v the aggregation: add one message per neighbour, v included.
W(l)
W at layer l the one shared weight matrix. There is no separate B: v's own vector arrives through its self-edge.
hu(l−1)
h-u at layer l minus one the vector u sends.
|N(v)|
the size of N of v v's neighbour count, v itself included. Dividing turns the sum into a mean.
In plain words

Each node u in N of v, v included, sends W times its vector h-u, divided by the size of N of v. Node v adds the messages and applies sigma. Every input gets the same weight, one over the size of N of v.

With numbers

From the device, with W = 1 and no sigma. Node 2 has five inputs: itself, 0, 1, 3 and its bridge friend 4. They hold 1, 1, 1, 1 and −1. So h-2 = (1 + 1 + 1 + 1 − 1) / 5 = 0.60.

Kipf and Welling's paper divides by √(d̃u d̃v) instead, where d̃ is a degree counting the self-edge. Lecture 3 derives that version. Both weights come from the graph alone. A weight depends on degree, never on what the neighbour says.

Read the weights a node uses

Two groups of four friends meet at one bridge, edge 2–4. Inputs are +1 for group A and −1 for group B (yellow). Tap a node to see its weights (blue) and its value after one layer (purple). Then switch the rule.

Exact arithmetic on the Code Lab's graph, with W = 1 and no σ. A node's fill shows its value after one layer: warm for group A, pink for group B.

The Code Lab builds Kipf and Welling's matrix for this graph. You write one line.

Symptom: a node drifts toward a group it does not belong to.

Measure: node 2's value falls from 1.00 to 0.60 after one layer, because its bridge friend weighs 0.20, the same as each friend in its own group.

Fix: let the layer learn each weight from the content, as GAT does in Chapter 4.

In GCN's message W h-u / |N(v)|, what decides how much neighbour u counts?

Chapter 3

GraphSAGE Keeps the Node Apart

Aggregate the neighbours first, then place the node's own vector beside the summary

Hamilton, Ying and Leskovec built GraphSAGE (NeurIPS 2017) for graphs that grow, and for graphs the model never saw. Its layer has two stages. First, aggregate the neighbours into one summary vector. Second, concatenate v's own vector and the summary, that is, stack them into one longer vector, and transform it with W.

Say it aloud

h-v at layer l equals sigma of W at layer l times the concatenation of h-v at layer l minus one and AGG over u in N of v of h-u at layer l minus one.

hv(l)
h-v at layer l node v's new vector.
σ
sigma the nonlinearity.
W(l)
W at layer l the shared matrix. It reads a vector twice as long, so it has one block for v and one for the summary.
CONCAT
concat stack two vectors end to end: d numbers and d numbers make 2d.
hv(l−1)
h-v at layer l minus one v's own old vector, kept whole.
AGGu∈N(v) hu(l−1)
AGG over u in N of v of h-u the neighbours' summary: a mean, a pool or an LSTM, over neighbours only.
In plain words

Summarize v's neighbours with AGG. Stack v's own old vector h-v next to the summary. Multiply by W and apply sigma.

With numbers

With made-up numbers, one number per node and no sigma: W = (0.5, 0.5), v holds 1 and its 16 friends each hold 0.4. The mean is 0.4, so h-v = 0.5 × 1 + 0.5 × 0.4 = 0.70.

Why stack and not add v to the average? In GCN, v is one input among |N(v)|. Member 1 of the club has 16 friends, so it keeps 1/17 = 0.059 of its own vector. GraphSAGE gives v its own block of W, so v keeps a fixed share at any degree. Split W into its two blocks and you get Lecture 3's layer: B times hv plus W times the mean. The paper calls the concatenation a simple form of skip connection.

Keep twins apart

Twins t1 and t2 share every friend, but t1 answers +1 and t2 answers −1 (yellow). Their friends all hold 0.4. Add friends and watch the gap between the twins' new values (purple).

16 friends

Illustrative toy, one number per node, no σ. GraphSAGE uses W = (0.5, 0.5): half for the node, half for the mean of its friends.

The paper's results bear this out. On protein graphs the model never saw in training, the mean aggregator with concatenation scored a micro-F1 of 0.598, against 0.500 for the same model without it (Table 1). On Reddit it scored 0.950 against 0.930.

Three more parts complete GraphSAGE. AGG can be a mean, a pool (an element-wise max over σ(Wpool hu + b)), or an LSTM over the neighbours in a random order. After each layer, GraphSAGE scales every hv to length 1. And each layer samples a fixed number of neighbours, 25 and then 10 in the paper, as Lecture 3 showed.

Symptom: two nodes with different features but the same friends get almost the same vector.

Measure: twins with 16 shared friends end 0.118 apart under the GCN mean, but 1.00 apart under GraphSAGE.

Fix: concatenate the node's own vector, so that its weight does not shrink with its degree.

A node has 99 neighbours. Under the GCN mean it keeps 1/100 of its own vector. What does GraphSAGE change?

Chapter 4

GAT Learns the Weights

Score every neighbour from both vectors, then turn the scores into weights that add up to one

GCN and GraphSAGE fix the weight of neighbour u in v's sum, αvu, from the graph. Veličković et al. (ICLR 2018) let the network compute it. The graph attention network (GAT) scores each pair (v, u) from their vectors.

First, the raw score evu. Transform both vectors with the shared W. Stack them, take the dot product with a learned vector a, and apply LeakyReLU, which keeps 0.2 of a negative input.

Say it aloud

e-v-u equals LeakyReLU of a-transpose times W h-v concatenated with W h-u.

evu
e-v-u the raw score: how much v wants u's message. One number.
LeakyReLU
leaky relu keeps a positive number; multiplies a negative one by 0.2.
aT
a-transpose the learned attention vector, 2d long. Its dot product turns the stacked pair into one number.
W hv
W h-v v's vector after the shared matrix.
||
concatenated with stack the two vectors end to end.
W hu
W h-u neighbour u's vector after the same matrix.
In plain words

Multiply v's vector and u's vector by W. Stack the two results. Take the dot product with a, then apply LeakyReLU. That number is e-v-u.

With numbers

With made-up numbers: v = (1.0, 0.5), A = (0.9, 0.6), B = (0.1, 0.8), W = I and a = (1, −1, 0.5, −0.5). Then e-v-A = 1.0 − 0.5 + 0.45 − 0.30 = 0.65, and e-v-B = 1.0 − 0.5 + 0.05 − 0.40 = 0.15.

A softmax puts the scores on one scale: exponentiate each and divide by the total. The paper counts v as its own neighbour.

Say it aloud

alpha-v-u equals e to the e-v-u, divided by the sum over k in N of v of e to the e-v-k.

αvu
alpha-v-u the attention weight: u's share of v's sum, between 0 and 1.
exp(evu)
e to the e-v-u the score made positive. A larger score grows fast.
Σk∈N(v)
the sum over k in N of v add one term for every neighbour k of v.
exp(evk)
e to the e-v-k each neighbour's positive score. Dividing by their total makes the weights add up to 1.
In plain words

Raise e to every score e-v-k. Divide u's term by their total: that is alpha-v-u. The layer then sums the messages with these shares: h-v = σ(Σ alpha-v-u W h-u).

With numbers

The two scores above: e0.65 = 1.916 and e0.15 = 1.162, total 3.078. So alpha-v-A = 0.622 and alpha-v-B = 0.378. A's message counts 1.6 times as much as B's.

Move v and watch the attention

Edge width and the bars show each weight αvu. Tap the plot or drag the sliders to move v. Then try GATv2.

1.00
0.50

Illustrative toy with v in its own neighbourhood. GAT uses W = I and a = (1, −1, 0.5, −0.5). GATv2 uses made-up weights that score closeness: e = −2 times the city-block distance between v and u.

Move v anywhere. Under GAT the weights change, but the neighbour order never does: C, A, D, B. The score is a part from v plus a part from u. LeakyReLU and softmax keep order, so the part from u alone ranks the neighbours. Brody, Alon and Yahav (ICLR 2022) call this static attention. Their GATv2 applies a after the nonlinearity, so the ranking can depend on v.

On Cora, GAT scored 83.0% against GCN's 81.5%. On unseen protein graphs it scored 0.973 micro-F1, against 0.934 with constant attention. The Code Lab computes the softmax.

Symptom: every node ranks its neighbours in the same order, whatever the node's own features.

Measure: wherever v sits, GAT ranks C, A, D, B. GATv2 ranks the neighbour closest to v first.

Fix: GATv2's scoring, or several heads: each head can learn a different ranking (Chapter 5).

What does the softmax in GAT guarantee?

Chapter 5

Heads and Modules Inside a Layer

Run several attention heads side by side, then wrap the layer in normalization, dropout and an activation

One attention head learns one ranking. GAT runs K heads side by side. Head k has its own matrix Wk and its own attention vector ak, so it computes its own weights αvuk. Hidden layers concatenate the K outputs:

hv = CONCAT over k = 1 … K of σ( Σu∈N(v) αvuk Wk hu )

The last layer averages the heads instead, because it must output one score per class. On Cora the paper used 8 heads of 8 features each, 64 in all, then one head for the 7 classes.

Heads also steady the weights. One head can lean hard on a single neighbour, by chance or early in training. The average of K heads leans less.

Average the heads

Each small group is one head's weights over A, B, C and D. The large group is the average of the first K heads. Add heads and watch the largest weight fall.

K = 1

Illustrative toy: the four-neighbour star of the Code Lab, W = I, v left out of its own neighbourhood. Each head's attention vector is a random draw (normal, spread 1.5, seed 4); we train nothing. Over 20,000 random heads, one head's weight swings by 0.10 on average; the mean of 8 heads swings by 0.036.

Heads cost few parameters. Split 64 output features over 8 heads: each Wk is 64 × 8, so the matrices hold 4,096 weights. The 8 attention vectors of length 16 add 128. That is 4,224 in all, against 4,096 for a GCN layer of the same width.

The lecture's suggested layer also wraps the message in modules from the rest of deep learning. You et al. tested each one across their tasks:

Symptom: one head's weights pile onto a single neighbour.

Measure: in the device, head 1 puts 0.531 on A. The average of 8 heads puts at most 0.260 on any neighbour.

Fix: use several heads, and concatenate or average them.

How does GAT combine its heads in a hidden layer, and in the output layer?

Chapter 6

Stacking Layers Smooths Everything

Add layers and each node reads more of the graph, until every node reads the same graph and holds the same value

Stack L layers and node v reads its receptive field: every node within L hops. Each new layer makes two nodes' receptive fields overlap more. On the club, members 1 and 34 share 4 members at one hop, 16 at two hops and 33 of 34 at three.

Watch two receptive fields merge

Rings mark the two chosen members. Grey dots fill the members that both receptive fields share; open grey rings mark members only one of them reads. Tap a member to move one of the two. Then add layers.

L = 1

Exact counts on the club (its longest shortest path is 5 hops). The values use the hero's layer: the mean over a member and its friends, applied L times to the survey.

Nodes that read the same inputs end with the same vector. This is over-smoothing: all node vectors converge to one value, and the model can no longer tell the nodes apart. See it in the hero's layer, which has no weights. Collect the inputs in a vector x. Call the averaging matrix P: row v holds 1/d̃v for v and for each friend, where d̃v is v's degree plus its self-edge. L layers apply P to x L times.

Say it aloud

P to the L times x tends to c times the all-ones vector, where c is the sum over v of d-tilde-v times x-v, divided by the sum over v of d-tilde-v.

PL
P to the L the averaging matrix applied L times: L layers with no weights.
x
x the inputs, one number per node: here the survey answers.
→
tends to gets as close as you like as L grows.
c
c the one value every node ends with.
1
the all-ones vector a 1 for every node: every node gets the same c.
Σv d̃v xv
the sum over v of d-tilde-v times x-v each input weighted by its node's degree plus one.
Σv d̃v
the sum over v of d-tilde-v the total weight, so c is a weighted mean.
In plain words

Average again and again, and every node drifts to the same number c. Busy nodes pull harder: c weights each input x-v by its node's d-tilde-v.

With numbers

On the club, the d-tilde-v add up to 190: 156 friendship ends plus 34 self-edges. The survey answers, weighted by d-tilde-v, add up to −26. So c = −26 / 190 = −0.137. That is the president's side, so a deep stack places only his 17 members right. At 32 layers every value already lies between −0.152 and −0.104.

Real layers have weights and a nonlinearity, and training can resist for a while. The overlap stays. The lecture draws two lessons. First, count the hops your task needs, such as the diameter (5 on the club), and use a few more layers, not many more. Second, add power without depth: an MLP inside each layer, or MLP layers before and after the GNN layers.

The Code Lab watches a six-feature field smooth on the two-group graph of Chapter 2.

Symptom: accuracy falls as you add layers.

Measure: on the hero's survey, 33 of 34 right at two layers, 22 at 16 layers and 17 at 32.

Fix: use fewer layers, or the skip connections of Chapter 7.

Why do deep stacks of averaging layers over-smooth?

Chapter 7

Skip Connections Mix Depths

Add each layer's input to its output, and the network becomes a mixture of shallow and deep paths

Early layers often tell nodes apart better than late ones. A skip connection adds a layer's input back to its output, as in ResNets (He et al., 2015). Call the layer's usual output F(h): for GCN, σ of the degree-weighted sum of Chapter 2.

Say it aloud

h-v at layer l equals F of h-v at layer l minus one, plus h-v at layer l minus one.

hv(l)
h-v at layer l node v's new vector.
F
F the layer as before: messages, aggregation, σ.
hv(l−1)
h-v at layer l minus one the layer's input, which F reads through v's neighbourhood.
+ hv(l−1)
plus h-v at layer l minus one the skip: the same input, added around the layer.
In plain words

Run the layer F on the old vectors. Add each node's old vector h-v to its result. A layer that has nothing useful to add can learn to output almost zero and pass h-v through.

With numbers

Stack 3 such layers. Each one offers two routes, through F or around it, so there are 2 × 2 × 2 = 8 paths. One path skips every layer (depth 0), 3 pass through one layer, 3 through two, and 1 through all three. The average path depth is 1.5, not 3.

So N skips give 2N paths, and the network acts as a mixture of shallow and deep GNNs (Veit et al., 2016). The device draws the share of each depth.

Count the paths by depth

Each bar is the share of the output that arrives through paths of that depth (blue). Pick a connection, then add layers. Watch where the mass sits.

L = 3

Exact path shares when F is the hero's averaging layer. Residual (F + h, halved) weights depth k by C(L, k)/2L. The shortcut weights it by 0.1 × 0.9k, with 0.9L on depth L. Jumping knowledge here takes the mean of all L + 1 layer outputs. Scores use the hero's survey.

In the toy, N skips weight depth k by C(N, k)/2N, so the mixture centres on depth N/2. A 32-layer residual stack acts like a 16-layer plain one: both place 22 of 34 in the hero. Skips delay over-smoothing; they do not stop it.

Two other routes keep shallow views for good. Jumping knowledge (Xu et al., 2018) sends every layer's output straight to the end and combines them by concatenation or max. The final layer can then pick its depth per node. A shortcut to the input mixes a little of x into every layer; APPNP (Gasteiger et al., 2019) uses 0.1. In the hero, that shortcut holds 30 of 34 at any depth.

Symptom: a deep residual GNN still blurs nodes together.

Measure: the hero's skip stack peaks at 33 of 34 at five layers, then falls to 22 at 32 layers and 17 at 64.

Fix: a shortcut to the input, or jumping knowledge to the last layer.

A GNN has 10 residual skip connections. In the averaging toy, which path depth carries the most weight?

Chapter 8

Change the Graph, Not the Layer

Edit the input graph into a better computation graph before any message moves

The raw input graph need not be the graph the GNN computes on. The lecture's fourth design choice, graph augmentation, edits it first. Lecture 5 treats it in depth. There are three common moves:

Add a virtual node

Numbers show each member's distance in hops from the ringed member. Tap a member, then switch on the virtual node. The chart below compares the hero's stacked layers with and without it.

Exact distances on the club. The virtual node starts at value 0 and joins the averaging like any member. Scores use the hero's survey and its stacked averaging layer.

A virtual node is a shortcut for messages, and that cuts both ways. The club's longest shortest path drops from 5 hops to 2, and the average distance from 2.41 to 1.86 hops. A two-layer GNN now reaches every member. But every receptive field covers the club by layer 2, so averaging also smooths sooner: the toy falls to 17 of 34 by layer 7, instead of layer 19.

Symptom: a node needs facts from far away, but more layers over-smooth.

Measure: members 17 and 24 sit 5 hops apart, so a two-layer GNN never lets one hear the other.

Fix: a virtual node puts them 2 hops apart. Then keep the layer count low: with the virtual node the toy peaks at 32 of 34 at two layers.

What does a virtual node joined to every node do to a GNN?

Chapter 9

Design a GNN on the Club

Pick the aggregation, the connections, the depth and the graph, then test the design on fresh surveys

Now put the design space in your hands. Each knob is one of the lecture's choices: the layer (Chapters 2 and 3), the connections (Chapter 7), the depth (Chapter 6) and the computation graph (Chapter 8). The layers still learn nothing, so you see what the structure alone does.

Design and test a GNN

Pick a layer and a connection, set the depth, then raise the survey noise. The solid line is this survey; the dashed line is the average over 200 fresh surveys at the same noise.

2 layers
30%

Illustrative toy on real ties, no learned weights. The first survey at 30% is the hero's; "New survey" draws a fresh one at the chosen error (seeded). GraphSAGE here is 0.5 × own value + 0.5 × the mean of the friends. Residual halves F + h; the shortcut is 0.9 × F + 0.1 × x; jumping knowledge takes the mean of all layer outputs. Averages use 200 surveys drawn with fixed seeds.

Three things to find. First, a plain stack is best shallow: on average it peaks at 2 to 4 layers for every layer type. By 16 layers it scores below the survey alone.

Second, the residual, the shortcut and jumping knowledge move the best depth deeper and flatten the curve. Their average peaks are no higher: 27 to 28 of 34 at 30% error. Third, at 45% error the best design gains only about 1.5 members over the survey alone. Structure can only spread what the inputs hold.

One number to check on the hero's survey: GraphSAGE's half-self layer peaks at 32 of 34 at four layers. At 16 layers it still places 27, where the GCN mean has fallen to 22.

With stacked layers, the dashed line (200 surveys) peaks at 2 to 4 layers for every layer type. What does that say?

Chapter 10

Field Guide

Keep the recipe, the numbers, the code and the road to Lecture 5

A GNN layer computes a message from each neighbour and aggregates the messages with an order-free function. GCN weights each input by degree. GraphSAGE concatenates the node's own vector with a neighbour summary. GAT computes the weights from the features with a softmax, though its ranking of neighbours ignores the node itself; GATv2 fixes that.

Stacking layers grows each receptive field until the fields overlap and the vectors smooth into one. Skip connections mix shallow and deep paths, and a shortcut to the input or jumping knowledge keeps the shallow views for good. Graph augmentation edits the computation graph.

IdeaRuleThis lesson's number
Messagemu = W huW = 0.5: messages 0.1, 0.3, 0.5
Aggregationσ(AGG mu + B hv)mean 0.7, sum 1.3, max 0.9
GCNΣ W hu / |N(v)|bridge node 2: 0.60
GraphSAGEW · CONCAT(hv, AGG hu)twins 1.00 apart, not 0.118
GATsoftmax of LeakyReLU(aT[Whv || Whu])0.622 and 0.378
Headsconcatenate, average at the end4,224 weights vs 4,096
Over-smoothingPLx → c 1c = −26/190 = −0.137
SkipsF(h) + h; 2N paths32 residual layers act like 16: 22 of 34
Herostacked, this survey26 alone, 33 at 2 layers, 17 at 32; shortcut 30
Virtual nodeone node joined to alldiameter 5 → 2; collapse at layer 7, not 19
LayerWeight on neighbour uThe node itselfPaper result
GCN (Kipf and Welling, 2017)1/√(d̃ud̃v), from the graphone input among the restCora 81.5%
GraphSAGE (Hamilton et al., 2017)1/|N(v)| in the mean; or pool, LSTMconcatenatedunseen proteins 0.598 vs 0.500 without concat
GAT (Veličković et al., 2018)softmax of a learned score; static rankingin its own neighbourhoodCora 83.0%; proteins 0.973 vs 0.934 constant
GATv2 (Brody et al., 2022)a after LeakyReLU; ranking can depend on vin its own neighbourhoodstrictly more expressive attention
Skip, JK, APPNPany layer aboveadded back: previous layer, all layers, or the inputhero at 32 layers: residual 22, JK mean 27, shortcut 30

The whole toy in NumPy: the averaging matrix, three ways to connect layers, and the score. We ran it; it prints the hero's numbers.

python
import networkx as nx, numpy as np
G = nx.karate_club_graph(); n = 34
A = nx.to_numpy_array(G, nodelist=range(n), weight=None)
y = np.array([1 if G.nodes[v]["club"] == "Mr. Hi" else -1 for v in range(n)])
x = np.array([1,-1,1,1,1,-1,1,1,1,1,-1,1,1,-1,-1,-1,1,-1,1,1,-1,1,-1,1,
              -1,-1,-1,-1,-1,-1,-1,-1,-1,-1], float)   # the survey: 26 of 34 right
At = A + np.eye(n)                                   # self-edges
P = At / At.sum(1, keepdims=True)                   # GCN mean: 1/|N(v)|, W = 1, no sigma
steps = {"stacked":  lambda h: P @ h,
         "residual": lambda h: 0.5 * (P @ h + h),           # F(h) + h, halved
         "shortcut": lambda h: 0.9 * (P @ h) + 0.1 * x}     # APPNP-style
for name, step in steps.items():
    h, right = x.copy(), []
    for L in range(33):
        right.append(int((np.sign(h) == y).sum())); h = step(h)
    print(name, right[0], right[2], right[16], right[32])
# stacked 26 33 22 17 / residual 26 28 29 22 / shortcut 26 30 30 30

For real work, PyTorch Geometric ships each layer of this lesson: GCNConv, SAGEConv, GATConv (with heads=K) and GATv2Conv, each taking x and edge_index.

The road through CS224W, Fall 2026

LecturesTopic
1 to 3Introduction, node embeddings and graph neural networks: the GCN layer this lesson generalizes
4A general perspective on GNNs: this lesson
5GNN augmentation and training: graph augmentation in depth, objectives, evaluation
6 to 8Theory of GNNs, powerful graph encoders, graph Transformers: which aggregators see the most

Keep going

Read next

The habit. When you meet a new GNN layer, ask three questions: what is the message, what is the aggregation, and what happens to the node itself? Then ask how deep it can go before every node looks the same.
In one sentence, what is a GNN layer in the lecture's general view?

Now teach it: press Present or Teach. Explain the message and the aggregation, what GCN, GraphSAGE and GAT each do with the weights and the node itself, and why deep stacks over-smooth. Then go back to the club, slide to 32 layers, and say what the shortcut changes before you press it.

A General Perspective on GNNs · Stanford CS224W, Lecture 4
Previous: Graph Neural Networks · Next: GNN Augmentation and Training · Course site · Back to Gleams