Foundations to Frontier
Tests
E. Deep Learning › E14 Graph Neural Networks

Graph Attention Network (GAT, GATv2)

Graph attention networks (Veličković and colleagues, ICLR 2018) replace the GCN's fixed, degree-based neighbour weights with learned attention: each node scores its neighbours, normalises the scores with a softmax and takes a weighted average, using several heads. GATv2 (Brody, Alon and Yahav, ICLR 2022) found a hidden limit in the original. GAT's scoring function ranks neighbours the same way for every query node ("static attention"). Moving the nonlinearity inside the scoring fixes this and gives dynamic attention. This page covers attention coefficients, multi-head attention and the static-versus-dynamic distinction. In the exercises you implement both scoring functions, then train each on a dictionary-lookup task: 6 queries must each find the key that matches them. GAT gave every query the same top key in 100% of graphs and stayed at chance (0.18). GATv2 solved the task (0.92; 1.00 on two other seeds).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Listen more to the neighbours that matter

In a GCN every neighbour counts according to a fixed formula. In a graph attention network, each node decides how much to listen to each neighbour, based on their features. It computes a score for every neighbour, turns the scores into weights that sum to one, and averages accordingly. The improved GATv2 lets those choices depend on the node doing the listening, not just on the neighbour. The demo shows one node’s attention weights over its neighbours.

Graph neural network lab
01234567891011
Node 0 attends to its 3 neighbours with weights 0.23, 0.57, 0.19. GAT learns these scores; GCN uses fixed degree-based weights instead.
Why engineers care

Attention lets a model focus on the relevant neighbours: the critical line in a power grid, the nearby robot that matters, the bonded atom that drives a property. It also gives weights that can be inspected.

📝 Quick check (Level 0)
0/3 answered
  1. 1. GAT's neighbour weights are…
  2. 2. Attention weights over neighbours sum to…
  3. 3. GATv2 lets attention depend on…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • GAT scores: e_ij = LeakyReLU(aᵀ[W h_i ‖ W h_j]) = LeakyReLU(a_sᵀ W h_i + a_nᵀ W h_j), with negative slope 0.2.
    • LeakyReLU(−2) = −0.4.
  • Attention and update:
    • α_ij = softmax over j ∈ N(i) of e_ij; equal scores over 4 neighbours give 0.25 each;
    • h_i’ = σ(Σ_j α_ij W h_j).
  • Multi-head: K heads run in parallel and are concatenated (8 heads × 8 features = 64) or averaged in the final layer.
    • Each head has W (d_in × F) and a (2F): 8 × (1433·8 + 16) = 91,840 weights for the Cora GAT’s first layer.
  • Static attention: LeakyReLU is monotone, so for every query i the neighbour with the largest a_nᵀ W h_j has the largest score. All queries share one ranking.
  • GATv2: e_ij = aᵀ LeakyReLU(W [h_i ‖ h_j]). Applying a after the nonlinearity lets the ranking change with i.
📝 Quick check (Level 1)
0/3 answered
  1. 1. LeakyReLU(−2) with slope 0.2:
  2. 2. 8 heads of 8 features, concatenated:
  3. 3. In GAT, the top-ranked neighbour is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Graph attention in code

Exercise 1.

  • GAT scores gave the same top neighbour for all 5 queries, as the theory predicts.
  • GATv2 scores and the softmax were implemented.

Practice.

  • In PyTorch Geometric, use GATConv(in, out, heads=8) or GATv2Conv. Typical settings are attention dropout 0.6 and an ELU nonlinearity.
  • Prefer GATv2 when the best neighbour depends on the node asking: matching, routing and retrieval tasks.
  • Attention weights are a useful but imperfect explanation of model decisions.
from torch_geometric.nn import GATv2Conv
conv1 = GATv2Conv(d_in, 8, heads=8, dropout=0.6)            # 8 heads x 8 features, concatenated
conv2 = GATv2Conv(64, n_classes, heads=1, concat=False, dropout=0.6)
x = F.elu(conv1(x, edge_index)); out = conv2(x, edge_index)
🐍 Exercise 1: GAT and GATv2 scores
Implement GAT and GATv2 attention scores and the neighbour softmax.
📝 Quick check (Level 2)
0/3 answered
  1. 1. PyG layer for dynamic attention:
  2. 2. Typical GAT attention dropout:
  3. 3. Attention weights as explanations are…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Static attention (Brody, Alon and Yahav).

  • In GAT, e_ij = LeakyReLU(q_i + k_j) with q_i = a_sᵀ W h_i and k_j = a_nᵀ W h_j.
  • LeakyReLU is strictly increasing, so for fixed i the scores are ordered like k_j. The argmax neighbour j* = argmax k_j is the same for every i.
  • GAT can make attention sharper or flatter for different queries, but never change the ranking.

Dynamic attention.

  • GATv2 computes aᵀ LeakyReLU(W_s h_i + W_n h_j). The nonlinearity mixes query and key before the final projection.
  • With enough width, this can represent any scoring function of (h_i, h_j) on a finite set (a universal-approximation argument), including different rankings for different queries.

Versus Transformers. Dot-product attention, q_iᵀ k_j, is also dynamic. Graph Transformers (a later page) use it, with structural encodings, over all nodes or over neighbourhoods.

📝 Quick check (Level 3)
0/3 answered
  1. 1. GAT's ranking is fixed because LeakyReLU is…
  2. 2. GATv2 applies a…
  3. 3. Dot-product attention is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Can GAT attend to the right neighbour?

History.

  • P. Veličković, G. Cucurull, A. Casanova, A. Romero, P. Liò and Y. Bengio, “Graph attention networks” (ICLR 2018).
  • S. Brody, U. Alon and E. Yahav, “How attentive are graph attention networks?” (ICLR 2022), introduced GATv2 and the dictionary-lookup benchmark used here.

Exercise 2.

  • Graphs: bipartite graphs in which 6 query nodes each connect to the same 6 key nodes.
  • Task: key j carries an id and a value (a random permutation per graph). Query i carries id i and must output the value of the key with the same id.
  • Models: one attention layer, GAT or GATv2 (16-dimensional scoring), trained for 600 Adam steps on fresh graphs.
  • Test: 200 new graphs, measuring accuracy, how often all queries in a graph share the same top key, and the attention weight on the matching key.
model accuracy graphs where all queries share one top key weight on matching key
GAT 0.178 100% 0.13
GATv2 0.921 0% 0.71

Chance is 1/6 = 0.167.

  • GAT is stuck at chance: in every test graph, all six queries ranked the same key first, exactly as the static-attention argument predicts. It cannot route each query to its own key.
  • GATv2 solves the task: its attention ranking differs between queries, and it reached 0.92 (seeds 1 and 2: 1.00 and 1.00, while GAT gave 0.17 and 0.17).
  • A surprise on seed 1: GATv2 reached perfect accuracy with zero weight on the matching key. It learned to attend to every key except the matching one and to decode the one missing value, a query-dependent solution that a simple “does it attend to the right key?” check would miss.
🐍 Exercise 2 (research): can GAT attend to the right neighbour?
Train GAT and GATv2 attention on a dictionary-lookup task and compare accuracy and how query-dependent their attention is.

Graph attention in AI, robotics and aerospace

  • Multi-agent coordination: each robot or aircraft attends to the neighbours most relevant to it, which needs dynamic attention.
  • Sensor fusion on graphs: weighting reliable sensors more than noisy ones.
  • Design lesson: check whether your attention mechanism can express query-dependent choices. Static scoring silently limits what it can learn.

References

  1. P. Veličković et al. (2018). “Graph attention networks.” ICLR.
  2. S. Brody, U. Alon and E. Yahav (2022). “How attentive are graph attention networks?” ICLR.