Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Explainers/Research Papers

Attention Is All You Need, explained simply

The 2017 paper that built a translation model from attention alone, letting every word read every other word at once. It became the base of modern language models.

Derpx0612 Apr · 6 min read
Contents
The idea in one sentenceHow it worksStep 1: Query, key and valueStep 2: Scaled dot-product attentionStep 3: Multi-head attentionStep 4: Positional encodingStep 5: Encoder and decoderWhat the paper foundWhat to be careful aboutSources
0% read · 6 min leftBack to top ↑
On this page0%
Contents
The idea in one sentenceHow it worksStep 1: Query, key and valueStep 2: Scaled dot-product attentionStep 3: Multi-head attentionStep 4: Positional encodingStep 5: Encoder and decoderWhat the paper foundWhat to be careful aboutSources

Before 2017, the best programs for translating languages read a sentence one word at a time. That was slow, because each step had to wait for the step before it. It also made it hard to link words that sit far apart. This paper showed a model that looks at all the words at once, using only a tool called attention. Almost every large language model today is built on it.

Paper
TitleAttention Is All You Need
AuthorsAshish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin
Year2017
VenueNIPS 2017 (31st Conference on Neural Information Processing Systems)
Linkarxiv.org/abs/1706.03762

The idea in one sentence

Build a translation model out of attention only, with no recurrence and no convolution, and call it the Transformer.

Attention lets each word in a sentence look at every other word and take in the information it needs. Because all words are handled together, the work can run in parallel on a GPU (a chip made for many small calculations at once).

How it works

Step 1: Query, key and value

A vector is a list of numbers. For each word, the model makes three vectors from that word's own vector, using learned weights (numbers the model adjusts during training).

  • The query says what this word is looking for.
  • The key says what this word offers to be matched against.
  • The value is the information this word hands over if it is matched.

The model compares one word's query with every word's key. A good match means "pay attention to that word". Then it takes a weighted mix of the values.

Step 2: Scaled dot-product attention

The paper's formula (equation 1) is:

Attention(Q,K,V)=softmax ⁣(QKTdk)V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}\!\left(\frac{QK^{T}}{\sqrt{d_k}}\right)VAttention(Q,K,V)=softmax(dk​​QKT​)V

Read it from the inside out.

  1. Dot product. Multiply two vectors entry by entry and add up the results. A bigger number means the vectors point in a more similar direction. QKTQK^{T}QKT does this for every query and key pair.
  2. Divide by dk\sqrt{d_k}dk​​. Here dkd_kdk​ is the length of the key vector. When dkd_kdk​ is large, dot products get large, and the next step then gives almost all the weight to one word, so learning slows down. Dividing keeps the numbers moderate. The paper says it suspects this is the reason, and it reports that unscaled dot-product attention does worse for larger dkd_kdk​.
  3. Softmax. This turns any list of scores into weights that are positive and add up to 1. Each score xxx becomes exe^{x}ex, divided by the sum of all the exe^{x}ex values.
  4. Multiply by V. Mix the value vectors using those weights.
Toy example (our own numbers, not from the paper)

One query q=(1,0)q=(1,0)q=(1,0) looks at three words with keys k1=(1,0)k_1=(1,0)k1​=(1,0), k2=(0,1)k_2=(0,1)k2​=(0,1), k3=(1,1)k_3=(1,1)k3​=(1,1) and values v1=(10,0)v_1=(10,0)v1​=(10,0), v2=(0,10)v_2=(0,10)v2​=(0,10), v3=(5,5)v_3=(5,5)v3​=(5,5). Here dk=2d_k=2dk​=2.

Dot products: 1,0,11, 0, 11,0,1. Divide by 2≈1.414\sqrt{2}\approx1.4142​≈1.414: 0.707,0,0.7070.707, 0, 0.7070.707,0,0.707. Softmax gives weights 0.401,0.198,0.4010.401, 0.198, 0.4010.401,0.198,0.401, which add up to 1.

Output =0.401⋅(10,0)+0.198⋅(0,10)+0.401⋅(5,5)≈(6.02, 3.98)=0.401\cdot(10,0)+0.198\cdot(0,10)+0.401\cdot(5,5)\approx(6.02,\ 3.98)=0.401⋅(10,0)+0.198⋅(0,10)+0.401⋅(5,5)≈(6.02, 3.98).

Query
Keys
Scores
Weights
Output

Compare the query with every key

Step 1 of 3

The word is called self-attention when the queries, keys and values all come from the same sentence.

Step 3: Multi-head attention

One attention step produces one set of weights, so it can follow only one kind of link at a time. The paper runs h=8h=8h=8 attention steps side by side, called heads. Each head first projects the queries, keys and values with its own learned weights into smaller vectors (dk=dv=64d_k=d_v=64dk​=dv​=64 when the model width is dmodel=512d_{model}=512dmodel​=512). The outputs of all heads are joined and projected once more. Because each head is smaller, the total cost is similar to one full-size head.

Multi-head attention architecture
Multi-head attention architecture

In the paper's tests, one head was 0.9 BLEU worse than the best setting, and too many heads also hurt.

Step 4: Positional encoding

Attention treats a sentence as a set of words. It has no sense of order, so "dog bites man" and "man bites dog" would look alike. The paper adds a positional encoding to each word's vector: a vector of the same length, built from sine and cosine waves of different wavelengths.

PE(pos,2i)=sin⁡ ⁣(pos100002i/dmodel),PE(pos,2i+1)=cos⁡ ⁣(pos100002i/dmodel)PE_{(pos,2i)}=\sin\!\left(\frac{pos}{10000^{2i/d_{model}}}\right),\quad PE_{(pos,2i+1)}=\cos\!\left(\frac{pos}{10000^{2i/d_{model}}}\right)PE(pos,2i)​=sin(100002i/dmodel​pos​),PE(pos,2i+1)​=cos(100002i/dmodel​pos​)
Toy example (our own numbers, with $d_{model}=4$)

For position 0 the first four values are (0, 1, 0, 1)(0,\ 1,\ 0,\ 1)(0, 1, 0, 1). For position 1 they are (0.841, 0.540, 0.010, 1.000)(0.841,\ 0.540,\ 0.010,\ 1.000)(0.841, 0.540, 0.010, 1.000). For position 2 they are (0.909, −0.416, 0.020, 1.000)(0.909,\ -0.416,\ 0.020,\ 1.000)(0.909, −0.416, 0.020, 1.000). Each position gets its own pattern. The first pair changes fast from position to position, and the second pair changes slowly.

The authors chose waves because they expected the model could learn to look at relative positions. They also tried learned position vectors and got nearly identical results.

Step 5: Encoder and decoder

The model has two stacks, each with N=6N=6N=6 identical layers.

  • The encoder reads the input sentence. Each layer has multi-head self-attention, then a small feed-forward network applied to each position.
  • The decoder writes the output one word at a time. Each layer has masked self-attention over the words written so far, then attention over the encoder's output (its queries come from the decoder, its keys and values from the encoder), then a feed-forward network.

Around every sub-layer there is a residual connection (the input is added back to the output) and layer normalization (rescaling the numbers to a steady range).

The mask sets scores for later positions to −∞-\infty−∞. Softmax turns those into weight 0, so a word cannot look at words that come after it.

What the paper found

All results are on translation, scored with BLEU, a number that measures how much a machine translation overlaps with human translations (higher is better). The test sets are WMT 2014.

ModelEnglish to GermanEnglish to FrenchTraining cost (FLOPs, German)
ConvS2S25.1640.469.6e18
MoE26.0340.562.0e19
GNMT + RL24.639.922.3e19
ConvS2S ensemble26.3641.297.7e19
Transformer (base)27.338.13.3e18
Transformer (big)28.441.82.3e19
  • English to German: the big model beat the best earlier results, including ensembles, by more than 2 BLEU. It trained for 3.5 days on 8 GPUs.
  • English to French: 41.8 BLEU for a single model, at less than a quarter of the training cost of the previous best single model. The body text of the paper says 41.0, but the abstract and Table 2 say 41.8.
  • The paper also tested English constituency parsing (finding the grammar tree of a sentence). A 4-layer Transformer scored 91.3 using only the WSJ data and 92.7 with extra data.

These numbers show that attention alone can match or beat the strongest 2017 models on these translation tasks, for less training cost. They do not show it is best for every task or every length of text.

What to be careful about

  • Cost grows with length. Every word looks at every other word, so the work grows with the square of the sentence length. The paper itself points to restricted attention for very long inputs as future work.
  • Results are mostly translation. Other uses, such as chatbots, came from later work.
  • The paper only suspects why scaling helps. It gives a short argument, not a proof.
  • Longer inputs than seen in training were a hope. The authors chose sine waves because they might allow longer sequences, but the paper does not test this.
Key takeaways
  • Attention lets every word read every other word, so no step waits for another.
  • Query, key and value turn "find what matters" into a dot product, a softmax and a weighted mix.
  • Multi-head attention, positional encoding and masking add several views, word order and no peeking.
  • The Transformer scored 28.4 and 41.8 BLEU on two translation tasks at lower training cost than earlier models.

Sources

  • Vaswani et al., Attention Is All You Need, NIPS 2017 (read in full from the arXiv PDF, version 7).
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Research Papers · 5 minMamba explained: an alternative to TransformersResearch Papers · 6 minLoRA explained: fine-tune a big AI model cheaplyResearch Papers · 7 minChinchilla explained: how big should an AI model be?
Start here
01Why do AI chatbots make things up?02How does Raft work? Getting computers to agree03Mamba explained: an alternative to Transformers