What Does BERT Look At? An Analysis of BERT's Attention

Kevin Clark, Urvashi Khandelwal, Omer Levy, Christopher D. Manning

Abstract: Large pre-trained neural networks such as BERT have had great recent success in NLP, motivating a growing body of research investigating what aspects of language they are able to learn from unlabeled data. Most recent analysis has focused on model outputs (e.g., language model surprisal) or internal vector representations (e.g., probing classifiers). Complementary to these works, we propose methods for analyzing the attention mechanisms of pre-trained models and apply them to BERT. BERT's attention heads exhibit patterns such as attending to delimiter tokens, specific positional offsets, or broadly attending over the whole sentence, with heads in the same layer often exhibiting similar behaviors. We further show that certain attention heads correspond well to linguistic notions of syntax and coreference. For example, we find heads that attend to the direct objects of verbs, determiners of nouns, objects of prepositions, and coreferent mentions with remarkably high accuracy. Lastly, we propose an attention-based probing classifier and use it to further demonstrate that substantial syntactic information is captured in BERT's attention.

Introduction

While BERT has revolutionized Natural Language Processing (NLP), it has largely remained a "black box." We know it works exceptionally well, but understanding how it processes the structure of language is crucial for model interpretability. This research dives deep into the model's internal mechanics to see if its "attention" mechanisms align with human linguistic concepts.

Methodology

The study analyzes the 144 attention heads in BERT-base (12 layers, 12 heads each) using several techniques:

  1. Individual Head Probing: Treating each head as a classifier to see if its maximum attention corresponds to specific grammatical relations.
  2. [SEP] Token Analysis: Investigating the high volume of attention directed toward delimiter tokens using gradient-based feature importance.
  3. Probing Classifiers: Building "attention-only" models to quantify the total syntactic information available across multiple heads.
  4. Clustering: Using Jensen-Shannon Divergence to measure behavioral similarity between heads across different layers.

General Attention Patterns

BERT's attention heads do not attend to words randomly. Instead, they exhibit distinct, repeatable patterns. Some heads focus on the immediate next or previous token, while others distribute attention broadly across the entire sentence.

Examples of different attention patterns found in BERT's heads, including broad attention, next-token attention, and attention to delimiters.

Lower layers tend to contain more "broad" heads, while middle layers show higher specialization. Interestingly, heads within the same layer often exhibit similar behaviors, suggesting a degree of redundancy in the model's architecture.

The [SEP] Token as a "No-Op"

A striking observation is that a significant portion of attention (often exceeding 50%) is directed toward the [SEP] (separator) token.

Extracted figure from page 2 of What Does BERT Look At? An Analysis of BERT's Attention

The researchers hypothesize that when an attention head’s specific function (e.g., finding a direct object) is not applicable to a particular word, it "parks" its attention on [SEP] as a null operation. Gradient analysis supports this "no-op" theory, showing that attention to [SEP] has minimal impact on the final model output compared to attention on substantive words.

Syntactic and Semantic Specialization

Without explicit supervision, specific heads naturally emerge as specialists for linguistic tasks. The study found that individual heads could identify grammatical relations with high accuracy:

  • Determiners: 94.3% accuracy in linking "the" to its noun.
  • Direct Objects: 86.8% accuracy in linking verbs to their objects.
  • Prepositional Objects: 76.3% accuracy.

Beyond syntax, the model also captures semantic relationships. One specific head (Head 5-4) achieved 65.1% accuracy in coreference resolution—linking pronouns to their antecedents—vastly outperforming simple string-matching baselines (27%).

Conclusion

BERT’s self-supervised training on raw text implicitly teaches it complex syntax and coreference. While no single head is a "syntax master," the collective attention maps provide a nearly complete representation of English grammar. These findings suggest that BERT's success is partly due to its ability to recover and utilize the underlying structural rules of language from unlabeled data.

Summary updated

BibTeX
@inproceedings{clark2019what,
  author = {Kevin Clark and Urvashi Khandelwal and Omer Levy and Christopher D. Manning},
  title = {{What Does BERT Look At? An Analysis of BERT's Attention}},
  booktitle = {BlackboxNLP @ ACL},
  year = {2019},
  doi = {10.18653/v1/w19-4828},
}
Figure