KnowFormer: Revisiting Transformers for Knowledge Graph Reasoning

Junnan Liu, Qianren Mao, Weifeng Jiang, Jianxin Li

Abstract

Knowledge graph reasoning plays a vital role in various applications and has garnered considerable attention. Recently, path-based methods have achieved impressive performance. However, they may face limitations stemming from constraints in message-passing neural networks, such as missing paths and information over-squashing. In this paper, we revisit the application of transformers for knowledge graph reasoning to address the constraints faced by path-based methods and propose a novel method KnowFormer. KnowFormer utilizes a transformer architecture to perform reasoning on knowledge graphs from the message-passing perspective, rather than reasoning by textual information like previous pretrained language model based methods. Specifically, we define the attention computation based on the query prototype of knowledge graph reasoning, facilitating convenient construction and efficient optimization. To incorporate structural information into the self-attention mechanism, we introduce structure-aware modules to calculate query, key, and value respectively. Additionally, we present an efficient attention computation method for better scalability. Experimental results demonstrate the superior performance of KnowFormer compared to prominent baseline methods on both transductive and inductive benchmarks.

Introduction

Knowledge Graph (KG) reasoning—the task of predicting missing links between entities—has traditionally been dominated by two paradigms: embedding-based methods and path-based methods (such as Graph Neural Networks). While path-based methods are powerful, they face two significant structural bottlenecks: Missing Paths and Information Over-squashing.

The core bottlenecks of traditional path-based methods: Missing Paths in sparse graphs and Information Over-squashing in deep networks.

Missing paths occur when a graph is sparse and no physical edge sequence exists between related entities, causing models to fail. Over-squashing happens as GNNs increase in depth to capture long-range dependencies; the information from distant nodes is "squashed" into a single vector, leading to a loss of critical detail. KnowFormer addresses these by treating the Transformer's attention mechanism as a global message-passing tool that can create "virtual paths" between any two entities.

Methodology

KnowFormer moves beyond simple text-encoding by embedding structural message-passing directly into the attention mechanism. Instead of standard linear projections for Query (Q), Key (K), and Value (V), KnowFormer introduces specialized structure-aware modules.

Structure-Aware Attention

The architecture utilizes two primary components to calculate attention:

  • Q-RMPNN (Query Function): A relational message-passing network that encodes an entity’s local neighborhood to determine its relevance to a specific query.
  • V-RMPNN (Value Function): Encodes pairwise structural information into the values being passed, ensuring the model maintains spatial awareness of the graph topology.
The KnowFormer architecture, illustrating how Q-RMPNN and V-RMPNN modules generate structure-aware Query, Key, and Value vectors.

Scalability and Efficiency

Standard Transformers suffer from quadratic complexity ($O(N^2)$), which is prohibitive for large KGs. KnowFormer utilizes a kernel-based approximation (a first-order Taylor expansion of the exponential kernel) to reduce this to linear complexity ($O(N)$), allowing it to scale to graphs with hundreds of thousands of entities.

Key Results

KnowFormer was evaluated on standard transductive benchmarks (FB15k-237, WN18RR, NELL-995, and YAGO3-10) and various inductive reasoning tasks.

  • State-of-the-Art Performance: KnowFormer achieved SOTA results on nearly all benchmarks. For instance, on the large-scale YAGO3-10 dataset, it outperformed the best baselines by a significant margin in Mean Reciprocal Rank (MRR).
  • Robustness to Sparsity: In experiments where facts were randomly removed, KnowFormer’s performance remained stable, whereas traditional path-based models like NBFNet saw sharp declines.
  • Handling Long-Range Dependencies: Unlike GNNs, KnowFormer's performance did not degrade as reasoning path lengths increased, proving its effectiveness in mitigating the over-squashing problem.

Conclusion

KnowFormer demonstrates that Transformers can be effectively adapted for structural KG reasoning by replacing standard attention with structure-aware message passing. By overcoming the limitations of physical paths and information bottlenecks, it provides a scalable and robust framework for complex link prediction tasks. Future work will focus on extending this architecture to full-inductive scenarios involving entirely new relations.

Summary updated

BibTeX
@inproceedings{liu2024knowformer,
  author = {Junnan Liu and Qianren Mao and Weifeng Jiang and Jianxin Li},
  title = {{KnowFormer: Revisiting Transformers for Knowledge Graph Reasoning}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=EncFNR3hxM},
}
Figure