Using Prior Knowledge to Guide BERT's Attention in Semantic Textual Matching Tasks

Tingyu Xia, Yue Wang, Yuan Tian, Yi Chang · DOI

Abstract

Semantic Textual Similarity (STS) is a critical task in natural language understanding. While BERT-based models have achieved state-of-the-art results, they often require massive data or expensive pre-training to capture fine-grained semantic relationships like synonymy. This paper proposes BERT-Sim, a method to "steer" BERT’s internal attention mechanism by directly injecting word-similarity knowledge from WordNet. By modifying the multi-head attention in BERT’s initial layer with a WordNet-derived similarity matrix, the model achieves superior performance on semantic matching tasks, particularly in low-resource settings. Furthermore, BERT-Sim is twice as fast as traditional data augmentation methods, offering a more efficient alternative for knowledge enhancement.

1. Introduction

Determining if two sentences are semantically equivalent is a fundamental challenge in NLP. Despite the success of Transformer-based models like BERT, they are frequently treated as "black boxes." When these models fail to capture nuances such as synonymy, the standard response is often brute-force data augmentation or computationally intensive pre-training. This research investigates a more targeted approach: identifying BERT's specific "knowledge deficiencies" and injecting the missing information directly into the model's architecture rather than relying on the model to learn it through sheer volume of data.

2. Methodology

2.1 Layer-wise Analysis and Motivation

To determine where BERT lacks information, the authors conducted pilot studies analyzing performance across different layers. As shown in Figure 3, BERT’s middle layers (specifically layer 6) are the most "informative" for semantic matching, while the initial layers are relatively "uninformed." This suggests that the first layer acts as a broad filter that could benefit most from external guidance.

Average F1 performance across BERT layers, demonstrating that middle layers are most informative for matching.

2.2 Attention Steering with BERT-Sim

The core of the proposed BERT-Sim model is the modification of the Multi-Head Attention mechanism. Instead of relying solely on learned query-key interactions, the authors introduce a Word Similarity Matrix ($S$) derived from WordNet. This matrix assigns a score between 0 and 1 to word pairs based on their semantic relatedness (e.g., "happy" and "glad" receive a high score).

The attention scores in the first layer are modified using a Hadamard (element-wise) product: $$scores = (QK^T \odot S)$$

This "surgical" intervention forces the model to prioritize word pairs that are known to be related, providing a helpful inductive bias during the early stages of processing. This architecture is illustrated in Figure 4.

Architecture of the modified attention mechanism incorporating an external similarity matrix.

3. Experimental Setup

The model was evaluated on four major benchmarks:

  • MRPC & STS-B: Small datasets consisting of news paraphrases and captions.
  • QQP & TwitterURL: Large-scale datasets for duplicate detection and social media paraphrasing.

4. Key Results and Insights

4.1 Performance in Data-Scarce Scenarios

BERT-Sim showed its greatest strength when training data was limited. On the MRPC dataset, it achieved an 88.3 F1 score, outperforming standard BERT-base (87.0). The performance gap was most pronounced when training on only 10-20% of the available data, indicating that external knowledge can effectively compensate for a lack of examples.

4.2 Efficiency and Precision

Unlike data augmentation, which increases training time by expanding the dataset, BERT-Sim maintains the original dataset size. In head-to-head tests on the QQP dataset, BERT-Sim achieved a 2x speedup over augmentation methods (3,206s vs. 6,372s per epoch). Furthermore, the model significantly reduced "false positives" by better distinguishing between true synonyms and unrelated words that happen to appear in similar contexts.

5. Discussion and Conclusion

While BERT-Sim offers a powerful and efficient way to enhance Transformer models, it faces limitations regarding polysemy, as the current similarity matrix is static and does not account for context-dependent meanings. Future work could involve integrating dynamic, context-aware knowledge bases. Nevertheless, this research demonstrates that "steering" attention with prior knowledge is a viable, efficient alternative to brute-force scaling, particularly for specialized or low-resource domains.

Summary updated

BibTeX
@inproceedings{xia2021using,
  author = {Tingyu Xia and Yue Wang and Yuan Tian and Yi Chang},
  title = {{Using Prior Knowledge to Guide BERT's Attention in Semantic Textual Matching Tasks}},
  booktitle = {The Web Conference},
  year = {2021},
  doi = {10.1145/3442381.3449988},
}
Figure