A Generalization of Transformer Networks to Graphs

Vijay Prakash Dwivedi, Xavier BressonarXiv 2020

Abstract

We propose a generalization of transformer neural network architecture for arbitrary graphs. The original transformer was designed for Natural Language Processing (NLP), which operates on fully connected graphs representing all connections between the words in a sequence. Such architecture does not leverage the graph connectivity inductive bias, and can perform poorly when the graph topology is important and has not been encoded into the node features. We introduce a graph transformer with four new properties compared to the standard model. First, the attention mechanism is a function of the neighborhood connectivity for each node in the graph. Second, the positional encoding is represented by the Laplacian eigenvectors, which naturally generalize the sinusoidal positional encodings often used in NLP. Third, the layer normalization is replaced by a batch normalization layer, which provides faster training and better generalization performance. Finally, the architecture is extended to edge feature representation, which can be critical to tasks s.a. chemistry (bond type) or link prediction (entity relationship in knowledge graphs). Numerical experiments on a graph benchmark demonstrate the performance of the proposed graph transformer architecture. This work closes the gap between the original transformer, which was designed for the limited case of line graphs, and graph neural networks, that can work with arbitrary graphs.

Introduction

The Transformer architecture has revolutionized Natural Language Processing (NLP) and Computer Vision, but its application to graph-structured data has historically been limited. In NLP, a sentence is treated as a fully connected graph where every word can attend to every other word. This approach works for sequences but fails to leverage the structural inductive biases inherent in sparse, real-world graphs like molecules or social networks.

Standard Transformers rely on positional encodings (e.g., sinusoids) to understand word order. However, graphs lack a linear order, making traditional encoding methods insufficient for capturing complex topology. This paper addresses these gaps by adapting the Transformer's "attention-is-all-you-need" philosophy to arbitrary graphs.

Proposed Architecture

The authors introduce a Graph Transformer (GT) architecture defined by four primary modifications to the standard Transformer block:

  1. Graph Sparsity in Attention: Instead of global attention, the mechanism is restricted to a node’s local neighborhood. This respects the graph's natural connectivity and improves computational efficiency.
  2. Laplacian Positional Encodings (LapPE): To provide structural context, the model uses the $k$ smallest non-trivial eigenvectors of the graph Laplacian. These act as a multi-dimensional coordinate system, generalizing sinusoidal encodings to any graph shape.
  3. Batch Normalization: Replacing the standard Layer Normalization with Batch Normalization was found to provide faster training and superior generalization in graph contexts.
  4. Edge Feature Pipeline: For tasks like molecular modeling where bond types are critical, the architecture includes a dedicated pipeline to maintain and update edge representations alongside node features.
The Graph Transformer architecture showing the integration of Laplacian Positional Encodings and the optional edge feature pipeline.

Experimental Setup

The model was evaluated on three major benchmarks from the "Benchmarking GNNs" suite:

  • ZINC: A molecular dataset (12,000 graphs) for regressing constrained solubility, testing the edge feature pipeline.
  • PATTERN & CLUSTER: Synthetic datasets (14,000 and 12,000 graphs) generated via Stochastic Block Models (SBM) to test node classification and structural pattern recognition.

Key Results and Discussion

The Graph Transformer consistently outperformed baseline isotropic (GCN) and anisotropic (GAT) Graph Neural Networks. On the ZINC dataset, the GT achieved a Mean Absolute Error (MAE) of 0.226, significantly outperforming GCN (0.367) and GAT (0.384).

The Importance of Sparsity and Normalization

A critical finding was that "Full Graph" attention—where every node attends to every other node—often performed worse than sparse, neighborhood-based attention. This suggests that local connectivity is a vital signal that global attention can inadvertently obscure. Furthermore, the ablation studies confirmed that the combination of Laplacian PE and Batch Normalization is essential for peak performance.

Closing the GNN-Transformer Gap

This work positions the Graph Transformer as a bridge between two paradigms. It demonstrates that GNNs (like GAT) are essentially Transformers operating on sparse graphs, while standard Transformers are GNNs operating on fully connected graphs. By introducing Laplacian eigenvectors, the authors provide a mathematically elegant "sinusoid for graphs" that reflects a node's relative position and connectivity.

Conclusion

The proposed Graph Transformer offers a simple and generic framework that can serve as a "black box" for future graph-based applications. While challenges remain in scaling to massive single graphs with billions of nodes or extending to highly heterogeneous domains, this architecture provides a robust foundation for graph pre-training and complex structural modeling.

Summary updated

BibTeX
@misc{dwivedi2020generalization,
  author = {Vijay Prakash Dwivedi and Xavier Bresson},
  title = {{A Generalization of Transformer Networks to Graphs}},
  year = {2020},
  eprint = {2012.09699},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2012.09699},
}
Figure