A Generalization of Transformer Networks to Graphs
- Publication
- AAAI Workshop on Deep Learning on Graphs 2021Workshop
- First submitted to arXiv
- arXiv:2012.09699
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{graphlearninggraphtransformer2020,
title = {{A Generalization of Transformer Networks to Graphs}},
author = {Vijay Prakash Dwivedi and Xavier Bresson},
year = {2020},
eprint = {2012.09699},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2012.09699}
}
When a Transformer processes a sentence, every word looks at every other word. Viewed another way, the sentence is a fully connected graph: words are nodes, with an edge between every pair.
If sentences are already graphs, can real graphs be fed straight into a Transformer?
Vijay Prakash Dwivedi and Xavier Bresson at Nanyang Technological University argue against a direct transplant. A sentence has no given connectivity and usually only tens or hundreds of words, making full connectivity reasonable and affordable. A graph brings its own structure and may have millions of nodes; connecting everything wastes that structure and costs too much.
Their late-2020 Graph Transformer (GT below) is an early attempt to generalize Transformers to arbitrary graphs. SAN six months later, and GraphGPS another year later, both use it as a starting comparison.
Defining the Problem
The authors split the problem into two parts.
Sparsity. Earlier graph Transformers, such as Li et al. (2019), let every node see all nodes for global information. The authors argue that this discards sparse graph connectivity, a valuable inductive bias. Positional encodings or virtual nodes could supply global information instead.
Positional encoding. Sentence words have an order, encoded by sinusoids in a Transformer. Graphs have no canonical node order and can have symmetries, making unique positions hard to define. The authors see missing positional information as one reason for GAT's limited performance despite neighbor-only attention.
The paper tests whether neighbor-only attention with suitable positional encoding can produce a general, effective graph Transformer.
Solving the Problem

The left is the basic version, the right adds edge features. There are three changes from the original Transformer.
Attention Only among Neighbors
One GT layer's attention is almost identical to Transformer attention, differing only in the summation range:
is node 's neighborhood, and concatenates heads. The Transformer sums over all positions, GT only over neighbors. Residuals, normalization and a feedforward network follow as usual.
Laplacian Positional Encoding
Positional encoding uses Laplacian eigenvectors:
The authors take the smallest nontrivial eigenvectors, skipping the one with eigenvalue 0. Each node's entries in these vectors form its positional encoding . After a linear projection, it is added to node features only at the input, not at intermediate layers.
Why eigenvectors? They generalize NLP's sinusoidal encodings to graphs, the authors argue, bringing distance information so nearby nodes get nearby encodings.
Try a small graph of two triangles joined by a bridge. A, B and C are on the left, D, E and F on the right, C and D form the bridge, and . The first eigenvector separates the left and right groups, positive on the left triangle and negative on the right; the second separates the bridge nodes C and D from the four outer nodes. A and B receive exactly the same encoding because they are symmetric in the graph, and a purely structural encoding cannot tell them apart.
Eigenvectors have another problem: their sign is arbitrary. Multiply the whole vector by −1 and it is still an eigenvector. The same graph can receive two opposite positional encodings, and with vectors there are combinations. On a different graph, a rule such as “make a certain node positive” may not even make sense. GT randomly flips the signs during training so the model learns not to care.
Edge Features and Normalization
Molecular bond types and knowledge-graph relations live on edges. GT's edge-feature version first computes neighboring attention scores, multiplies them by projected edge features , then applies softmax. Edge representations also have their own feedforward networks and update layer by layer.
The final change replaces Transformer LayerNorm with BatchNorm, giving faster training and better generalization.
Discussion
Experimental Setup
The authors use three Benchmarking GNNs datasets:
- ZINC: molecular graph regression on a 12,000-molecule subset, predicting a solubility-related property with MAE, lower being better. Bond types are edge features, so the edge-feature GT is used.
- PATTERN: 14,000 stochastic-block-model graphs, with binary node classification.
- CLUSTER: 12,000 generated graphs, assigning each node to one of 6 clusters.
Models have 10 layers, 8 attention heads and around 500,000 parameters. Each experiment uses 4 random seeds.
What Happens with Full Connectivity?
The authors run the same model on the original graph and a fully connected graph. With BatchNorm and Laplacian encodings:
| Dataset | Neighbors only | All nodes |
|---|---|---|
| ZINC (MAE ↓) | 0.226 | 0.598 |
| CLUSTER (accuracy ↑) | 73.17% | 27.12% |
| PATTERN (accuracy ↑) | 84.81% | 54.94% |
All three deteriorate dramatically. PATTERN has two classes, so 54.94% is near chance; CLUSTER has 6, so 27.12% is only modestly above chance at 16.7%. The fully connected ZINC version omits edge features, deliberately testing a setting with no sparse structure.
The authors conclude that sparse connectivity is a crucial inductive bias on arbitrary graphs.
Positional Encoding and BatchNorm
Across four combinations of LayerNorm/BatchNorm and with/without positional encoding, BatchNorm plus Laplacian encoding wins on all three datasets. Some settings vary enormously: on PATTERN, LayerNorm without encodings gives 73.14% ± 13.63% across four seeds.
The authors also compare Graph-BERT's WL positional encodings:
| Positional encoding | ZINC (MAE ↓) | CLUSTER | PATTERN |
|---|---|---|---|
| None | 0.264 | 72.14% | 83.95% |
| Laplacian | 0.226 | 73.17% | 84.81% |
| WL (Graph-BERT) | 0.267 | 70.79% | 75.49% |
WL encoding reaches 97.03% training accuracy on PATTERN but only 75.49% test accuracy; the authors suspect overfitting on generated graphs.
Comparison with GNNs
Finally, GNNs under the same 500,000-parameter budget:
| Model | ZINC (MAE ↓) | CLUSTER | PATTERN |
|---|---|---|---|
| GCN | 0.367 | 68.50% | 71.89% |
| GAT | 0.384 | 70.59% | 78.27% |
| GatedGCN | 0.214 | 76.08% | 86.51% |
| GT | 0.226 | 73.17% | 84.81% |
GT clearly beats GCN and attention-based GAT but remains slightly behind GatedGCN on all three. The authors position it as a stronger attention-based GNN baseline rather than the best model of the time.
Conclusion
GT's answer to whether graphs should be fully connected is: not yet. Neighbor-only attention works best together with Laplacian positional encoding and BatchNorm, while full connectivity lags badly on all three datasets.
This conclusion does not last long. Six months later, SAN makes full connectivity work with more careful positional encoding and adjustable weighting for added connections. Another year later, GraphGPS, with Dwivedi also among its authors, puts neighbor-only message passing and all-node attention in the same layer.
Before using Laplacian eigenvectors as positional encodings, decide how to handle signs. Otherwise the same graph can receive different encodings every time.