Back to paper library
Paper notesarXiv 2021NeurIPS 2021

SAN

Hearing the Shape of a Graph

Original paper

Rethinking Graph Transformers with Spectral Attention

Paper authors

Devin Kreuzer · Dominique Beaini · William L. Hamilton · Vincent Létourneau · Prudencio Tossou

Publication
NeurIPS 2021Conference
First submitted to arXiv
arXiv:2106.03893
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{graphlearningsan2021,
  title = {{Rethinking Graph Transformers with Spectral Attention}},
  author = {Devin Kreuzer and Dominique Beaini and William L. Hamilton and Vincent Létourneau and Prudencio Tossou},
  year = {2021},
  eprint = {2106.03893},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2106.03893}
}
On this page

In 1966, mathematician Mark Kac writes “Can One Hear the Shape of a Drum?” A drum's shape determines its vibration frequencies; conversely, can those frequencies alone reveal its shape?

Graphs have frequencies too: their Laplacian eigenvalues and eigenvectors. Devin Kreuzer, Dominique Beaini, William L. Hamilton and colleagues at McGill, Mila and Valence Discovery want a Transformer to “hear” these frequencies and learn each node's position.

They also want to overturn a verdict. Six months earlier, Graph Transformer (GT) tried letting every node see every other, badly hurting all three datasets and leading its authors to favor neighbor-only attention. SAN, Spectral Attention Network, aims to show that full connectivity can work.

Defining the Problem

The authors list message passing's known limitations: WL-bounded graph discrimination, over-smoothing with depth, and over-squashing over distance. All are tied to graph structure directly determining computational routes.

A Transformer lets all positions interact and supplies structure softly, through positional encoding. For graphs, that encoding is the key.

GT already uses Laplacian eigenvectors, but the authors identify unresolved issues:

  • Eigenvalues carry information too. GT supplies eigenvectors without eigenvalues. Sequence frequencies are fixed, so order identifies them; graph frequencies vary, and order alone loses the frequency corresponding to each vector.
  • Repeated eigenvalues. With multiplicity, any combination within the corresponding eigenspace is a valid eigenvector.
  • Variable counts. A graph has at most as many eigenvectors as nodes. Taking a fixed kk means using a smaller fraction on larger graphs.
  • Signs. kk vectors have 2k2^k sign combinations. Random sign flips are manageable for small kk, less so as it grows.

Repeated eigenvalues show up even in 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, and C and D form the bridge. Its normalized Laplacian has two eigenvalues equal to 1.5. The vector (1,−1,0,0,0,0)/2(1,-1,0,0,0,0)/\sqrt2, nonzero only on A and B, the vector (0,0,0,0,1,−1)/2(0,0,0,0,1,-1)/\sqrt2, nonzero only on E and F, and any combination of the two are all eigenvectors for 1.5. The value A receives may be 0.71, 0 or 0.5, depending on which one the library picks. Use that value directly as A's position, and what the model learns may change with the library.

Solving the Problem

SAN has two stages: learn a positional encoding for each node, concatenate it with node features, and feed the result into a fully connected Transformer.

Original teaching diagram: on the left, two triangles joined at C–D have 7 original solid edges and 8 added dashed blue connections. Original-edge weights multiply by 1/(1+γ), added connections by γ/(1+γ); γ=0 uses only original edges, γ=1 treats both equally. On the right, node j's m eigenvalue/eigenvector-entry pairs pass through a linear layer, Transformer and sum to form LPE, concatenated to node features.
Original diagram based on the formulas in §3.1 and §3.3 of the paper.

Learned Positional Encoding

The right half is SAN's learned positional encoding (LPE).

For node jj, take the mm smallest eigenvalues and pair each λi\lambda_i with the corresponding eigenvector entry ϕi,j\phi_{i,j}, producing a length-mm sequence. The authors view eigenvectors as sitting on a frequency axis, with eigenvalues specifying positions along it.

A linear layer maps each pair to kk dimensions. A Transformer encoder attends across the mm elements, then sums them into node jj's kk-dimensional LPE. If the graph has fewer than mm nodes, masked padding fills the remainder.

This addresses three of the four issues. Eigenvalues accompany eigenvectors; sequence lengths can vary, with mm even reaching the largest graph size in the dataset to use the full spectrum; and the Transformer can potentially combine or ignore vectors from repeated eigenspaces. Signs remain unresolved: SAN randomly flips them during training, like GT.

The appendix also describes an edge-wise version, using sign-invariant quantities such as ∣ϕi,j1−ϕi,j2∣|\phi_{i,j_1}-\phi_{i,j_2}| and ϕi,j1ϕi,j2\phi_{i,j_1}\phi_{i,j_2} as pairwise features. It is theoretically stronger but costs up to O(N4)O(N^4) with full connectivity, so the main text uses node-wise LPE.

Full Connectivity, Two Attention Mechanisms

The left half is the main Transformer. Every nonadjacent pair gets an added connection with its own edge embedding. Original edges use Q1Q^1, K1K^1, E1E^1; added connections use Q2Q^2, K2K^2, E2E^2. Hyperparameter γ\gamma balances their weights:

wij={11+γ⋅softmax(w^ij)i,j originally adjacentγ1+γ⋅softmax(w^ij)other node pairsw_{ij}= \begin{cases} \dfrac{1}{1+\gamma}\cdot\mathrm{softmax}\big(\hat w_{ij}\big) & i,j\ \text{originally adjacent}\\[2mm] \dfrac{\gamma}{1+\gamma}\cdot\mathrm{softmax}\big(\hat w_{ij}\big) & \text{other node pairs} \end{cases}

At γ=0\gamma=0, added connections have no weight and attention is neighbor-only; at γ=1\gamma=1, neither set is favored. This knob trades local structure against distant interaction. Residuals, normalization and feedforward layers follow as in GT. Edge representations are not updated: the authors say doing so adds complexity with almost no benefit.

Discussion

How Large Should γ\gamma Be?

The authors sweep γ\gamma on ZINC, PATTERN, CLUSTER and MolHIV. On molecular datasets ZINC and MolHIV, the best log⁡γ\log\gamma lies between −7 and −5, giving added connections only a tiny share. PATTERN and CLUSTER prefer larger γ\gamma.

The explanation is that molecular graphs are small and depend on local structures such as rings and particular bonds; a few sparse-attention layers already span the graph. CLUSTER is semi-supervised, with some true node labels provided in each graph; full connectivity exposes those labeled nodes to everyone at every layer.

Contributions of Connectivity and LPE

Keeping γ\gamma fixed and parameters around 500,000, the authors ablate both designs:

AttentionLPEZINC (MAE ↓)PATTERNCLUSTERMolHIV (ROC-AUC)
SparseNo0.26783.61%75.68%73.46%
SparseYes0.19881.33%75.74%76.61%
Fully connectedNo0.39286.32%76.45%73.84%
Fully connectedYes0.15786.44%76.69%77.57%

Both factors matter. On ZINC, full connectivity without LPE worsens MAE from 0.267 to 0.392, matching GT's observation; with LPE, it becomes best at 0.157. LPE helps molecular datasets most, which the authors connect to substructure detection; PATTERN and CLUSTER are simpler clustering tasks, so its effect is smaller.

Against the Best Models of the Time

Tuned SAN reaches ZINC MAE 0.139, ahead of PNA's 0.142. PATTERN at 86.58% and CLUSTER at 76.69% also lead the table. Fully connected GT has ZINC MAE 0.598 in the same table. To the authors' knowledge, SAN is the first fully connected model to perform well on graph benchmarks.

OGB molecular results are less striking. MolHIV test ROC-AUC is 77.85%, below DGN's 79.70%. The authors note SAN's validation score of 85.30% exceeds PNA's 84.25% and DGN's 84.70%, attributing the outcome to low validation/test correlation on this dataset. MolPCBA average precision is 27.65%, versus DGN's 28.85%.

The Cost

Full attention costs O(N2)O(N^2), LPE O(m2N)O(m^2N), far more than message passing's O(∣E∣)O(|E|), as the authors acknowledge. Later work makes the cost clearer: GraphGPS reports 196 seconds per epoch for roughly 6 million-parameter GPS on ogbg-molpcba, versus 883 seconds for SAN on the same GPU type. On LRGB's COCO-SP, with over 120,000 graphs averaging nearly 480 nodes, SAN fails to converge after 60 hours on an A100.

Conclusion

SAN demonstrates that fully connected Transformers can perform well on graphs, provided positional encodings carefully handle the spectrum and original versus added edges score separately, balanced by γ\gamma. Its ablation also explains GT's observation: full connectivity without good positional encoding makes things worse.

The following year, GraphGPS, also involving Beaini, treats this attention as a replaceable module alongside message passing in each layer. It also offers encodings such as SignNet specifically for sign ambiguity.

For graphs with only tens of nodes, SAN is worth trying. As graphs grow, quadratic computation becomes the first obstacle.