Back to paper library
Paper notesarXiv 2020DLG-AAAI 2021

Graph Transformer

Fully Connected, Worse Results

Original paper

A Generalization of Transformer Networks to Graphs

Paper authors

Vijay Prakash Dwivedi · Xavier Bresson

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}
}
On this page

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

Figure 1 of the paper: the left diagram is a Graph Transformer layer. Node features with Laplacian positional encoding λ undergo Q, K, V projections, multiplication, scaling, softmax and summation, then Add&Norm, FFN and Add&Norm, repeated L times. The edge-feature version on the right adds e_ij: attention scores multiply E-projected edge features, and edge representations have their own FFN and normalization.
Figure 1 from Dwivedi and Bresson, taken from PDF page 2. The original caption is omitted.

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:

h^iℓ+1=Ohℓ ∥k=1H(∑j∈Niwijk,ℓVk,ℓhjℓ),wijk,ℓ=softmaxj(Qk,ℓhiℓ⋅Kk,ℓhjℓdk)\hat h_i^{\ell+1}=O_h^\ell\,\Big\Vert_{k=1}^{H}\Big(\sum_{j\in\mathcal N_i}w_{ij}^{k,\ell}V^{k,\ell}h_j^\ell\Big),\qquad w_{ij}^{k,\ell}=\mathrm{softmax}_j\Big(\frac{Q^{k,\ell}h_i^\ell\cdot K^{k,\ell}h_j^\ell}{\sqrt{d_k}}\Big)

Ni\mathcal N_i is node ii's neighborhood, and ∥\Vert concatenates HH 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:

Δ=I−D−1/2AD−1/2=U⊤ΛU\Delta=I-D^{-1/2}AD^{-1/2}=U^\top\Lambda U

The authors take the kk smallest nontrivial eigenvectors, skipping the one with eigenvalue 0. Each node's entries in these vectors form its positional encoding λi\lambda_i. 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 k=2k=2. 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 kk vectors there are 2k2^k 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 Ek,ℓeijℓE^{k,\ell}e_{ij}^\ell, 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:

DatasetNeighbors onlyAll nodes
ZINC (MAE ↓)0.2260.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 encodingZINC (MAE ↓)CLUSTERPATTERN
None0.26472.14%83.95%
Laplacian0.22673.17%84.81%
WL (Graph-BERT)0.26770.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:

ModelZINC (MAE ↓)CLUSTERPATTERN
GCN0.36768.50%71.89%
GAT0.38470.59%78.27%
GatedGCN0.21476.08%86.51%
GT0.22673.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.