Do Transformers Really Perform Bad for Graph Representation?
- Publication
- NeurIPS 2021Conference
- First submitted to arXiv
- arXiv:2106.05234
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{graphlearninggraphormer2021,
title = {{Do Transformers Really Perform Bad for Graph Representation?}},
author = {Chengxuan Ying and Tianle Cai and Shengjie Luo and Shuxin Zheng and Guolin Ke and Di He and Yanming Shen and Tie-Yan Liu},
year = {2021},
eprint = {2106.05234},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2106.05234}
}
By 2021, Transformers dominate natural language processing and have found firm footing in computer vision. Graph leaderboards tell a different story.
The OGB Large-Scale Challenge's PCQM4M-LSC has over 3.8 million molecules, requiring prediction of a quantum-chemistry property from molecular graphs. GT, a graph-adapted Transformer, has validation MAE 0.1400; widening to 83.2 million parameters makes it 0.1408. Neither beats GIN with virtual nodes (GIN-VN), at 0.1395 with only 6.7 million parameters.
Chengxuan Ying, Tianle Cai, Shengjie Luo and colleagues at Dalian University of Technology, Princeton, Peking University and Microsoft Research Asia put the question into their title: do Transformers really perform poorly on graphs?
Their Graphormer uses a standard Transformer and ultimately wins the challenge's graph-level track.
Defining the Problem
Many attempts to bring Transformers to graphs, the authors observe, succeed mainly by replacing a GNN component such as aggregation with softmax attention while retaining a GNN backbone.
What is missing? Self-attention computes semantic similarity between node features without graph structure. It cannot see which nodes are important, how far apart two nodes lie, or what edge types connect them.
The key, the authors argue, is to encode that structural information effectively.
Solving the Problem

Graphormer adds three encodings: one to nodes, two to node pairs, hence attention scores.
Centrality Encoding
In social networks, people with more followers tend to be more influential. The authors use simple degree centrality, looking up learnable vectors for each node's in-degree and out-degree and adding them to input features:
For undirected graphs, in-degree and out-degree coincide and can be combined. Both Q and K then carry node importance.
Spatial Encoding
For sentences, Transformers need explicit word positions. Graphs have no fixed axis, so the authors describe pairwise relationships instead.
The shortest-path distance selects a learnable scalar , added to the attention score:
Disconnected pairs receive a special distance −1 with its own bias. These biases are shared across all layers.
If learns to decrease with distance, nodes favor nearby nodes over distant ones while retaining access to the whole graph.
Edge Encoding
Molecular bond types are edge features. Earlier approaches added them to endpoint nodes or included them during aggregation, passing edge information only to connected nodes.
Graphormer puts edge features into attention scores too. For each node pair , choose a shortest path , take the inner product of each edge's features with a learnable vector, and average:
Other Details
LayerNorm precedes attention and the feedforward network: Pre-LN. A special [VNode] connects to all nodes, with its final-layer representation summarizing the graph, like BERT's [CLS]. Its distance to every node is 1, but these are not real edges, so its spatial encoding gets a dedicated bias.
Which GNNs Can It Represent?
With suitable weights and distance functions, the authors prove that one Graphormer layer represents GIN, GCN or GraphSAGE aggregation and update. For mean aggregation, set distance-1 bias to 0 and others to negative infinity, with and . Softmax then averages only neighbors.
Take a node with three neighbors. Its attention is split evenly among them, a third each, while the node itself and the nodes at distance 2 get 0. Multiplying the mean by the degree gives the sum, and the degree is available from the centrality encoding; max aggregation needs one attention head per dimension to pick out the maximum, with the proof in the paper's appendix.
Beyond WL
Conversely, Graphormer can see distinctions WL misses. The appendix gives a pair:

On the left is a ladder; on the right, two triangles joined by a bridge. Both graphs have four nodes of degree 2 and two of degree 3, and WL colors them identically in every round. Shortest-path distances are a different story. A blue node's distances to the other nodes are 0, 1, 1, 1, 2, 2 in both graphs; among the pink nodes, a corner of the ladder has only one node at distance 3, while an outer node of a triangle has two. As long as the model can see distances, the two graphs can be told apart.
The authors also prove that self-attention without any encoding can implement a whole-graph mean readout, effectively supplying a virtual node in every layer. Experimentally, increasing depth and width keeps reducing training and validation errors, without observed over-smoothing.
Discussion
PCQM4M-LSC
The main experiments use PCQM4M-LSC. Full Graphormer has 12 layers, hidden dimension 768 and 47.1 million parameters; the smaller version has 6 layers, 512 dimensions and 12.5 million parameters. Training takes around two days on 8 V100s.
| Model | Parameters | Validation MAE ↓ |
|---|---|---|
| GCN | 2.0M | 0.1691 |
| GIN | 3.8M | 0.1537 |
| GIN-VN | 6.7M | 0.1395 |
| DeeperGCN-VN | 25.5M | 0.1398 |
| GT | 0.6M | 0.1400 |
| GT-Wide | 83.2M | 0.1408 |
| Graphormer (small) | 12.5M | 0.1264 |
| Graphormer | 47.1M | 0.1234 |
Validation MAE falls 11.5% relative to the previous best GIN-VN. Making GT over a hundred times wider fails to improve it. For the challenge, an ensemble of Graphormer and another model, ExpC, reaches 0.1200 on the full test set, winning the graph-level track.
Other Datasets
ZINC offers the cleanest comparison: all models have around 500,000 parameters and train from scratch. GraphormerSLIM, with 489,000 parameters, achieves test MAE 0.122, versus SAN's 0.139, PNA's 0.142 and GT's 0.226.
The two OGB molecular datasets require attention to conditions. Graphormer reaches 31.39% average precision on MolPCBA versus the previous best 29.79%, and 80.51% ROC-AUC on MolHIV versus 79.70%. Both versions are pretrained on PCQM4M-LSC, have 119.5 million and 47 million parameters respectively, and use FLAG augmentation against overfitting. For fairness, the authors also report GIN-VN pretrained and fine-tuned in the same way: 29.02% and 77.80%.
Each Encoding's Contribution
PCQM4M-LSC ablations use 12-layer models trained for only 100,000 steps to save computation:
| Setting | Validation MAE ↓ |
|---|---|
| No structural encoding | 0.2276 |
| Laplacian positional encoding | 0.1483 |
| Shortest-path spatial encoding | 0.1427 |
| Spatial + centrality encoding | 0.1396 |
| Add edge encoding at nodes | 0.1328 |
| Add edge encoding during aggregation | 0.1327 |
| Add edge encoding as attention bias | 0.1304 |
Without structural encoding, Transformer MAE is 0.2276, far worse than encoded versions. Spatial encoding beats Laplacian encoding; edge encoding in attention scores beats the two conventional placements.
The Cost
The authors themselves list limitations in the conclusion. Attention is quadratic in nodes, making large graphs unaffordable; node-level tasks need sampling; degrees and shortest paths are general choices, but particular domains could use more suitable encodings, such as 3D distances between atoms.
Conclusion
Graphormer's answer to its title is that Transformers can perform well on graphs, provided structure is supplied. Add degrees to nodes and shortest-path distances and path edges to attention scores, and a standard Transformer beats all GNNs on the largest graph-level dataset of the time.
A year later, GraphGPS pushes PCQM4Mv2 validation MAE lower: GPS-medium with 19.4 million parameters reaches 0.0858, versus Graphormer with 48.3 million at 0.0864—almost tied, with less than half the parameters.
For Transformers on small molecules, add structural encodings first. At the same 100,000 training steps, removing them worsens MAE from 0.13 to 0.23.