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

Graph attention networks

Hook

The Graph Convolutional Network (GCN)'s flat average treats every neighbor as equally important — a node with one quiet neighbor and one wildly different one gets pulled toward their plain mean regardless. Real relationships aren't equal. What if the aggregation rule itself could decide who to listen to?

Intuition
01.012.020.033.041.052.0
self=0.19, node 0=0.16, node 2=0.13, node 3=0.52

Click a node. Every edge in its neighborhood gets its own thickness now — that's an attention weight, not a fixed 1deg(v)+1\frac{1}{\deg(v)+1} share. Node 3's neighbors don't all matter the same amount to it, and the graph now shows exactly how much each one does.

Formalize

A Graph Attention Network (GAT) layer replaces GCN's fixed mean with a learned, per-neighbor weight. For node ii and each member jj of its neighborhood (itself included):

eij=LeakyReLU(a[WhiWhj]),αij=exp(eij)kN(i){i}exp(eik),hi=jαijWhje_{ij} = \text{LeakyReLU}\big(a^\top [Wh_i \,\Vert\, Wh_j]\big), \qquad \alpha_{ij} = \frac{\exp(e_{ij})}{\sum_{k \in \mathcal{N}(i)\cup\{i\}} \exp(e_{ik})}, \qquad h_i' = \sum_{j} \alpha_{ij} \, Wh_j
  • eije_{ij} — the raw, unnormalized attention score between node ii and neighbor jj.
  • αij\alpha_{ij} — the normalized attention weight node ii assigns to neighbor jj, after softmax.
  • WW — a learned weight matrix applied to every node's features before scoring.
  • aa — a learned attention vector that scores how compatible two transformed features are.
  • hih_i, hjh_j — node ii's and neighbor jj's current feature vectors.
  • hih_i' — node ii's updated feature: the attention-weighted sum over itself and its neighbors.
  • N(i)\mathcal{N}(i) — node ii's neighborhood, including itself.
  1. W and a score compatibility, like query-key attention

    WW is a learned weight matrix and aa a learned attention vector — together they score how compatible ii and jj are, exactly the way a query and key score compatibility in Part IV's attention chapter.

  2. This chapter's simplification

    With one scalar feature per node and WW fixed to the identity, that score collapses to a straight comparison of the two raw features. This chapter fixes it at eij=LeakyReLU(hjhi)e_{ij} = \text{LeakyReLU}(h_j - h_i).

  3. LeakyReLU lets asymmetric signal through

    A neighbor with a much larger feature scores far higher, one with a much smaller feature is only mildly suppressed — LeakyReLU's whole point is to leak a little of that signal through instead of zeroing it, the same nonlinearity from Part III.

Play

Every bar is one node's GAT update minus what GCN's plain mean would have produced, on the exact same starting graph. None of them are zero — attention never reduces to a flat average unless every neighbor already agrees. Node 4's bar is the tallest: it's a leaf with exactly one neighbor, so there's no averaging-out effect to soften how hard that neighbor's much larger feature pulls the update.

Worked example

Node 11 (feature 22), attending to itself and its three neighbors — node 00 (feature 11), node 22 (feature 00), node 33 (feature 33):

  1. Raw scores
    • e1,1=LeakyReLU(22)=LeakyReLU(0)=0e_{1,1}=\text{LeakyReLU}(2-2)=\text{LeakyReLU}(0)=0
    • e1,0=LeakyReLU(12)=LeakyReLU(1)=0.2×(1)=0.2e_{1,0}=\text{LeakyReLU}(1-2)=\text{LeakyReLU}(-1)=0.2\times(-1)=-0.2
    • e1,2=LeakyReLU(02)=LeakyReLU(2)=0.2×(2)=0.4e_{1,2}=\text{LeakyReLU}(0-2)=\text{LeakyReLU}(-2)=0.2\times(-2)=-0.4
    • e1,3=LeakyReLU(32)=LeakyReLU(1)=1e_{1,3}=\text{LeakyReLU}(3-2)=\text{LeakyReLU}(1)=1

    Node 33's score is the only one that grows past its raw gap — every negative score gets shrunk to a fifth of its size.

  2. Softmax turns those into weights

    Subtracting the max score (11) for stability, then exponentiating:

    • e01=e10.368e^{0-1}=e^{-1}\approx0.368 (self)
    • e0.21=e1.20.301e^{-0.2-1}=e^{-1.2}\approx0.301 (node 00)
    • e0.41=e1.40.247e^{-0.4-1}=e^{-1.4}\approx0.247 (node 22)
    • e11=e0=1e^{1-1}=e^{0}=1 (node 33)

    Sum 0.368+0.301+0.247+1=1.916\approx0.368+0.301+0.247+1=1.916. Normalizing:

    • α1,10.368/1.9160.192\alpha_{1,1}\approx0.368/1.916\approx0.192
    • α1,00.301/1.9160.157\alpha_{1,0}\approx0.301/1.916\approx0.157
    • α1,20.247/1.9160.129\alpha_{1,2}\approx0.247/1.916\approx0.129
    • α1,31/1.9160.522\alpha_{1,3}\approx1/1.916\approx0.522

    Node 33 alone gets more than half the attention, despite being just one of three neighbors.

  3. The weighted sum lands far from a plain average

    h1=0.192(2)+0.157(1)+0.129(0)+0.522(3)2.107h_1' = 0.192(2) + 0.157(1) + 0.129(0) + 0.522(3) \approx 2.107. GCN's flat mean of the same four numbers is exactly 1.51.5 — attention pulls node 11's update a full 0.60.6 higher, entirely because node 33 dominates the weighted sum instead of counting for a plain 14\frac{1}{4}.

Checkpoint

Click through the nodes and find the one whose GAT update diverges furthest from what GCN's flat average would give it.

01.412.121.632.142.852.7
click a node
Click a node to compare
Summary
αij=softmaxj(LeakyReLU(a[WhiWhj])),hi=jαijWhj\alpha_{ij} = \text{softmax}_j\big(\text{LeakyReLU}(a^\top[Wh_i \Vert Wh_j])\big), \qquad h_i' = \sum_j \alpha_{ij}\,Wh_j

GCN assumes every neighbor deserves an equal vote; GAT learns the votes instead, using the same query-key compatibility idea that made Part IV's attention mechanism work on sequences — just scored between a node and its graph neighbors instead of between sequence positions. Real GAT layers typically run several of these attention heads in parallel (exactly like multi-head attention) and stack a learned WW per layer; this chapter fixed both to keep every number checkable by hand. The next chapter wires message passing and attention together on a genuinely different kind of graph: a molecule, where the "neighbors" are chemical bonds and the thing being predicted is a property of the whole structure.