Skip to content

Attention

Lets each element of a sequence look at every other element and decide how much each one matters, then mix their information accordingly.

Alsoattention mechanism · self-attention · scaled dot-product attention · multi-head attention · QKV

Fig. / How it works
thethecatcatsatsatononthethematmatEVERY WORD LOOKS AT EVERY OTHER WORD
Role · kind
Core ideasTechnique
Level
Advanced

Bahdanau, Cho and Bengio, 2014

In the sentence 'The animal didn't cross the street because it was too tired', you know 'it' means the animal. Attention is how a model works that out: 'it' looks at every other word and pays the most attention to 'animal'.

Where it fits

Not shown: 3 more that build on it. See the full list below.

+All relationships as a list

See it on the full map

Try it

Where does each word look?
Loading interactive widget…
TakeawayEach attention head is a different way of reading the sentence. Some just look at the previous word; one learned, without being told, that “it” usually means the animal.

How it works

RNNs squeeze everything they have read into one hidden state, a bottleneck. Attention removes it: whenever the model needs information, it looks back over all positions and pulls from the relevant ones.

It works like a soft database lookup. Every token produces three vectors:

  • a query: what am I looking for?
  • a key: what do I contain?
  • a value: what will I hand over if picked?

For one token, compare its query with every key (a dot product). High similarity means high relevance. A softmax turns the similarities into weights that sum to 1, and the output is the weighted mix of all the values.

In self-attention, queries, keys and values all come from the same sequence, so every word gathers context from every other word. Multi-head attention runs several of these in parallel, so one head can track grammar while another tracks who “it” refers to.

Attention was first added to RNN translation models in 2014. In 2017 the Transformer showed you could drop the recurrence entirely and build a model from attention alone.

The math

With query, key and value matrices Q,K,VQ, K, V and key dimension dkd_k:

Attention(Q,K,V)=softmax ⁣(QK⊤dk)V\text{Attention}(Q, K, V) = \text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right) V

Dividing by dk\sqrt{d_k} keeps the dot products from growing with dimension, which would push the softmax into regions with tiny gradients.

Everyday use cases

  • Case 01

    Translation

    When translating a word, the model attends to the source words it corresponds to, even if word order differs.

  • Case 02

    Chat assistants

    Every token in a long conversation can look back at exactly the earlier parts it needs.

Strengths, weaknesses and when to use it

Strengths

  • Connects any two positions directly, however far apart.
  • Fully parallel across the sequence, which suits GPUs.
  • Attention weights give a (partial) window into what the model looks at.

Weaknesses

  • Cost grows with the square of the sequence length.

When to use itWhenever elements of your input need to share information flexibly, which is why it is now everywhere.

Check yourself

  1. Q01

    In attention, what decides how much one token attends to another?

    Show answer

    A: The similarity between the first token's query and the other token's key

  2. Q02

    How does the cost of self-attention grow with sequence length n?

    Show answer

    B: Quadratically

Go deeper