Part III · Ch. 18 — The Graph Network (GNN)

Part III · Chapter 18 of 29

The Graph Network (GNN)

Learn by passing messages between connected nodes


What shape does it eat?

Suppose the important structure is who is connected to whom, rather than where a pixel sits or when a measurement arrived. Can we train a model that uses those relationships directly? We’ll begin with the same four-node contact star used in the sister book, then inspect a model trained on a recorded social graph.

A graph describes entities connected by relationships. A node is an entity; an edge is a connection between nodes. An adjacency matrix records which pairs are connected. Each node can also carry a feature vector. The graph drawing and these arrays describe the same connectivity, but the drawing’s horizontal and vertical positions need not be features supplied to the model.

Draw the exact hand star beside its adjacency and mean-neighbor matrix; a second panel reads the actual E7 edge list with train/validation node outlines. Do not use chemistry imagery.
An edge list supplies structure that a plain feature table omits.

E7 uses the karate-club graph’s recorded edge list with identity node features: each node starts with its own coordinate marked. The task predicts a class per node. Training uses some labels, while other labels are held out. The graph structure and features remain visible. This is transductive evaluation, testing unseen labels on an observed graph. It is a different question from classifying nodes in an entirely new graph.

Let edges choose the neighbors

message passing gathers neighbor values, aggregates them, then updates a node. aggregation is a rule such as sum, mean, or max. permutation invariance means an aggregate remains unchanged when we reorder its inputs. Adding the neighbor values 1,2,3 gives the same total as adding 3,1,2. permutation equivariance means node outputs follow the same reordering as the corresponding input nodes.

Three identical star layouts with values (0,1,2,3), (2,1,2,3), (4,3,4,5), and the readout 16. Keep each old node value visible while showing the simultaneous update.
The same simultaneous neighbor-mean rule reaches farther on each round.

Those properties let us change arbitrary node numbering without changing the represented problem. We must move adjacency rows and columns together with the corresponding features. Relabeling features alone would assign them to different entities. Dragging a node on the page changes only its display position. A model based on the given adjacency still gathers from the same neighbors after the drawing moves.

The same contact star, every update

The hand star has center value 0 and leaf values 1,2,3. Its center connects to each leaf, with no edges between leaves. Our update keeps a node’s old value and adds the mean of its neighbors. Crucially, every node reads the old values from the same round. We do not let a leaf consume a center value that was already updated a moment earlier in the calculation.

Two simultaneous rounds

The first center is 0+(1+2+3)/3=2. Each leaf sees the old center zero, so the leaves remain 1+0=1, 2+0=2, and 3+0=3. The complete new vector is (2,1,2,3). On the second round, the center becomes 2+(1+2+3)/3=4. The leaves now read the old center 2, giving 3,4,5. The final vector is (4,3,4,5), and its sum readout is 4+3+4+5=16.

The same update as a matrix

Let P have first row [0,1/3,1/3,1/3], with each leaf row [1,0,0,0]. Multiplying P by the feature column gathers each node’s neighbor mean. Add the identity matrix I to keep each old value as well. Its first row applied to the initial feature column gives 0+(1+2+3)/3=2, exactly as the picture did. An empty neighborhood contributes zero in the widget, preserving that node’s own value.

For the first formula, bold H collects the node features, and bold I and P perform the hand keep-plus-mean update. E7 uses a different gather rule. Its A is the original adjacency, I adds self-connections, and d counts each node’s connections after that addition. A hat marks the normalized adjacency. Bold W and b are the learned weights and biases. We write all three operations so the toy and trained rules remain distinguishable:

$$\mathbf{H}_{\mathrm{new}}=(\mathbf{I}+\mathbf{P})\mathbf{H}$$

In words: keep the node’s old feature and add its neighbors’ mean.

$$\hat A_{ij}=\frac{A_{ij}+I_{ij}}{\sqrt{d_i d_j}},\qquad d_i=\sum_j(A_{ij}+I_{ij})$$

In words: add self-connections, then scale each connection by the square root of the two endpoint degrees.

$$\mathbf{H}^{\prime}=\mathrm{ReLU}(\hat{\mathbf{A}}\mathbf{H}\mathbf{W}^{\top}+\mathbf{b})$$

In words: gather normalized neighbor features, transform them with the learned weight matrix, add bias, and apply the activation. The superscript top transposes the weight matrix: it swaps rows and columns without changing entries. Stored weights are output rows by input columns, as in chapter 3; the transpose lets these row-shaped node features multiply them.

Replay predictions on the observed graph

The first E7 layer maps 34 identity features to four hidden coordinates. Its weights and biases contribute 4×34+4=140 parameters. The second maps four coordinates to two scores, contributing 2×4+2=10. The model therefore has 150 trainable parameters. The normalized adjacency is part of its fixed graph computation rather than another trainable weight table.

E7 first/final class probabilities on one fixed graph layout, truth outline versus prediction fill and separate held-out accuracy/loss curves. Mark errors using actual held-out labels only.
The recorded result predicts held-out labels on a graph whose structure was visible.

At step 300, training loss is 0.000525, validation loss is 0.273955, and validation accuracy is 0.857143. These values come from media/runs/E7.summary.json#/values/final_loss, /values/final_val_loss, and /values/final_val_accuracy, with corresponding run-series pointers at index 300. The difference between training and validation loss is visible alongside the graph. The held-out nodes deserve attention even when most training labels are predicted confidently.

The sister book’s contact-star animation uses the same simultaneous mean-plus-old arithmetic.

Where this shows up when you train

Contact networks and road connectivity offer relationships that can guide a gather rule. A table of unrelated records does not acquire useful edges because a graph model is available. We need a reason why each connection should help answer the task. If distance or direction matters, those quantities must enter the representation explicitly; they are not supplied by dragging the diagram into an attractive layout.

Mean aggregation loses information about how many neighbors contributed: repeated equal values have the same mean. Sum retains that count but can grow with neighborhood size. Max keeps a strongest value while discarding the rest. These are concrete information choices. Before stacking more message rounds, inspect what one round preserves, what it loses, and how far a signal can travel along actual edges. A deeper graph model cannot recover a distinction already erased by its inputs or aggregation rule.

What you now know

  • A graph records entities and their connections.
  • Aggregation ignores neighbor order while preserving who is connected.
  • E7 measures held-out labels on a visible graph.

Where we’re headed

An autoencoder will learn a compact representation by trying to reconstruct its input. Continue the story.