Graph Attention Network (GAT): Attention on Graphs

Graph Attention Network (GAT) : Guide Complet — Attention sur Graphes

Graph Attention Network: Complete Guide — Attention on Graphs

Summary — The Graph Attention Network (GAT), proposed by Veličković et al. in 2017, improves GCNs by replacing uniform neighbor aggregation with a learned attention mechanism. Each neighbor receives a specific and dynamic attention weight, allowing the model to focus on the most relevant neighbors. With multi-head attention, GAT simultaneously captures multiple information subspaces, achieving superior performance on node classification (Cora, Citeseer, PubMed).


Mathematical Principle

1. Attention Mechanism on Graphs

For each node i, GAT computes an attention score with each of its neighbors j ∈ N(i):

e_ij = LeakyReLU(a^T [Wh_i || Wh_j])

Where:
– h_i ∈ R^F is the representation of node i
– W ∈ R^{F'×F} is a shared linear transformation matrix
– a ∈ R^{2F'} is a learned attention vector
– || denotes concatenation
– LeakyReLU with typical negative slope of 0.2

2. Softmax Normalization

Attention scores are normalized over all neighbors of node i:

α_ij = softmax_j(e_ij) = exp(e_ij) / sum_{k∈N(i)} exp(e_ik)

This guarantees that attention coefficients form a probability distribution: sum_j α_ij = 1.

3. Weighted Aggregation

The updated representation of node i is a weighted linear combination of its neighbors:

h_i' = σ(sum_{j∈N(i)} α_ij · Wh_j)

Where σ is a non-linear activation function (typically ELU).

4. Multi-Head Attention

To stabilize learning and capture different aspects of structure, GAT uses M attention heads in parallel:

Intermediate layers (concatenation):

h_i' = ||_{m=1..M} σ(sum_j α_ij^m · W^m h_j)

Final layer (averaging):

h_i' = σ(average_{m=1..M} sum_j α_ij^m · W^m h_j)

Concatenation multiplies the output dimension by M, while averaging preserves it.

5. Masked Attention

Attention is restricted to immediate neighbors in the graph (masked). Unlike Transformer attention which computes correlations between all pairs, GAT only considers existing connections, respecting the graph topology.


Intuition

Imagine you’re looking for advice on choosing a restaurant in an unfamiliar city.

You have 50 friends on social media, but they’re not all equally valuable:
– Your foodie friend who tests restaurants every week counts a lot
– Your colleague who doesn’t like going out counts very little
– Your neighbor who lives in that city counts moderately

A classical GCN would give equal weight to all your friends — as if each opinion counted exactly 1/50. GAT, on the other hand, automatically learns who is most relevant for this specific question.

Even more subtle: multiple attention heads mean your foodie is great for culinary quality but bad for prices, while another friend is an expert on good deals. GAT captures these different dimensions of relevance simultaneously.


Python Implementation

1. GAT on Cora with PyTorch Geometric

[Python code block preserved as-is from original]

2. Visualizing Attention Weights

[Python code block preserved as-is from original]


Hyperparameters

Hyperparameter Typical Value Description
heads 8 Number of attention heads (more = more expressive but heavier)
hidden_channels 64-256 Dimension per head (concat output is heads × hidden)
dropout 0.4-0.6 Regularization applied before each GAT layer
attention_dropout 0.3-0.6 Dropout specific to attention coefficients
negative_slope 0.2 LeakyReLU slope in attention computation
concat (intermediate) True Concatenate heads at intermediate layers
concat (final) False Average heads at the final layer
learning_rate 0.005 Adam, with weight_decay (5e-4)

Advantages

  1. Learned attention: Unlike GCN which uses fixed degree-based weights, GAT dynamically learns which neighbors matter most.
  2. Graph-agnostic: GAT doesn’t need the complete graph structure in advance (unlike GCN which precomputes the normalized matrix). It works on inductive graphs (new nodes).
  3. Interpretability: The attention coefficients α_ij are directly interpretable: they reveal which neighbors influence each node the most.
  4. Multi-head: Captures multiple types of relationships simultaneously, with each head able to specialize in a different aspect of the structure.

Limitations

  1. Computational cost O(V · E): Computing attention scores for each connected node pair is more expensive than GCN’s average aggregation.
  2. Overfitting on small graphs: With little training data (Cora has only 140 training labels), the 8 attention heads can overlearn. Dropout is crucial.
  3. Variable performance: On some benchmarks, GAT does not significantly beat simple GCN. Gains are clear on graphs with neighborhood heterogeneity.

4 Concrete Use Cases

1. Document Classification in a Citation Graph

GAT’s original application: classifying scientific articles (Cora, Citeseer, PubMed) using their citation networks. Articles citing the same “pivot” articles receive more weight, capturing semantic links more faithfully than GCN. GAT achieves 83.0% on Cora, versus 81.4% for GCN.

2. Social Recommendation

In a social network of users and products, GAT learns who to trust for which recommendations. When a user buys a product, GAT weights their friends’ reviews differently based on their expertise in the relevant domain (electronics, fashion, cooking).

3. Financial Fraud Detection

In a transaction graph (nodes = accounts, edges = transfers), GAT identifies suspicious accounts by learning that certain types of connections (rapid transfers, round amounts, complex sub-account networks) are more indicative of fraud than others.

4. Molecular Property Prediction

In a molecule (graph: atoms = nodes, bonds = edges), GAT learns that certain neighboring atoms (double bond with oxygen, aromatic ring) are more influential than others for predicting the compound’s solubility or toxicity. Multi-head attention captures different chemical properties simultaneously.


GAT vs GCN Comparison

Aspect GCN GAT
Neighbor weights Fixed (degree-based) Learned by attention
Inductive No (needs recalculation) Yes (works on new nodes)
Computational cost O(E) O(E · F’)
Interpretability Low High (attention weights visible)
Cora Performance ~81.4% ~83.0%
Multi-relational Not natively Yes (multi-head)

Conclusion

The Graph Attention Network brought a key innovation to the GNN field: replacing uniform aggregation with learned attention. This simple yet powerful idea allows the model to distinguish relevant neighbors from noise, improving performance while offering natural interpretability.

GAT has inspired a family of derived architectures: GATv2 (more expressive dynamic attention), AGNN (attention without prior linear transformation), and hypergraph attention models. Attention on graphs is now a standard component in the geometric deep learning toolkit.

See Also

About Salah YAHIAOUI

Salah YAHIAOUI publie sur The Pingouin des tutoriels Python et des décryptages sur l’intelligence artificielle. Les cas pratiques récents précisent leurs données, leurs tests et leurs limites. À propos, méthode éditoriale et contact.

View all posts by Salah YAHIAOUI →