Art of DS

Appearance

Palette
Theme
Unlock $20 one time
Sign in, or create an account

Sign in to Art of DSCreate your Art of DS account

We email you a link and a six-digit code. Either one signs you in; there is no password.Enter your email. We send a link and a six-digit code to confirm it — no password to remember. The account is free.

  • You come back to this page, right where you were.
  • Already have an account? The same email simply signs you in.

By continuing you accept the Terms and the Privacy policy.

Your account

Free accountLifetime access

difficulty 4/5

Attention

Definition

Letting every position compute a weighted average of every other, with the weights produced by comparing a query against keys — a learned, content-based routing of information.

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

Intuition

To interpret it in "the trophy did not fit in the suitcase because it was too big", a model has to look at trophy rather than suitcase. Which word to look at is not fixed by position — swap two adjectives and the answer changes — so it cannot be wired in. It has to be computed from the content.

Attention is that computation. Each position emits a query — what am I looking for — and every position emits a key — what I have. Compare them, turn the comparisons into weights that sum to one, and take a weighted average of the values. A word that matches the query strongly contributes most.

The reason this reshaped the field is not expressiveness but distance. In a recurrent network, information from token 1 reaches token 500 by passing through 499 steps — and, as that page measures, a gradient only a hundred steps back can already arrive at around 10⁻²³ of its strength. With attention it is one step, for every pair, always.

Attention is older than the transformer, and was invented to fix something else. It appears in Bahdanau et al.'s 2015 translation model, a recurrent encoder-decoder, in the paragraph explaining why the decoder should read a weighted mixture of the encoder's per-word states rather than a single summary: "Intuitively, this implements a mechanism of attention in the decoder. The decoder decides parts of the source sentence to pay attention to. By letting the decoder have an attention mechanism, we relieve the encoder from the burden of having to encode all information in the source sentence into a fixed-length vector" [1, §3.1]. The transformer's contribution two years later was to notice that once you have this, the recurrence it was bolted onto is no longer earning its place. See seq2seq for the bottleneck it was answering.

Formal definition

How it works

Where it's used

Why & when

Worked example

Pitfalls

Check yourself

3 questions

The questions on this page come with access. They are graded on the server, so the answers stay there until you have given yours.

Unlock all quizzes

Sources

  1. Bahdanau, Cho, Bengio. Neural Machine Translation by Jointly Learning to Align and Translate (2015)paperhttps://ar5iv.labs.arxiv.org/html/1409.0473
  2. Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin. Attention Is All You Need (2017)paperhttps://ar5iv.labs.arxiv.org/html/1706.03762
  3. Zhang, Lipton, Li, Smola. Dive into Deep Learningbook · retrieved 2026-09-05https://d2l.ai/
  4. Jain, Wallace. Attention is not Explanation (2019)paperhttps://arxiv.org/abs/1902.10186

Have a promo code?

A code that takes 100% off unlocks everything at once. A smaller one takes its share off the price in the payment window: enter the same code there.

Write to support

A question, a problem with access, a refund? Write to us and we answer by email.

What is it about?

Your address is used only to answer you.

Message sent

Thank you. We will reply to the address you gave, by email.

Or write to us directly

support@artofds.com

Message sent Thank you. We will reply to the address you gave, by email.

Check the email address: we need it to reply.

That is our own address. Enter yours, so the reply reaches you.

This address cannot receive email. Check it for a typo.

Write a little more: at least 10 characters.

Too many messages in a short time. Try again in a few minutes.

The message could not be sent. Please write to us directly: support@artofds.com