wikillm

Paper · 2019-02-04 · cs.CL, cs.AI, cs.LG, stat.ML

Attention in Natural Language Processing

Survey of attention in NLP. It defines one general model: a compatibility function scores keys against a query, a distribution function (usually softmax) turns scores into weights, and the weighted values form a context vector. A four-axis taxonomy covers input representation, compatibility, distribution and multiplicity. It has no experiments.

Attention mechanismSelf-attentionAttention interpretabilityattentionsurveynlptaxonomy

arXivPDFMarkdown10 min read

AI-compiled by claude-haiku-5-5 on 2026-10-24, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.

In plain words

Elements of a text differ in how much they matter for the task at hand. A neural network that reads text can learn to pick out the elements that matter for the job. This mechanism is called attention.

Attention works in three moves. The network compares each part of the input with something it is looking for, often a query. It turns those comparisons into weights that add up to one. Then it averages the parts of the input using those weights. The result is a short summary that is focused on what matters now.

This paper is a survey. It describes one general version of attention, shows how many published NLP models are special cases of it, and sorts them into a taxonomy. It also looks at how attention can carry outside knowledge into a model, whether its weights can be trusted as an explanation, and what open problems remain.

TL;DR

  • The survey writes attention as one model with three parts: a compatibility function, a distribution function and a weighted sum of values §II-B.
  • It sorts published NLP attention models along four axes: input representation, compatibility function, distribution function, and the number of inputs or outputs §IV.
  • It has no experiments and no quantitative comparison §I. Use it as a map of design choices, not as a source of benchmark numbers.

Problem

Elements of a text differ in how relevant they are to a task. A fixed-size encoding of a whole sentence or document can lose the parts that matter §I §II-A. Attention lets a model weigh the elements by relevance and combine them into a compact representation §III.

The survey also addresses two gaps in the literature. First, the field has no systematic overview of attention in NLP §I. Second, the same concept has appeared under different names; the paper gives an example where "inner attention" and "word attention" describe the same idea §I.

Method

The paper gives no training method of its own. Its contribution is a unified model that describes the attention variants found in NLP literature §II-B.

Worked example: RNNsearch

The paper uses the attention model of Bahdanau et al. for machine translation as its illustration §II-A. A bidirectional RNN encodes the source sentence into one annotation per position eq. 1. A decoder RNN predicts each target symbol from a context vector eq. 2. The context vector at each decoder step is a weighted sum of the annotations eq. 5. The weights come from a softmax over alignment scores, and each alignment score depends on the previous decoder state and one annotation eq. 3 eq. 4.

\mathbf{h}_1, \dots, \mathbf{h}_T = \mathrm{BiRNN}(x_1, \dots, x_T) eq. 1

p(y_t \mid y_1, \dots, y_{t-1}, x) = \mathrm{RNN}(c_t) eq. 2

e_{ti} = f(s_{t-1}, h_i) eq. 3

a_{ti} = \frac{\exp(e_{ti})}{\sum_{j=1}^{T} \exp(e_{tj})} eq. 4

c_t = \sum_i a_{ti}\, h_i eq. 5

The paper identifies the alignment model f with the compatibility function of its general model eq. 6. The additive form of this function is listed in tab. 4; the paper does not write out the RNNsearch alignment equation itself §II-A.

General model

The general model has one core part, shared by most surveyed models, plus optional extras §II-B fig. 3. It takes a set of keys K and values V from the input, and optionally a query q. Each key gets an energy score from the compatibility function f, which is applied to the query and keys eq. 6. The distribution function g turns the energies into attention weights a eq. 7. The softmax is the most common choice for g, and the paper calls it soft attention §II-B §II-C. Each weight scales its value eq. 8, and the scaled values are summed into a context vector eq. 9.

e = f(q, K) eq. 6

a = g(e) eq. 7

z_i = a_i\, v_i eq. 8

c = \sum_{i=1}^{d_K} z_i eq. 9

Keys and values come from the input through annotation functions, shown as kaf and vaf fig. 4 tab. 2. The query is optional. Some architectures have no query term, and their attention is computed from the input alone §IV-A. The notation of the model is listed in tab. 2; the symbols are defined in the Hyperparameters section below.

Deterministic and stochastic attention

The paper separates two uses of the word "deterministic" §II-C. Soft or deterministic attention uses a softmax, so every input contributes to the context vector eq. 9. Stochastic or hard attention draws one input index from the weights and uses only that input eq. 10 eq. 11. The paper says this form is trained in particular with a reinforcement-learning strategy §II-C.

\tilde{s} \sim \mathrm{Multinoulli}(\{a_i\}) eq. 10

c = \sum_{i=1}^{d_K} \tilde{s}_i\, v_i \quad \text{with } \tilde{s}_i \in \{0, 1\} eq. 11

The following pseudocode is our own rendering of the general model eq. 6-eq. 9. The paper has no algorithm box for it.

Pseudocode
# General attention model  [§II-B] [eq. 6]-[eq. 9]
# Input:  query (query_dim,); keys (num_keys, key_dim);
#         values (num_keys, value_dim)
# Output: context vector (value_dim,)

def attention(query, keys, values):
    # Score each key against the query with the compatibility
    # function f  [eq. 6]
    energies = compatibility(query, keys)      # (num_keys,)

    # Turn the energies into weights with the distribution
    # function g, usually softmax  [eq. 7]
    weights = distribution(energies)           # (num_keys,)

    # Scale each value by its weight  [eq. 8]
    weighted = weights[:, None] * values       # (num_keys, value_dim)

    # Sum the weighted values into one context vector  [eq. 9]
    context = weighted.sum(axis=0)             # (value_dim,)
    return context

Compatibility functions

The paper lists eleven compatibility functions tab. 4. Each maps a query and keys to energy scores eq. 6. Unless noted, W and b are learnable.

NameEquationRef.
similarityf(q,K) = \mathrm{sim}(q, K)[142]
multiplicative or dotf(q,K) = q^\top K[29]
scaled multiplicativef(q,K) = \dfrac{q^\top K}{\sqrt{n_k}}[36]
general or bilinearf(q,K) = q^\top W K[29]
biased generalf(q,K) = K^\top (W q + b)[67]
activated generalf(q,K) = \mathrm{act}(q^\top W K + b)[111]
concatf(q,K) = w_{imp}^\top \mathrm{act}(W[K;q] + b)[29]
additivef(q,K) = w_{imp}^\top \mathrm{act}(W_1 K + W_2 q + b)[2]
deepstack of L layers with activation act; the first layer takes K and q, the last projects to a scalar with w_{imp}[54]
convolution-basedf(q,K) = [e_0; \dots; e_{d_k}], e_j = \frac{1}{l}\sum_{i=j-l}^{j+l} e_{j,i}, e_{j,i} = \mathrm{act}(w_{imp}^\top [k_i; \dots; k_{i+l}] + b)[119]
location-basedf(q,K) = f(q)[29]

Source: tab. 4. The deep row is paraphrased because the layer equations in the table are hard to transcribe reliably; read tab. 4 before implementing it.

The text gives the cost of the main forms §IV-B. The dot product costs O(n_k d_k) per query. The additive form costs O(n_w n_k d_k), where n_w is the size of w_{imp}. The convolution-based form costs O(l^2 n_k d_k) for one filter of size l. The general form, which maps the query through W first, costs O(n_q n_k d_k) §IV-B.

Aggregation for multiple inputs

When two sequences interact, the energy scores form a matrix E over query-key pairs eq. 12 eq. 13. The paper lists five aggregation functions that turn E into energy scores for keys and queries tab. 5. In most cases, the attention weights a_K and a_Q are then obtained from these energies by a distribution function, which the table leaves out tab. 5.

E_{j,i} = W([k_i; q_j; k_i q_j]) eq. 12

E_{j,i} = \big(\mathrm{act}(W_1 Q + b_1)\big)^\top \mathrm{act}(W_2 K + b_2) eq. 13

NameEquations (summary)Ref.
poolingkey score: max over query elements j of E_{j,i}; query score: max over keys i of E_{j,i}[69]
perceptronmultilayer perceptron over E, K and Q gives e_K and e_Q[80]
linear transformatione_K = W_1 E, e_Q = W_2 E[127]
attention over attentiontwo distribution matrices from E; a_Q is an average over one of them; a_K is a dot product[70]
perceptron with nested attentione_K and e_Q from a perceptron over K, Q and nested matrices M_1, M_2 built from E[88]

Source: tab. 5. The last two rows are summarized, not transcribed; read tab. 5 before implementing them.

Uses of attention

tab. 3 maps the uses of attention to example tasks. The paper frames attention as a way to reduce input to a compact context vector, to pick features, and to supply an auxiliary training signal §III.

UseTasks
Feature selectionMultimodal tasks
Auxiliary taskVisual question answering; semantic role labelling
Contextual embedding creationMachine translation; sentiment analysis; information extraction
Sequence-to-sequence annotationMachine translation
Word selectionDependency parsing; cloze question answering
Multiple input processingQuestion answering

Source: tab. 3.

Taxonomy

The paper describes attention models along four dimensions §IV. They are orthogonal, so a model can be placed on each one independently §IV.

  • Input representation §IV-A. Keys and values are representations of parts of the document, such as words, characters or sentences. Typical annotation functions are GRUs, LSTMs and CNNs §IV-A. Keys can be raw embeddings, which the paper calls inner attention §IV-A. Self-attention uses the input sequence as both keys and query, and some architectures use no query at all §IV-A fig. 5. Hierarchical-input models attend at a lower level and then again at a higher one §IV-A.2 fig. 6.
  • Compatibility function §IV-B tab. 4. See the table above.
  • Distribution function §IV-C. Softmax is the common choice. The paper also lists the logistic sigmoid, which lets several keys score high at once §IV-C. Sparsemax sets low scores to zero §IV-C. Local attention restricts the weights to a window around a position, with a Gaussian shape §IV-C fig. 8. Hard attention picks a location that is not differentiable, so it needs reinforcement learning or variance reduction §IV-C.
  • Multiplicity §IV-D. A model can produce several outputs, use several heads, or attend to two inputs at once. Multihead attention applies several linear projections of K, V and q in parallel and merges the results §IV-D.1. Coattention applies attention to each input in turn, either coarse-grained or fine-grained §IV-D.2 fig. 7 fig. 8.

Injecting knowledge

Section V of the paper is about using prior knowledge to shape attention. It groups the methods as follows §V.

  • Supervised attention. The attention model gets a target distribution from an external source. Preliminary training uses a classifier or other tool to produce the target §V-A.1. Auxiliary training adds attention learning as a second task §V-A.2. Transfer learning uses the attention distribution of a model trained on a source task §V-A.3.
  • Attention tracking. When attention is applied repeatedly, as in sequence-to-sequence models, a coverage structure keeps track of what has already been attended §V-B.
  • Prior knowledge in the distribution function. Constraints can enforce boundaries or coverage on the weights §V-C. A distance between positions can come from the dependency graph of a sentence §V-C.

Hyperparameters

The paper has no training setup, so there are no learning rates, batch sizes or other hyperparameters. The notation is in tab. 2. The table below defines the symbols used in the general model.

SymbolMeaningWhereNotes
n_ksize of each keytab. 2the paper's K shape is R^{n_k \times d_k}, see Pitfalls
d_knumber of keystab. 2the paper also uses d_K in eq. 9
n_vsize of each valuetab. 2
n_qsize of the querytab. 2
n_wsize of the importance vector w_{imp}tab. 4

Results

Not applicable. The paper is a survey with no experiments. It states that an empirical evaluation of attention models would require many architectures and benchmarks, and that it is beyond the scope of the article §I. It also says it does not offer a quantitative evaluation of the different attention types §I.

Paper vs code

No official code. The paper links no code repository, project page or supplementary code. A GitHub repository search for the title words returned no repositories.

Pitfalls

  • Notation inconsistency. tab. 2 says the keys form a matrix of d_k vectors of size n_k, but its shape is written R^{n_k \times d_k}, and eq. 9 sums over d_K. Read d_k as the number of keys and n_k as the size of each key, then check the shapes in your implementation.
  • Ambiguous "query". The paper says the query in its general model is not the query of question answering or information retrieval. It is an optional element of the architecture and is task-independent §II-B.
  • Ambiguous "context vector". Most authors use the term for the weighted sum c. Yang et al. and Wang et al. use it for other elements of the model, and Yang et al. call w_{imp} a context vector §II-B §IV-B.
  • Hard attention is not differentiable. Hard attention needs reinforcement learning or variance reduction to train §II-C §IV-C.
  • Sigmoid and sparsemax change the normalization. With sigmoid, each weight lies between 0 and 1 and the weights are not forced to sum to one. Sparsemax sets low scores to exactly zero §IV-C.
  • Max pooling is conservative. Only the highest score counts, so a key that matches one query element can score as high as a key relevant to many §IV-D.2.
  • Attention as explanation is disputed. The paper reports that attention weights may not be a reliable explanation. Prior work showed attention can disagree with other explainability methods, and that adversarial attention distributions are easy to build for local cases §VI-A. The paper says attention may be a plausible explanation without being a faithful one §VI-A.
  • Not stated. The paper gives no code, no training recipe, no hyperparameters, and no quantitative results. Choices such as the form of the alignment function in RNNsearch are left to the cited sources §II-A.
  • Two table rows are summarized. The deep row of tab. 4 and the last two rows of tab. 5 were read from low-resolution page images. Check the source table before implementing them.

Lineage

  • Builds on: Bahdanau et al. (RNNsearch) is the paper's worked example §II-A. The general model extends the models of Vaswani et al. and Daniluk et al. §II-B.
  • Compared against: no quantitative comparison. The paper contrasts variants by their compatibility, distribution and multiplicity choices §IV.
  • Superseded by: not stated.

Source and license

Adapted from “Attention in Natural Language Processing” by Andrea Galassi, Marco Lippi, Paolo Torroni (arXiv:1902.02181), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors.