Abstract
Knowledge graph (KG) embeddings have been a mainstream approach for reasoning over incomplete KGs. However, limited by their inherently shallow and static architectures, they can hardly deal with the rising focus on complex logical queries, which comprise logical operators, imputed edges, multiple source entities, and unknown intermediate entities. In this work, we present the Knowledge Graph Transformer (kgTransformer) with masked pre-training and fine-tuning strategies. We design a KG triple transformation method to enable Transformer to handle KGs, which is further strengthened by the Mixture-of-Experts (MoE) sparse activation. We then formulate the complex logical queries as masked prediction and introduce a two-stage masked pre-training strategy to improve transferability and generalizability. Extensive experiments on two benchmarks demonstrate that kgTransformer can consistently outperform both KG embedding-based baselines and advanced encoders on nine in-domain and out-of-domain reasoning tasks. Additionally, kgTransformer can reason with explainability via providing the full reasoning paths to interpret given answers.
Introduction
Knowledge Graphs (KGs) such as Wikidata and Freebase serve as the backbone for numerous downstream applications, from search engines to recommendation systems. Despite their scale, these graphs are often incomplete, missing critical relations between entities. The challenge of "reasoning" over these graphs involves answering complex logical queries—specifically Existential Positive First-Order (EPFO) logic—that require navigating multiple hops and handling logical operators like conjunction (AND) and disjunction (OR).
Traditional Knowledge Graph Embeddings (KGEs) have served as the standard solution, but they possess inherent limitations. They are typically "shallow" and "static," often restricted to a rigid, unidirectional reasoning flow. Furthermore, they struggle to generalize to query structures not seen during training and operate as "black boxes," providing answers without interpretable reasoning paths. To address these gaps, we introduce kgTransformer, a high-capacity model that adapts the Transformer architecture for complex KG reasoning.
The kgTransformer Architecture
Adapting the Transformer—originally designed for sequential natural language—to graph-structured data requires significant innovation. kgTransformer introduces two primary architectural enhancements:
Triple Transformation
Standard Transformers are designed to process node features, but KGs are defined by their edges (relations). To bridge this gap, kgTransformer employs a Triple Transformation method. Every relation in the KG is transformed into a "relation-node." This converts the KG into a directed graph where all information—both entities and relations—is stored in nodes. This transformation allows the model to utilize standard self-attention mechanisms to capture the intricate dependencies between entities and the relations that connect them.
Mixture-of-Experts (MoE)
KGs are massive, and modeling them requires high capacity. However, a standard dense Transformer would face a linear explosion in computational cost. kgTransformer utilizes Mixture-of-Experts (MoE) in its feed-forward layers. By using sparse activation, the model can maintain a massive parameter count while only activating a small, relevant subset of neurons for any given query. This approach exploits the inherent sparsity of KG reasoning—where only a fraction of the model's knowledge is relevant to a specific query—allowing for a 16x increase in capacity with only a marginal increase in compute time (approximately 11-38%).
Two-Stage Masked Pre-training
To improve the model's ability to generalize across different query types, kgTransformer utilizes a two-stage masked pre-training strategy:
- Stage 1: Dense Initialization: The model is first trained to learn general graph structures by predicting masked entities within large, randomly sampled subgraphs. This provides a foundational understanding of the KG's global topology.
- Stage 2: Sparse Refinement: The model is then refined on "meta-graphs" that mimic the small, sparse structures of actual logical queries. This stage bridges the gap between general graph knowledge and the specific requirements of complex reasoning tasks.
By formulating logical queries as graphs with "masked" target and intermediate entities, the model can perform bidirectional reasoning. Unlike previous "left-to-right" models, kgTransformer processes the entire query graph simultaneously, allowing information to flow from the target back to intermediate steps and vice versa.
Experimental Evaluation
The model was evaluated on two gold-standard benchmarks: FB15k-237 (a subset of Freebase) and NELL995. The researchers tested the model on nine different query types, including "in-domain" shapes and "out-of-domain" shapes that were entirely unseen during training.
Key Results
- State-of-the-Art Performance: kgTransformer consistently outperformed KGE-based baselines (such as GQE and Q2B) and advanced encoders (like CQD). It achieved a relative improvement of 6.4% to 12.1% across the benchmarks.
- Generalization: The model demonstrated exceptional performance on out-of-domain queries, suggesting that the pre-training strategy successfully teaches the model the underlying logic of the graph rather than just memorizing specific patterns.
- Explainability: Because the model treats reasoning as masked prediction, it can provide full reasoning paths. For a query like "What musical instruments did Minnesota-born Nobel Prize winners play?", the model doesn't just return the instrument; it can identify the specific intermediate entities (the people and the state) that lead to the answer.
Discussion and Future Work
While kgTransformer represents a significant step forward, several avenues for future research remain. The model currently shows slightly lower performance on simple "1-path" (single-hop) queries compared to specialized KG completion models, suggesting that the multi-layer Transformer architecture may be "overkill" for basic links.
Additionally, the current model is pre-trained on a single KG. Future work could explore "cross-KG" pre-training to transfer knowledge between domains, such as from a general-purpose KG to a specialized medical or scientific one. Finally, integrating textual labels of entities and relations (using models like BERT or GPT) could further enhance the model's reasoning depth by incorporating semantic information beyond pure graph structure.
Summary updated