Attention in Natural Language Processing

Andrea Galassi, Marco Lippi, Paolo TorroniarXiv 2019 · DOI

This paper provides a comprehensive survey and the first formal taxonomy of attention mechanisms in Natural Language Processing (NLP). It introduces a unified mathematical model to categorize the diverse landscape of attentive architectures—from the seminal work in machine translation to modern powerhouses like Transformers and BERT—offering a structured way to understand how neural networks learn to "focus."

TL;DR

The authors propose a unified framework and four-dimensional taxonomy to categorize the explosion of attention mechanisms in NLP. By standardizing terminology and mathematical notation, the paper provides a roadmap for researchers to compare existing models and design new ones that effectively combine neural learning with prior knowledge.

What Problem Does This Paper Address?

Since its introduction, the "attention" mechanism has become ubiquitous in NLP, enabling models to dynamically weigh the relevance of different input parts. However, the field has moved so fast that a systematic overview was missing. Researchers often use inconsistent terminology—for example, the term "context vector" has three distinct meanings across different seminal papers.

This lack of a unified language makes it difficult to compare architectures or understand the underlying principles that make one attention variant better than another for a specific task. This paper fills that gap by providing a formal, unified model and a taxonomy that organizes the vast body of literature.

Methods

The authors define a Unified Attention Model that breaks the mechanism down into a sequence of operations involving three main components: Keys ($K$), Queries ($q$), and Values ($V$).

The core attention model mapping keys and a query to attention weights.

Building on this, they propose a taxonomy based on four orthogonal dimensions:

  1. Input Representation: How the keys and values are generated (e.g., raw embeddings vs. RNN hidden states). This includes Self-Attention (where the query comes from the input itself) and Hierarchical-Input models that process data at multiple levels, like words then sentences.
  2. Compatibility Function: The mathematical operation used to score the match between a query and a key (e.g., dot-product, additive, or similarity-based).
  3. Distribution Function: How these scores are normalized into weights (e.g., Softmax for "soft" attention, or Sparsemax to ignore irrelevant elements entirely).
  4. Multiplicity: Whether the model produces multiple sets of weights (Multi-head attention) or handles multiple interacting input sequences (Co-attention).
The general attention model including annotation functions for inputs and queries.

Datasets

As a survey paper, it does not introduce new datasets. Instead, it categorizes research across a wide spectrum of NLP tasks, including:

  • Machine Translation (e.g., WMT datasets)
  • Sentiment Analysis (e.g., IMDB, Yelp)
  • Question Answering (e.g., SQuAD)
  • Text Summarization (e.g., CNN/Daily Mail)
  • Natural Language Inference (e.g., SNLI)

Key Insights and Ideas

  • Attention as Feature Selection: At its core, attention is a dynamic feature selection mechanism. It allows a model to condense a long input sequence into a compact "context vector" containing only the most task-relevant information.
  • The Interpretability Debate: While attention weights are often used to "explain" model decisions (e.g., highlighting which words triggered a sentiment classification), the authors note the ongoing debate about whether these weights are truly faithful representations of the model's internal reasoning.
  • Hierarchical Processing: For long documents, applying attention hierarchically (first at the word level to build sentence representations, then at the sentence level for the document) is significantly more effective than a flat approach.
Examples of hierarchical-input attention architectures.
  • Knowledge Injection: Attention provides a unique "hook" to inject symbolic knowledge into neural networks. For instance, one can use a syntax tree to constrain which words a model is allowed to attend to, bridging the gap between connectionist (subsymbolic) and symbolic AI.

Key Results

The paper's primary "result" is the synthesis of the field into a structured taxonomy. It highlights that:

  • Multi-head attention (used in Transformers) is particularly powerful because different "heads" can learn to capture different types of relationships (e.g., one head for syntax, another for semantic similarity).
  • Co-attention is essential for tasks involving two inputs, like Question Answering, as it allows the model to consider the mutual interactions between the question and the document simultaneously.

Limitations and Future Work

  • Quantitative Comparison: The authors acknowledge that a direct quantitative comparison of different attention mechanisms is nearly impossible because they are always embedded in larger, complex architectures. A "fair" evaluation would require massive hyperparameter tuning across many tasks.
  • Unsupervised Attention: Most current models rely on supervised signals. A promising future direction is developing purely unsupervised attention models that mimic how humans learn to focus without explicit labels.
  • Neural-Symbolic Integration: Further research is needed to use attention for complex reasoning tasks, such as performing logical inference over knowledge graphs.
  • Outlier Detection: The authors suggest that attention weights could be used for outlier detection in training data—samples that consistently receive low attention weights might be noisy or irrelevant.

Summary updated

BibTeX
@misc{galassi2019attention,
  author = {Andrea Galassi and Marco Lippi and Paolo Torroni},
  title = {{Attention in Natural Language Processing}},
  year = {2019},
  eprint = {1902.02181},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1902.02181},
}
Figure