NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

K-BERT: Enabling Language Representation with Knowledge Graph

Weijie Liu, Peng Zhou, Zhe Zhao, Zhiruo Wang, Qi Ju, Haotang Deng, Ping Wang

Abstract

Pre-trained language models like BERT excel at general language tasks but often lack domain-specific expertise. K-BERT addresses this by injecting knowledge from Knowledge Graphs (KGs) into the model using a "plug-and-play" framework. By employing a sentence tree structure and a visible matrix masking mechanism, K-BERT integrates expert knowledge while mitigating "knowledge noise" and avoiding the need for expensive domain-specific pre-training. Experimental results across 12 Chinese NLP tasks show that K-BERT significantly outperforms standard BERT, especially in specialized fields such as medicine, finance, and law.

Introduction

While models like BERT are masters of general language, they are often "laypeople" when it comes to specialized fields. For instance, a standard BERT model understands the grammar of a medical record but lacks the context that "Paracetamol" is a treatment for a "cold." Traditionally, making a model a "domain expert" required pre-training from scratch on massive specialized datasets, which is computationally expensive and time-consuming.

Simply appending knowledge to input text leads to two major issues:

  1. Heterogeneous Embedding Space (HES): Word embeddings and KG entity embeddings are often mathematically incompatible.
  2. Knowledge Noise (KN): Excessive external information can overwhelm the original sentence, causing the model to lose track of the actual meaning.

Methodology

K-BERT maintains the core architecture of BERT but introduces three key components to handle knowledge injection: the Knowledge Layer, the Embedding Layer, and the Seeing Layer.

The overall architecture of K-BERT, showing the flow from Knowledge Graph injection to task-specific outputs.

1. The Sentence Tree

Instead of a flat list of words, K-BERT converts input into a Sentence Tree. When an entity like "Tim Cook" is identified, K-BERT queries a KG and branches out with triples such as (Tim Cook, CEO, Apple). This allows the model to enrich the context without losing the original sentence structure.

2. Soft-Position Embedding

Standard BERT uses absolute positions (1, 2, 3...). Inserting knowledge into the middle of a sentence would shift subsequent words and destroy sequential logic. K-BERT uses soft-positions: injected knowledge tokens share the position index of the entity they describe, ensuring the main sentence retains its original sequence.

3. The Seeing Layer (Visible Matrix)

To prevent "Knowledge Noise," K-BERT uses a Visible Matrix ($M$) as a mask in the Transformer's self-attention mechanism. This ensures that tokens in the main sentence can see each other and injected knowledge tokens can see their parent entity, but unrelated branches of knowledge remain invisible to each other.

Detailed mechanism of K-BERT, illustrating the Sentence Tree construction, Soft-Position Embedding, and the resulting Visible Matrix.

Evaluation and Results

The researchers evaluated K-BERT on 12 Chinese NLP tasks across open-domain (sentiment analysis, QA, NER) and specific-domain (Finance, Law, Medicine) categories. They utilized three Knowledge Graphs: CN-DBpedia, HowNet, and a custom MedicalKG.

Key Performance Metrics

K-BERT significantly outperformed standard BERT in specialized fields:

  • Medicine: In the Medicine_NER task, K-BERT achieved an F1 score of 94.2%, compared to Google BERT's 92.5%.
  • Law: In Law_Q&A, K-BERT improved the F1 score from 86.4% to 87.6%.

Ablation Study

The importance of the Visible Matrix was confirmed through ablation studies. In the Law_Q&A task, a version of K-BERT without the visible matrix performed worse than standard BERT, proving that structural masking is essential to prevent knowledge noise from degrading performance.

Conclusion

K-BERT provides an efficient, "plug-and-play" method for empowering language models with expert knowledge without requiring expensive pre-training. While currently focused on Chinese NLP, future work involves optimizing knowledge filtering and extending the framework to other architectures like GPT and XLNet.

Summary updated

BibTeX
@inproceedings{liu2020k,
  author = {Weijie Liu and Peng Zhou and Zhe Zhao and Zhiruo Wang and Qi Ju and Haotang Deng and Ping Wang},
  title = {{K-BERT: Enabling Language Representation with Knowledge Graph}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2020},
  doi = {10.1609/aaai.v34i03.5681},
}
Figure