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

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

TL;DR

This paper dives into BERT's "attention heads" to understand what linguistic information it learns during self-supervised pre-training. By analyzing these attention patterns, the authors reveal that BERT implicitly learns syntactic structures and even coreference resolution, suggesting that powerful linguistic knowledge can emerge without explicit supervision.

What Problem Does This Paper Address?

Large pre-trained language models like BERT have achieved remarkable success in Natural Language Processing (NLP). However, the inner workings of these models, particularly how they achieve such high performance, remain largely a mystery. While previous research has focused on model outputs or internal vector representations, this paper aims to shed light on the attention mechanisms within BERT. Specifically, it asks: what aspects of language does BERT's attention pay attention to, and can these attention patterns reveal learned linguistic knowledge?

Methods

The authors propose and utilize several methods to analyze BERT's attention:

  • Surface-Level Pattern Analysis: They examine general patterns in how attention heads distribute their weights, looking for common behaviors like attending to specific tokens (e.g., [SEP], [CLS], punctuation) or fixed positional offsets.
  • Linguistic Probing: They treat individual attention heads as simple, untrained classifiers. For a given word, the head predicts which other word it attends to most strongly. This is then evaluated on tasks like dependency parsing and coreference resolution.
  • Attention-Based Probing Classifiers: To get a more holistic view of syntactic understanding, they develop novel probing classifiers that combine information from multiple attention heads. These classifiers are trained to predict syntactic relationships.
  • Clustering Attention Heads: They measure the similarity between attention heads using Jensen-Shannon divergence and visualize these relationships using multidimensional scaling to see if heads can be grouped by behavior.

Datasets

  • Wikipedia Segments: Used for extracting attention maps from BERT-base.
  • Penn Treebank (Wall Street Journal portion): Annotated with Stanford Dependencies, used for evaluating dependency parsing capabilities.
  • CoNLL-2012 dataset: Used for evaluating coreference resolution.

Key Insights and Ideas

  • [SEP] Token as a "No-Op": A significant portion of BERT's attention, especially in middle layers, focuses on the [SEP] token. The authors hypothesize this acts as a "no-op" or placeholder, particularly when a head's specific function isn't applicable to the current context. This is supported by gradient-based analysis showing minimal impact on model output when attention to [SEP] changes.
  • Specialized Attention Heads: Individual attention heads show remarkable specialization. For instance, specific heads accurately identify direct objects of verbs, determiners of nouns, and possessive pronouns, achieving over 75% accuracy on these specific linguistic relations.
  • Emergent Syntax and Coreference: The ability of attention heads to identify syntactic relations and even perform coreference resolution emerges purely from self-supervised training on unlabeled text. This suggests that BERT learns these complex linguistic structures implicitly.
  • Layer-wise Behavior: Attention heads within the same layer tend to exhibit similar behaviors, and these behaviors can be clustered. This indicates a structured organization of attention mechanisms across layers.
  • Attention vs. Vector Representations: The authors' attention-based probing classifier achieves a UAS of 77, which is comparable to other methods that probe BERT's vector representations. This suggests that attention maps capture a substantial amount of syntactic information, potentially as much as or even more than the hidden states for certain tasks.

Key Results

  • Dependency Parsing: An attention-based probing classifier achieved 77 UAS on dependency parsing, demonstrating BERT's attention captures significant syntactic information.
  • Specific Syntactic Relations: Several heads achieved high accuracy on specific relations:
  • Head 8-10: 86.8% accuracy for direct objects.
  • Head 8-11: 94.3% accuracy for determiners.
  • Head 9-6: 76.3% accuracy for prepositions attending to their objects.
  • Coreference Resolution: One attention head achieved 83% accuracy on coreference resolution for nominal mentions, outperforming a string-matching baseline by over 10 percentage points.

Limitations and Future Work

  • Interpreting "No-Op": While the [SEP] token's role as a potential "no-op" is hypothesized, its exact function and implications require further investigation.
  • Head Combinations: While the paper proposes probing combinations of heads, the full potential of these combinations for understanding BERT's overall linguistic knowledge is still being explored.
  • Pruning and Importance: The paper acknowledges that not all heads are equally important, and some might be pruned without significant performance loss, suggesting that a subset of heads might be responsible for most of the learned linguistic knowledge. Future work could focus on identifying and understanding these critical heads.
  • Beyond Syntax: While the paper focuses on syntax and coreference, future research could explore how BERT's attention mechanisms capture other linguistic phenomena like semantics, discourse, and pragmatics.

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