Abstract
We propose a novel positional encoding for learning graph on Transformer architecture. Existing approaches either linearize a graph to encode absolute position in the sequence of nodes, or encode relative position with another node using bias terms. The former loses preciseness of relative position from linearization, while the latter loses a tight integration of node-edge and node-topology interaction. To overcome the weakness of the previous approaches, our method encodes a graph without linearization and considers both node-topology and node-edge interaction. We name our method Graph Relative Positional Encoding dedicated to graph representation learning. Experiments conducted on various graph datasets show that the proposed method outperforms previous approaches significantly.
Introduction: The Spatial Awareness Problem in Graph Transformers
Transformers have revolutionized Natural Language Processing and Computer Vision, yet they face a significant "spatial awareness" challenge when applied to graph-structured data. Because the standard self-attention mechanism is permutation equivariant—treating all inputs as an unordered set—the model must be explicitly informed of the underlying graph structure.
In natural language, positional encodings (PE) typically denote sequential order (e.g., "1st word," "2nd word"). However, graphs lack a natural starting point. Previous attempts to address this generally fell into two categories:
- Linearization: Forcing the graph into a sequence using techniques like Laplacian eigenvectors. This often sacrifices the precision of relative distances between nodes.
- Relative Biases: Adding a static penalty or bonus to attention scores based on hop-distance (e.g., Graphormer). While an improvement, this approach treats all nodes at a specific distance identically, ignoring how specific node features might interact with the topology.
Methodology: Graph Relative Positional Encoding (GRPE)
The core of GRPE is the introduction of learnable encoding vectors: Topology Encodings (representing shortest path distances) and Edge Encodings (representing connection types). Unlike previous methods that use static biases, GRPE allows structural information to interact dynamically with node features.
The architecture introduces two primary innovations:
- Node-Aware Attention: Instead of calculating attention solely on node features ($q \cdot k$), GRPE includes terms where node features interact with structural encodings. A node's "Query" can effectively ask about the topological context of its neighbors, and the "Key" responds based on its own position.
- Graph-Encoded Value: Most models use structural info only for attention weights. GRPE injects structural encodings directly into the Value vector. Consequently, the information propagated to subsequent layers is explicitly aware of the relative distance and edge type it traversed.
To maintain efficiency, the authors utilize a pre-computation trick for dot products between node features and the finite set of structural encodings, keeping the complexity manageable.
Key Insights: Dynamic Interaction and Global Aggregation
A critical insight of GRPE is that structural information is most potent when it is feature-dependent. By allowing node features ($q, k$) to multiply with structural encodings, the model learns complex spatial relationships. Furthermore, the inclusion of a "Virtual Node" connected to all other nodes proves essential for global context.
As shown in the attention map visualizations, the Transformer initially focuses on local neighborhoods. As the network deepens, it increasingly utilizes the virtual node to aggregate a global summary of the entire graph, effectively transitioning from local feature extraction to global representation.
Experimental Results
GRPE was evaluated on major benchmarks from the Open Graph Benchmark (OGB) and other standard sets, consistently matching or exceeding state-of-the-art (SOTA) performance:
- ZINC: Achieved a Mean Absolute Error (MAE) of 0.094, a significant improvement over the previous SOTA (EGT at 0.108).
- MolHIV: Reached an ROC-AUC of 81.39%, outperforming Graphormer with a more parameter-efficient architecture.
- PATTERN/CLUSTER: Achieved SOTA weighted accuracy (87.020% and 81.586% respectively), demonstrating efficacy for node-level classification tasks.
Conclusion and Future Work
GRPE demonstrates that a tight integration of node features and graph topology within the attention mechanism is superior to simple biases or linearization. While currently reliant on shortest path distances—which may pose challenges for extremely large graphs—the framework is highly extensible. Future work may involve incorporating 3D molecular coordinates (angles and distances) to further enhance representation learning for chemical structures.
Reference: Park, W., et al. (2022). GRPE: Relative Positional Encoding for Graph Transformer. arXiv:2201.12787v3.
Summary updated