Understanding attention in graph neural networks

Boris Knyazev, Graham W. Taylor, Mohamed R. Amer

Abstract

We aim to better understand attention over nodes in graph neural networks (GNNs) and identify factors influencing its effectiveness. We particularly focus on the ability of attention GNNs to generalize to larger, more complex or noisy graphs. Motivated by insights from the work on Graph Isomorphism Networks, we design simple graph reasoning tasks that allow us to study attention in a controlled environment. We find that under typical conditions the effect of attention is negligible or even harmful, but under certain conditions it provides an exceptional gain in performance of more than 60% in some of our classification tasks. Satisfying these conditions in practice is challenging and often requires optimal initialization or supervised training of attention. We propose an alternative recipe and train attention in a weakly-supervised fashion that approaches the performance of supervised models, and, compared to unsupervised models, improves results on several synthetic as well as real datasets.

Introduction

Attention mechanisms in Graph Neural Networks (GNNs) are theoretically powerful tools for improving model generalization. By allowing a network to focus on the most relevant nodes, attention should, in principle, help GNNs handle the transition from small, clean training graphs to the larger, noisier graphs encountered in real-world deployment. However, in practice, these mechanisms are notoriously difficult to train effectively and often provide negligible benefits.

This research investigates why attention is so challenging to optimize in GNNs and identifies the specific conditions under which it provides a significant performance boost. The authors demonstrate that while poorly trained attention can actively degrade performance, well-trained attention allows GNNs to achieve massive gains in robustness.

Methodology

The study focuses on node-level attention and its integration with graph pooling. The authors introduce three primary technical contributions to improve GNN reasoning:

  1. Threshold-based Pooling: Unlike traditional "top-k" pooling, which retains a fixed percentage of nodes, this method keeps nodes whose attention scores exceed a learned threshold $\tilde{\alpha}$. This allows the model to adaptively prune the graph based on the complexity of the input.
  2. ChebyGIN: A hybrid architecture designed for high expressivity, combining the structural reasoning of Graph Isomorphism Networks (GIN) with the multi-scale spectral capabilities of ChebyNet.
  3. Weakly-supervised Attention Training: Recognizing that ground-truth attention labels are rarely available, the authors propose a two-stage "recipe." First, a baseline model is trained with global pooling. Then, the "sensitivity" of the model to each node is measured by observing how predictions change when a node is removed. These sensitivity scores serve as pseudo-labels to train the attention mechanism of a second, more robust model.
Overview of the node attention mechanism applied to the COLORS, TRIANGLES, and MNIST tasks, showing the filtering of input graph X into a sparse representation Z.

Experimental Setup and Datasets

To study attention in a controlled environment, the researchers utilized a mix of synthetic and real-world benchmarks designed to test specific structural reasoning capabilities:

  • COLORS: A task requiring the model to count nodes of a specific feature (color) amidst distractors.
  • TRIANGLES: A structural task where the model must count the number of triangles in a graph.
  • MNIST-75SP: A graph-based adaptation of the MNIST digit dataset, where images are represented as superpixel graphs.
  • Real-world Benchmarks: The weakly-supervised approach was further validated on COLLAB (social networks), PROTEINS, and D&D (bioinformatics).
Visualization of the training and testing datasets, demonstrating the significant increase in graph size (N) and noise levels used to evaluate model generalization.

Key Insights and Results

The "All-or-Nothing" Nature of Attention

The research reveals an exponential relationship between attention accuracy and classification performance. A model with 90% accurate attention may show little improvement over a baseline, but as accuracy approaches 99%, a massive "jump" in performance occurs. This creates an "initialization trap": if random weights start too far from the ground truth, the model often fails to find the optimal attention state.

Generalization via Sparsity

The primary value of attention is found in its ability to provide robustness. By learning to ignore "distractor" nodes or noise, the model can process graphs significantly larger or more corrupted than those seen during training.

  • Massive Gains on Noisy Data: In the COLORS-LARGEC task (featuring unseen colors and larger graphs), a standard GIN model achieved only 26% accuracy. Supervised attention boosted this to 89%, while the proposed weakly-supervised method reached 73%, significantly outperforming standard unsupervised attention (~11%).
  • Real-world Performance: On the D&D protein dataset, the weakly-supervised approach improved accuracy from 72.7% (global pooling) to 78.4%.
  • Interpretability: The attention weights generated via weak supervision were highly interpretable, correctly identifying critical "center nodes" in social networks and functional chains in protein structures.

Conclusion and Future Work

While attention is a powerful lever for GNN generalization, its effectiveness is highly sensitive to training strategies. The proposed weakly-supervised method offers a path toward robust GNNs without the need for manual attention labels. However, the approach currently requires a two-stage training process and $O(N)$ sensitivity calculations, which can be computationally expensive for very large graphs. Future research will focus on more efficient signal generation and better initialization strategies to enable effective attention learning in a single pass.

Summary updated

Abstract

We aim to better understand attention over nodes in graph neural networks (GNNs) and identify factors influencing its effectiveness. We particularly focus on the ability of attention GNNs to generalize to larger, more complex or noisy graphs. Motivated by insights from the work on Graph Isomorphism Networks, we design simple graph reasoning tasks that allow us to study attention in a controlled environment. We find that under typical conditions the effect of attention is negligible or even harmful, but under certain conditions it provides an exceptional gain in performance of more than 60% in some of our classification tasks. Satisfying these conditions in practice is challenging and often requires optimal initialization or supervised training of attention. We propose an alternative recipe and train attention in a weakly-supervised fashion that approaches the performance of supervised models, and, compared to unsupervised models, improves results on several synthetic as well as real datasets. Source code and datasets are available at https://github.com/bknyaz/graph_attention_pool.
BibTeX
@inproceedings{knyazev2019understanding,
  author = {Boris Knyazev and Graham W. Taylor and Mohamed R. Amer},
  title = {{Understanding attention in graph neural networks}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2019},
  url = {https://openreview.net/forum?id=NXgu79aDPo},
}
Figure