Part XXII — Graph Neural Networks & Structured Data · Chapter 5

Predict a molecule's property from its bonds

Hook

Six atoms with the exact same six starting properties can still be six different molecules — a ring, a chain, a star, wired up differently. A model that only looked at atom features and ignored the bonds would predict the exact same thing for all three. Can message passing and attention, wired together, actually tell them apart?

Intuition
01.211.521.232.142.052.3
predicted property = 10.278

Same six atoms, same six starting features, every time — only the bonds change. Watch the predicted property move as you switch structures. Nothing about any single atom changed; only who it's connected to did.

Formalize

This capstone chains the last two chapters' rules into a single two-layer pipeline, then reduces the result to one number:

h(1)=GCN(h(0)),h(2)=GAT(h(1)),y^=vhv(2)h^{(1)} = \text{GCN}(h^{(0)}), \qquad h^{(2)} = \text{GAT}(h^{(1)}), \qquad \hat{y} = \sum_v h^{(2)}_v
  • h(0)h^{(0)} — the atoms' initial input features, before either layer runs.
  • h(1)h^{(1)} — atom features after layer 1, the GCN (Graph Convolutional Network) neighborhood-mean update.
  • h(2)h^{(2)} — atom features after layer 2, the GAT (Graph Attention Network) attention update.
  • y^\hat{y} — the model's final predicted whole-molecule property.
  • vv — index ranging over every atom in the molecule, summed by the readout.
  1. Layer 1: GCN's plain neighborhood mean

    Layer 1 is Chapter 2's plain neighborhood mean — every atom blends with its bonded neighbors, no weighting.

  2. Layer 2: GAT's attention, on top of layer 1

    Layer 2 is last chapter's attention rule, applied on top of layer 1's output, not the raw features — so by the time attention runs, every value already carries a first hop of neighbor information.

  3. Readout: sum atom embeddings into one number

    The final sum — a readout — is the simplest way to turn six per-atom numbers into one whole-molecule prediction: real molecular property predictors do exactly this, summing (or averaging) learned atom embeddings into a single graph-level output.

Play

Three different numbers, from three different arrangements of the exact same six atoms. The ring's prediction is the highest of the three — every atom in the ring's 3-cycle (nodes 0, 1, 2) has at least two neighbors to draw on, where the chain's end atoms have only one.

Worked example

Atom 00 on the ring structure, through both layers:

  1. Layer 1 (GCN): mean over atom 0's neighborhood {0, 1, 2}

    h0(1)=1+2+03=1.0h^{(1)}_0 = \frac{1+2+0}{3} = 1.0 (self 11, neighbors {1,2}\{1,2\} with features 2,02,0). The same mean rule for the rest of the ring:

    • h1(1)=2+1+0+34=1.5h^{(1)}_1 = \frac{2+1+0+3}{4} = 1.5 (self 22, neighbors {0,2,3}\{0,2,3\} with features 1,0,31,0,3)
    • h2(1)=0+1+23=1.0h^{(1)}_2 = \frac{0+1+2}{3} = 1.0 (self 00, neighbors {0,1}\{0,1\} with features 1,21,2)
    • h3(1)=3+2+1+24=2.0h^{(1)}_3 = \frac{3+2+1+2}{4} = 2.0 (self 33, neighbors {1,4,5}\{1,4,5\} with features 2,1,22,1,2)
    • h4(1)=1+32=2.0h^{(1)}_4 = \frac{1+3}{2} = 2.0 (self 11, neighbor {3}\{3\} with feature 33)
    • h5(1)=2+32=2.5h^{(1)}_5 = \frac{2+3}{2} = 2.5 (self 22, neighbor {3}\{3\} with feature 33)

    These are needed next, since layer 2 attends over these values, not the raw ones.

  2. Layer 2 (GAT): attention over the same neighborhood, using layer 1's output

    Scores for atom 00 (neighbors {1,2}\{1,2\}, using layer 1's values):

    • e0,0=LeakyReLU(1.01.0)=0e_{0,0}=\text{LeakyReLU}(1.0-1.0)=0
    • e0,1=LeakyReLU(1.51.0)=0.5e_{0,1}=\text{LeakyReLU}(1.5-1.0)=0.5
    • e0,2=LeakyReLU(1.01.0)=0e_{0,2}=\text{LeakyReLU}(1.0-1.0)=0

    Softmax (subtracting the max, 0.50.5):

    • e00.50.607e^{0-0.5}\approx0.607 (self)
    • e0.50.5=1e^{0.5-0.5}=1 (atom 11)
    • e00.50.607e^{0-0.5}\approx0.607 (atom 22)

    Sum 2.213\approx2.213. Normalized weights:

    • 0.607/2.2130.2740.607/2.213\approx0.274
    • 1/2.2130.4521/2.213\approx0.452
    • 0.607/2.2130.2740.607/2.213\approx0.274

    Atom 11 dominates, since it's the only neighbor layer 1 pushed higher than atom 00 itself.

  3. Atom 0's final embedding

    h0(2)0.274(1.0)+0.452(1.5)+0.274(1.0)1.226h^{(2)}_0 \approx 0.274(1.0) + 0.452(1.5) + 0.274(1.0) \approx 1.226.

  4. The same two layers, for the ring's other five atoms

    Running the identical GAT step (scores → softmax → weighted sum) over each atom's own layer-1 neighborhood:

    • Atom 11 (neighbors {0,2,3}\{0,2,3\}, self h1(1)=1.5h^{(1)}_1=1.5): scores (0,0.1,0.1,0.5)(0,-0.1,-0.1,0.5) for (self, 00, 22, 33); softmax weights (0.224,0.203,0.203,0.370)\approx(0.224,0.203,0.203,0.370); h1(2)0.224(1.5)+0.203(1.0)+0.203(1.0)+0.370(2.0)1.482h^{(2)}_1\approx0.224(1.5)+0.203(1.0)+0.203(1.0)+0.370(2.0)\approx1.482
    • Atom 22 (neighbors {0,1}\{0,1\}, self h2(1)=1.0h^{(1)}_2=1.0): the same self/neighbor values as atom 00 (one neighbor at 1.01.0, one at 1.51.5), so h2(2)1.226h^{(2)}_2\approx1.226 — identical to atom 00
    • Atom 33 (neighbors {1,4,5}\{1,4,5\}, self h3(1)=2.0h^{(1)}_3=2.0): scores (0,0.1,0,0.5)(0,-0.1,0,0.5) for (self, 11, 44, 55); softmax weights (0.220,0.199,0.220,0.362)\approx(0.220,0.199,0.220,0.362); h3(2)0.220(2.0)+0.199(1.5)+0.220(2.0)+0.362(2.5)2.082h^{(2)}_3\approx0.220(2.0)+0.199(1.5)+0.220(2.0)+0.362(2.5)\approx2.082
    • Atom 44 (neighbor {3}\{3\} only, self h4(1)=2.0=h3(1)h^{(1)}_4=2.0=h^{(1)}_3): self and neighbor share the same value, so softmax gives equal weights 0.5/0.50.5/0.5 and h4(2)=2.0h^{(2)}_4=2.0 exactly
    • Atom 55 (neighbor {3}\{3\} only, self h5(1)=2.5h^{(1)}_5=2.5, neighbor h3(1)=2.0h^{(1)}_3=2.0): scores (0,0.1)(0,-0.1); softmax weights (0.525,0.475)\approx(0.525,0.475); h5(2)0.525(2.5)+0.475(2.0)2.263h^{(2)}_5\approx0.525(2.5)+0.475(2.0)\approx2.263

    Summing all six: 1.226+1.482+1.226+2.082+2.0+2.26310.2781.226+1.482+1.226+2.082+2.0+2.263\approx10.278 — the ring's whole-molecule prediction.

  5. Same atoms, different bonds: the chain's prediction

    Wiring the exact same six atoms into a chain (0-1-2-3-4-50\text-1\text-2\text-3\text-4\text-5) instead changes every neighborhood, so layer 1 gives different means:

    • h0(1)=1+22=1.5h^{(1)}_0=\frac{1+2}{2}=1.5
    • h1(1)=2+1+031.0h^{(1)}_1=\frac{2+1+0}{3}\approx1.0
    • h2(1)=0+2+331.667h^{(1)}_2=\frac{0+2+3}{3}\approx1.667
    • h3(1)=3+0+131.333h^{(1)}_3=\frac{3+0+1}{3}\approx1.333
    • h4(1)=1+3+23=2.0h^{(1)}_4=\frac{1+3+2}{3}=2.0
    • h5(1)=2+12=1.5h^{(1)}_5=\frac{2+1}{2}=1.5

    Running the same layer-2 attention step on these values gives h(2)(1.263, 1.462, 1.348, 1.739, 1.627, 1.811)h^{(2)}\approx(1.263,\ 1.462,\ 1.348,\ 1.739,\ 1.627,\ 1.811), summing to 1.263+1.462+1.348+1.739+1.627+1.8119.2501.263+1.462+1.348+1.739+1.627+1.811\approx9.250 — lower than the ring's 10.27810.278, purely because the chain's end atoms (00 and 55) have only one neighbor to draw on instead of two.

Checkpoint

Find the bond structure, among the three candidates, that gives the highest predicted property.

Pick a structure to try it
Summary
y^=vGAT(GCN(h(0)))v\hat{y} = \sum_v \text{GAT}\big(\text{GCN}(h^{(0)})\big)_v

Two layers, two different rules, one readout — and the atoms never had to change for the prediction to change. That's the entire reason this part exists: a molecule's properties depend on its bonds, not just its atoms, and a plain feedforward network fed only atom features has no way to see that at all. Real molecular GNNs — short for Graph Neural Networks — stack more layers, learn every weight instead of fixing them by hand, and use richer readouts — but the shape of the pipeline (message passing, then attention, then pooling to one number) is exactly what's here. Part XV turns to a different structural limitation entirely: what happens when the "neighbors" aren't a handful of bonded atoms, but every previous token in a sequence thousands of positions long.