Graph Neural Networks
A graph neural network lets every node update itself from its neighbors: average their vectors, multiply by a shared weight matrix, apply an activation. One layer spreads information one hop, so layers let a node hear from everything within hops.
What the symbols mean
- New state of v
Node v's vector after layer l + 1.
- Neighbor's state
What neighbor u knew after layer l: its message to v.
- Neighborhood
The set of nodes joined to v by an edge; |N(v)| is how many there are (the degree).
- Shared weights
One learned matrix used by every node in the layer.
- Activation
A nonlinearity such as ReLU, applied entry by entry.
What the symbols mean
- Adjacency matrix
Entry (v, u) is 1 when v and u are joined by an edge, 0 otherwise.
- Degree matrix
Diagonal, with each node's degree on the diagonal. D⁻¹A divides row v of A by deg(v), so it averages over neighbors.
- Node states
One row per node: every node's vector, stacked.
Explanation
A graph neural network (GNN) is simply a neural network that runs on a graph. Every node holds a small vector of numbers (what it currently "knows"), and in each layer every node collects its neighbors' vectors, averages them, and passes the average through an ordinary neuron layer. The formula looks heavy, but it's four small pieces: (what neighbor knows right now), the average over (all of 's neighbors), (a weight matrix shared by every node) and (an activation like ReLU). It's the artificial neuron again, fed by a node's neighbors instead of a fixed list of inputs. That's the whole layer.
Why graphs need their own kind of network. Molecules, road maps, social networks and power grids are all graphs (see Graph Basics): dots joined by lines. They come in different sizes and have no natural order: which atom in a molecule is "first"? An ordinary dense layer needs a fixed number of inputs in a fixed order, so it can't take them. Averaging over neighbors solves both problems. It works for a node with 2 neighbors or 200, and it doesn't care what order you list them in, since a mean is the same in any order. Relabel the nodes and every node's result just moves along with it.
Sharing one across all nodes is the same trick as weight sharing in Convolution and Weight Sharing. In fact an image is a grid graph, with each pixel joined to its neighbors, and a convolution is message passing on that grid. Don't mix this up with Networks as Graphs: there, the network itself was a graph. Here, the data is a graph.
The same layer as matrices. Stack every node's vector as a row of . Row of adds up the rows of 's neighbors, since exactly when is a neighbor. Multiplying by divides row by , turning the sum into a mean. So is the averaging matrix: row holds at each neighbor and elsewhere, and its rows add to 1. Then multiply by the weights (on the right, as , because the nodes are rows) and apply . Many GNNs also add self-loops, using so each node averages itself together with its neighbors and doesn't forget what it knew.
Why layers see hops. One layer mixes in only direct neighbors, since is nonzero only when is next to . Two layers apply the averaging twice, and
which is nonzero only if some is a neighbor of and also a neighbor of : a walk of length 2 from to . In general, exactly when there's a walk of length from to , the same fact as " counts walks." The activations between layers don't change which nodes can affect which. So after layers a node's vector depends only on nodes within hops, where hops are the distance from Paths, Connectivity and Shortest Paths. If a fraudulent account sits 3 transfers away from yours, a 2-layer GNN simply can't see it. There's an animation further down that shows the signal spreading one hop per layer.
Too many layers: over-smoothing. Averaging over and over blurs everything together. Keep applying to a connected graph and every node drifts toward the same value, a degree-weighted average of where they started, and the network can no longer tell nodes apart. The next lesson explains why: is exactly the transition matrix of a random walk, and a random walk forgets its starting point. That's why practical GNNs usually stop at 2 to 4 layers.
A common mistake. It's tempting to think one GNN layer lets every node see the whole graph, the way a dense layer sees all of its inputs. It doesn't: one layer moves information exactly one hop. If the answer for a node depends on something 5 hops away, you need at least 5 layers, or some extra mechanism to reach farther.
See it
Worked examples
A star graph has edges 1–2, 2–3 and 2–4. The node features are , , , . One GNN layer uses the mean of the neighbors (no self-loops), and ReLU. Find the new vectors for nodes 2 and 1.
- 1
Start with node 2. Its neighbors are the nodes it shares an edge with: .
On the path graph 1–2–3–4–5, let (no self-loops). Find , and how many layers it takes before node 1 can affect node 5.
- 1
Write down the degrees: nodes 1 and 5 have one neighbor each, and nodes 2, 3, 4 have two.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A node's three neighbors hold the one-number features , and . One GNN layer computes . What is the node's new feature?
A node has degree 4. In its row of , what is the entry for each of its neighbors?
A bank models accounts as nodes and money transfers as edges. A known fraudulent account is 3 transfers away from a customer's account. What is the smallest number of GNN layers that lets the fraud signal reach the customer's node?
A GNN layer maps 64-number node vectors to 32-number vectors with a weight matrix and a bias. How many parameters does it have on a graph with 10,000 nodes?
The 4-cycle 1–2–3–4–1 has adjacency matrix . What is , the number of walks of length 2 from node 1 to node 3?
Why do GNNs combine neighbors with a mean (or sum, or max) instead of concatenating the neighbors' vectors into one long input?
On the path 1–2–3–4 with self-loops, let where . Find .
A team stacks 30 GNN layers on a small connected graph and finds that every node ends up with nearly the same output vector. What's the most likely cause?
The hardest one here. Get it, and you've got this lesson.
On the path 1–2–3 with self-loops, start with features . Each layer computes , where is the mean of over and its neighbors. What is after two layers? Why is it zero after one?
Where it's used
Drug-discovery teams run GNNs on molecules (atoms as nodes, bonds as edges) to predict whether a compound will be toxic or bind to a target before anyone makes it in a lab. Map apps predict travel times with GNNs on road networks, where each road segment hears from the traffic a few segments away. Payment companies flag fraud rings by passing messages across the graph of accounts and transfers, and recommendation systems treat users and items as one big graph.