Search

Search papers and lessons

Graph Attention Networks

Petar Veličković, Guillem Cucurull, Arantxa Casanova et al. · 2017

GAT lets each node weight its neighbours with learned attention, so a graph model no longer treats every neighbour the same and works on graphs it never saw in training.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Architectures
  • Learning basics
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

A graph attention network classifies the nodes of a graph by letting each node attend over its neighbours. Every neighbour gets a learned weight, computed from the features of the two nodes, so the model can treat close and weak neighbours differently instead of averaging them equally. It needs no eigendecomposition or similar costly matrix operation, and it can run on a graph it never saw in training. The paper reports state-of-the-art or matching results on three citation networks (Cora, Citeseer, Pubmed) and on a protein-protein interaction dataset with unseen test graphs.

Builds on: Graph Convolutional Network, Multi-Head Attention, Transductive Learning, Softmax · Leads to: Neighborhood Aggregation, the general pattern that later graph networks refine

Why it mattered

Graph data is everywhere: citation networks, social networks, road maps, molecules, protein interactions. Before GAT, the strongest graph models were built from graph convolutions, which average a node's neighbours with fixed weights set by the graph's structure. GAT showed that the attention idea from language models, where a word weights other words by relevance, transfers cleanly to graphs. The paper's layer became a common building block for later graph models.

The paper also tests inductive learning directly. Its protein-protein interaction experiment trains on some graphs and tests on two graphs the model never saw during training, which the paper contrasts with methods that depend on the full graph structure.

The problem

Earlier graph models had two weaknesses that the paper names.

  1. Spectral methods define convolutions in the eigenbasis of the graph Laplacian, which depends on the particular graph. A filter learned on one graph cannot be applied directly to a graph with a different structure, and computing eigenvectors is expensive. The Laplacian is a matrix that measures how much a signal varies across edges.
  2. Graph convolutions such as GCN weight neighbours by fixed, structure-based normalisation. Every neighbour's influence is set by how many connections the two nodes have, not by what the neighbour's features say about the node being classified.

Inductive methods that sample a fixed number of neighbours avoid the cost, but they throw information away, and some depend on an arbitrary ordering of neighbours. The paper asks for an operator that uses the whole neighbourhood, learns its weights from features, needs no global graph structure and works on unseen graphs.

How it works

Here is one graph attention layer for one node, traced through a tiny neighbourhood.

flowchart LR
  A["node features h"] --> B["shared linear map W"]
  B --> C["score each neighbour with vector a"]
  C --> D["softmax over the neighbourhood"]
  D --> E["weighted sum of neighbour features"]
  E --> F["nonlinearity gives new features"]
  1. Transform every node with one shared matrix. Each node's feature vector h is multiplied by the same weight matrix W. This gives every node a new feature vector, W h, in a space where comparisons are made.

  2. Score each neighbour. For node i and each neighbour j in its neighbourhood (including i itself), compute a score e_ij from the pair of transformed vectors. A single learned vector a is applied to the concatenation of the two vectors, and a LeakyReLU makes negative scores smaller, not zero. Check: if a is (0.5, 1), node i has transformed value 1 and neighbour j has 2, the score is 0.5 times 1 plus 1 times 2, which is 2.5.

  3. Normalise the scores with softmax. The scores of all neighbours of i are turned into weights that are positive and sum to one. The weight for neighbour j is exp of its score, divided by the sum of exp over all neighbours. Check: with three neighbours the weights must add to 1, and in the example below they do.

  4. Take the weighted sum. The new feature of node i is the sum over neighbours of the weight times the neighbour's transformed feature, passed through a nonlinearity sigma. A neighbour with weight 0.69 contributes much more than one with weight 0.05.

  5. Run several heads and join them. The paper runs K independent attention mechanisms, each with its own W and a, and concatenates their outputs. Eight heads of eight features give 64 features per node. In the final classification layer the heads are averaged instead, and the softmax or sigmoid is applied after the averaging.

  6. Stack layers. Several such layers are stacked, so a node's representation depends on neighbours two or more hops away, up to the depth of the network. Because each layer touches only the edges that exist, the cost grows with the number of nodes plus the number of edges, not with the number of possible pairs.

The math

Definition. Let h_i be the input features of node i, with F values each, and let N_i be its neighbourhood, which in every experiment is i and its first-order neighbours. A single attention head computes

eij=a(Wh⃗i,Wh⃗j)e_{ij}=a(\mathbf{W}\vec{h}_i,\mathbf{W}\vec{h}_j)

for each j in N_i, and then normalises across the neighbourhood.

Step 2 in symbols: the score

In the paper the attention mechanism is a single feedforward layer with weight vector a⃗∈R2F′\vec{a}\in\mathbb{R}^{2F'} and LeakyReLU with negative slope 0.2:

eij=LeakyReLU ⁣(a⃗T[Wh⃗i ∥ Wh⃗j])e_{ij}=\mathrm{LeakyReLU}\!\left(\vec{a}^{T}\left[\mathbf{W}\vec{h}_i\,\|\,\mathbf{W}\vec{h}_j\right]\right)

Here the double bar stands for concatenation, which stacks the two vectors into one longer vector. LeakyReLU returns x for positive x and 0.2 times x for negative x.

Plot of LeakyReLU against ReLU, the nonlinearity inside the score:

−3−2−10123−10123scoreoutput
  • LeakyReLU, slope 0.2
  • ReLU
LeakyReLU keeps a small negative slope, so a negative score still gives a small positive weight after exp.

Step 3 in symbols: the softmax

αij=softmaxj(eij)=exp⁡(eij)∑k∈Niexp⁡(eik)\alpha_{ij}=\mathrm{softmax}_j(e_{ij})=\frac{\exp(e_{ij})}{\sum_{k\in\mathcal{N}_i}\exp(e_{ik})}

The weights alpha_ij are positive and sum to 1 over the neighbourhood, which is what makes them comparable across nodes with different numbers of neighbours. Softmax is the function that does this normalisation.

Step 4 in symbols: the update

h⃗i′=σ ⁣(∑j∈Niαij Wh⃗j)\vec{h}'_i=\sigma\!\left(\sum_{j\in\mathcal{N}_i}\alpha_{ij}\,\mathbf{W}\vec{h}_j\right)

Step 5 in symbols: multi-head attention

With K heads, each with its own W^k and attention weights alpha^k_ij, the hidden layers concatenate:

h⃗i′=[σ ⁣(∑j∈Niαij1W1h⃗j) ∥ ⋯ ∥ σ ⁣(∑j∈NiαijKWKh⃗j)]\vec{h}'_i=\left[\sigma\!\Big(\sum_{j\in\mathcal{N}_i}\alpha^{1}_{ij}\mathbf{W}^{1}\vec{h}_j\Big)\,\|\,\cdots\,\|\,\sigma\!\Big(\sum_{j\in\mathcal{N}_i}\alpha^{K}_{ij}\mathbf{W}^{K}\vec{h}_j\Big)\right]

The final layer averages the heads instead:

h⃗i′=σ ⁣(1K∑k=1K∑j∈NiαijkWkh⃗j)\vec{h}'_i=\sigma\!\left(\frac{1}{K}\sum_{k=1}^{K}\sum_{j\in\mathcal{N}_i}\alpha^{k}_{ij}\mathbf{W}^{k}\vec{h}_j\right)

Step 6 in symbols: the cost

The paper gives the time complexity of one head producing F' features as O(|V| F F' + |E| F'), where |V| is the number of nodes and |E| the number of edges. That is the same order as a GCN layer, and it involves no eigendecomposition.

Worked examples

Example
1. LeakyReLU on a negative score
  1. Take a raw score of minus 3.
  2. Because it is negative, LeakyReLU multiplies by the slope 0.2: 0.2 times minus 3 equals minus 0.6.
  3. Compare with ReLU, which would return 0.

So LeakyReLU(minus 3) is minus 0.6, not 0. Its unnormalised weight exp(minus 0.6) is about 0.55, which is positive. A neighbour with a bad score still gets some influence, which keeps gradients alive for it.

Example
2. The three-neighbour softmax

Node i has three neighbours in its neighbourhood: itself (i), node j and node k. Let a = (0.5, 1), and let the transformed features be W h_i = 1, W h_j = 2 and W h_k = minus 1.

  1. Score the self-loop: 0.5 times 1 plus 1 times 1 is 1.5, and LeakyReLU of 1.5 is 1.5.
  2. Score neighbour j: 0.5 times 1 plus 1 times 2 is 2.5, and LeakyReLU of 2.5 is 2.5.
  3. Score neighbour k: 0.5 times 1 plus 1 times minus 1 is minus 0.5, and LeakyReLU of minus 0.5 is 0.2 times minus 0.5, which is minus 0.1.
  4. Exponentiate: exp(1.5) is about 4.4817, exp(2.5) is about 12.1825, exp(minus 0.1) is about 0.9048.
  5. Add them: about 17.5690.
  6. Divide to get weights: i gets about 0.255, j about 0.693, k about 0.052. They sum to 1.
  7. Take the weighted sum of transformed features: 0.255 times 1 plus 0.693 times 2 plus 0.052 times minus 1 is about 1.59.

The new feature of node i is about 1.59. Neighbour j, with the highest score, dominates, and the negative neighbour k is pushed down rather than cancelled out.

Example
3. Parameters of the first Cora layer

The first layer on Cora uses K = 8 heads, each with F' = 8 output features, from F = 1433 input features per node. Each head has a weight matrix W∈RF′×F\mathbf{W}\in\mathbb{R}^{F'\times F} and an attention vector a⃗∈R2F′\vec{a}\in\mathbb{R}^{2F'}. Ignore bias terms, which the paper's equations do not include.

  1. One head's weight matrix has 8 times 1433 entries, which is 11464.
  2. One head's attention vector has 2 times 8 entries, which is 16.
  3. One head therefore has 11464 plus 16, which is 11480 parameters.
  4. Eight heads have 8 times 11480, which is 91840 parameters.
  5. The layer outputs 8 times 8 features, 64 per node, after concatenation.

So the first layer has 91840 parameters, and its output is 64 features per node. Almost all the parameters sit in the input projections, not in the attention scores, because the attention vector is tiny.

Try it: the second layer takes the 64 features and produces C = 7 class scores with a single head. Count its parameters, with W∈RC×64\mathbf{W}\in\mathbb{R}^{C\times 64} and a⃗∈R2C\vec{a}\in\mathbb{R}^{2C}.

Answer: W has 7 times 64, which is 448 entries. a has 2 times 7, which is 14 entries. Total 448 plus 14 is 462.

Results

The paper uses four benchmarks. Cora (2708 nodes, 5429 edges, 7 classes, 1433 features per node), Citeseer (3327 nodes, 4732 edges, 6 classes, 3703 features) and Pubmed (19717 nodes, 44338 edges, 3 classes, 500 features) are transductive citation networks. Only 20 labelled nodes per class are used for training, with 500 for validation and 1000 for testing. The inductive protein-protein interaction dataset (PPI) has 24 graphs in total, of which 20 train, 2 validate and 2 test, and the test graphs are unseen during training. PPI has 50 features per node and 121 labels.

Table 2, classification accuracy on the citation networks (test set, mean over 100 runs with standard deviation):

MethodCoraCiteseerPubmed
GCN (Kipf and Welling)81.5%70.3%79.0%
GCN with 64 hidden features81.4%70.9%79.0%
MoNet81.7%not reported78.8%
GAT (this paper)83.0% ± 0.7%72.5% ± 0.7%79.0% ± 0.3%

Table 3, micro-averaged F1 on PPI (test graphs, mean over 10 runs):

MethodPPI
GraphSAGE-LSTM0.612
GraphSAGE, best the authors could tune (GraphSAGE*)0.768
Const-GAT (same architecture, constant attention)0.934 ± 0.006
GAT (this paper)0.973 ± 0.002

The Const-GAT row isolates the attention mechanism: same architecture, but every neighbour gets weight 1, so it behaves like a GCN-style average. The gain from learned attention on PPI is therefore 0.039 in F1, and the gain over the best tuned GraphSAGE is 0.205. The paper writes these gains as 3.9% and 20.5%; they are differences on the F1 scale.

On Cora and Citeseer, GAT beats the original GCN by 1.5 and 2.2 points, and the 64-feature GCN-64 by 1.6 and 1.6 points. On Pubmed the two are tied at 79.0 percent. The paper reports that Pubmed needed a change in the architecture (8 output heads and stronger regularisation), because its training set is only 60 nodes.

Limits. The baselines' numbers are copied from earlier papers rather than rerun under the same conditions, except the GCN-64 variant. The transductive numbers are over 100 runs, but the inductive ones over 10. The attention weights are visualised on Cora with a t-SNE plot, and the paper says that interpreting them needs domain knowledge and is left for future work. The sparse implementation supports only rank-2 tensors, which limits batching across graphs. The receptive field is bounded by the depth of the network. Tradeoffs. Attention costs more than a fixed-weight convolution and adds parameters for each head, so GAT is worth it when neighbours genuinely differ in importance or when the graph changes between training and use. When every neighbour should count equally, or the graph is huge and the budget is tight, a GCN is a simpler choice. The paper's Const-GAT row shows the difference in practice.

Reading guide

Read next: Semi-Supervised Classification with Graph Convolutional Networks for the convolutional baseline this paper improves on, and Neural Machine Translation by Jointly Learning to Align and Translate for the attention mechanism it adapts.

Check yourself

QuestionWhy does GAT use masked attention rather than letting every node attend to every other node?Show answer

Full attention would ignore the graph structure, because every node would be a neighbour of every other. Masking computes scores only for the neighbourhood N_i, so the graph's edges still define which nodes interact, and the cost scales with the number of edges instead of the square of the number of nodes.

QuestionA node has two neighbours with scores 1 and 3, and its own score is 1 as well. Compute the attention weight of the neighbour with score 3.Show answer

The three exponentials are exp(1), about 2.718, exp(3), about 20.086, and exp(1), about 2.718. Their sum is about 25.522. The weight of the neighbour with score 3 is 20.086 divided by 25.522, which is about 0.787.

QuestionWhy can GAT be applied to a graph that was not present during training, while a spectral model cannot?Show answer

GAT's weights act on each node's features and on the attention between a node and its neighbours, not on a fixed set of eigenvectors of one graph's Laplacian. The same learned W and a can be applied at every node of any graph. A spectral filter is tied to the eigenbasis of the training graph, so a different graph has a different basis and the filter no longer means the same thing.

QuestionA research group has a protein interaction graph where every edge matters equally and it must train on a single large graph within a tight memory budget. Which model should they choose first, and why?Show answer

A GCN. When every neighbour should count the same, the attention parameters and the per-head storage of GAT add cost without a clear gain, and the paper notes that multi-head attention multiplies the storage by K. A GCN has the same asymptotic cost with fewer parameters. GAT is worth trying if the neighbours later turn out to differ in importance.

Sources

Read the full paper

Petar Veličković, Guillem Cucurull, Arantxa Casanova et al., 2017. arXiv:1710.10903