Transformers and Attention Is All You Need

Scroll to walk through how a transformer turns text into a prediction, one mechanism at a time, from tokens to attention to the next word. Unlike a recurrent neural network, which reads a sequence one token at a time, a transformer looks at every token at once, which is what makes it so much faster to train.

Scope of this page: the 2017 paper actually describes a full encoder-decoder built for machine translation: an encoder that reads the input sentence, and a decoder that generates the output one token at a time, attending both to its own earlier outputs and, through a separate cross-attention sublayer, to the encoder's output. This page covers the decoder half on its own, which is the architecture GPT-style LLMs actually use for next-token prediction. The core interface below, attention, multi-head attention, residual-and-normalize blocks stacked on top of each other, is the same shape either way. Some details have moved on since 2017, though: most modern decoder-only models place normalization inside the residual branch rather than after it (Pre-LN, easier to train at depth), and swap the paper's fixed sinusoidal positional encoding for schemes like RoPE or ALiBi. This page teaches the original paper's version of both, since the reasoning behind them carries over. Cross-attention and the encoder side get their own explainer later.
01

Predicting the next token

The cat sat on the ___

candidate next tokens
mat
0.00
floor
0.00
chair
0.00
roof
0.00
moon
0.00
P(next token∣tokens so far)P(\text{next token} \mid \text{tokens so far})

Type anything into a chatbot, and underneath the conversation is one repeated operation: given the text so far, guess what comes next.

That guess is not a single word. It is a probability spread across the model's entire vocabulary, every possible next token ranked by how likely it is.

Everything else on this page, tokenization, embeddings, attention, stacked layers, is really just how that probability distribution gets computed.

02

Tokens and embeddings

The cat sat on the mat

xi=E[tokeni]x_i = E[\text{token}_i]

Before any of that math happens, text is split into tokens, roughly word-sized pieces.

Each token is looked up in a table, learned during training, that maps it to a fixed-size vector of numbers: its embedding.

That lookup table has no idea about context. The same token always starts from exactly the same vector, no matter what surrounds it.

"Roughly" word-sized is doing real work in that first sentence: a real tokenizer only keeps common words whole. A rarer or unfamiliar word gets split into multiple subword pieces instead, which is a big part of why LLMs sometimes stumble on letter-counting or unusual spellings, the model never sees that word as one atomic unit to begin with.

03

Why embeddings alone aren't enough

river
bank
savings
bank

"river bank" and "savings bank" both contain the token "bank". Its starting embedding is identical in both phrases.

But the two phrases clearly need different meanings for that word. A static, one-size-fits-all vector cannot capture that by itself.

What the model needs is a way to let each token's vector shift based on the tokens before it, pulled toward whichever meaning the earlier context supports. (Both examples here put the disambiguating word first, on purpose: a decoder can only look backward, never forward, as the next section covers.)

04

Query, Key, Value

Query
Key
Value
cat
->W_Q->
->W_K->
->W_V->
sat
->W_Q->
->W_K->
->W_V->

Each small bar is one dimension of the vector on that side. Bar color fades toward black as that dimension's value drops toward 0, so a vector with several low values can look like it has fewer bars than one with high values everywhere. It doesn't, every vector here has the same 8 dimensions; a dark bar is still a bar, just a small number.

Q=XWQ,K=XWK,V=XWVQ = XW_Q, \quad K = XW_K, \quad V = XW_V

Every token's embedding produces three new vectors: a Query (what this token is looking for), a Key (what this token contains), and a Value (what it offers if attended to).

Each is produced by multiplying the embedding by its own learned weight matrix, the same three matrices reused for every token in the sequence.

The Query and Key vectors are what get compared to decide attention weights. The Value vector is what actually gets passed forward once those weights are set.

05

Scaled dot-product attention

raw scores for "sat"
The
0.11
cat
1.00
sat
0.71
on
0.36
the
0.07
mat
0.14
softmax ⁣(QKTdk+mask)V\text{softmax}\!\left(\frac{QK^T}{\sqrt{d_k}} + \text{mask}\right) V

For every pair of tokens, take the dot product of one token's Query with the other's Key. A high dot product means that Key looks like what the Query is searching for.

Those raw scores are divided by the square root of the key dimension. Without this, large dot products push softmax into regions where its gradient is nearly flat, which makes the model much harder to train.

Since this model is predicting the next token, a token is also blocked from attending to anything that comes after it. Those positions are masked to negative infinity before softmax runs, so they get exactly zero weight.

Softmax turns each token's row of remaining scores into a proper probability distribution: every value positive, the whole row summing to 1.

Do this for every token at once and you get the full attention matrix, lower-triangular because of the mask: how much each token attends to itself and everything before it.

06

Multi-head attention

head 1
The
cat
sat
mat
The
cat
sat
mat

Each row sums to 1 over its unmasked cells: the probability distribution one token uses to gather information from itself and earlier tokens. The hatched upper triangle is structurally blocked, not just low-weight: a token can never attend to one that comes after it. Hover a row to isolate it.

self-focused: each token attends mostly to itself
MultiHead(Q,K,V)=Concat(head1,…,headh) WO\text{MultiHead}(Q,K,V) = \text{Concat}(\text{head}_1, \dots, \text{head}_h)\, W_O

A single attention head has to average over everything it looks at, which the paper's authors point out inhibits capturing more than one kind of relationship at a time. Running several heads in parallel, each with its own learned projections, lets the model jointly attend to different representation subspaces at once.

Each head has its own learned Query, Key, and Value matrices, so each one can end up focused on something different: nearby words, subject-verb pairs, sentence position. The original paper uses 8 heads, each working in a 64-dimensional subspace of the model's 512 dimensions. The four below are simplified stand-ins so the different patterns are easy to see, shortened to "The cat sat mat" so a 4x4 grid fits cleanly; a real trained head rarely looks this clean.

The outputs of every head are concatenated back together and passed through one more learned matrix, W_O, mixing what each head found into a single vector per token. Nothing forces a head to specialize this way, it falls out of training because different patterns each help minimize the loss, and researchers have found this kind of specialization by inspecting real trained models.

07

Positional encoding

position 0position 49
PE(pos, 2i)=sin⁡ ⁣(pos100002i/d),PE(pos, 2i+1)=cos⁡ ⁣(pos100002i/d)PE_{(pos,\,2i)} = \sin\!\left(\frac{pos}{10000^{2i/d}}\right), \quad PE_{(pos,\,2i+1)} = \cos\!\left(\frac{pos}{10000^{2i/d}}\right)

Attention, as described so far, has no notion of distance. The causal mask already tells a token which positions came before it, but nothing about how far back, one step earlier and a hundred steps earlier look identical without more information.

So before attention runs, a positional encoding vector, built from sine and cosine waves at different frequencies, is added directly onto each token's embedding (first scaled up by sqrt(d_model), so its size is comparable to the positional signal being added to it).

The paper chose these specific sine and cosine waves over a learned position vector for two reasons: the fixed, regular pattern should make it easy for the model to learn to attend by relative position, and it should let the model handle sequences longer than any it saw during training.

In practice, this means every position in the sequence gets added a different, but consistent, fingerprint.

08

Stacking blocks

block 1
Multi-head attention
Add & Norm
Feedforward
Add & Norm
block 2
Multi-head attention
Add & Norm
Feedforward
Add & Norm
block 3
Multi-head attention
Add & Norm
Feedforward
Add & Norm
z=LayerNorm(x+Attention(x)),y=LayerNorm(z+FFN(z))z = \text{LayerNorm}(x + \text{Attention}(x)), \quad y = \text{LayerNorm}(z + \text{FFN}(z))

One pass of attention is not enough. The transformer stacks many identical blocks, each one refining every token's representation a bit further. The original paper stacks 6 on the decoder side; modern LLMs typically stack far more.

Inside each block, the attention output is added back to its input, a residual connection, then normalized with LayerNorm. This keeps gradients well-behaved as more blocks stack up.

The residual connection matters because it gives gradients a direct, unobstructed path backward: instead of a block having to learn the whole next representation from scratch, it only has to learn a correction, an "add this on top" delta, on top of an identity path that always passes the input straight through. That shortcut is what makes stacking dozens of blocks trainable at all.

LayerNorm does something more literal: for each token's vector on its own, it subtracts that vector's mean and divides by its standard deviation, forcing every token into the same predictable scale (mean 0, variance 1) before it enters the next block, then applies a small learned rescale. Watch the values below: same shape, same relative pattern, just rescaled onto common ground.

After that, a feedforward network processes each token's vector independently, expanding it to a wider hidden size and back down (512 to 2048 and back, in the original paper) before the next block begins.

09

Back to a probability distribution

logits (relative scale)
mat
1.00
floor
0.63
chair
0.46
roof
0.37
moon
0.34
logits=hfinal ET,P=softmax(logits)\text{logits} = h_{\text{final}}\, E^T, \qquad P = \text{softmax}(\text{logits})

After the last block, the final token's vector has absorbed information from the whole sequence, filtered through every layer of attention and feedforward processing.

That vector is projected back to the size of the vocabulary: one score, a logit, for every token the model knows about. The paper reuses the embedding table itself as this projection, rather than learning a separate matrix, so the same weights map a token to a vector and a vector back to token scores.

Softmax turns those scores into the same kind of probability distribution we started with, this time computed from the model's real internal state instead of illustration.

10

How these weights get learned

training step 0training step Nloss
L=−log⁡P(correct token),w←w−η∂L∂wL = -\log P(\text{correct token}), \qquad w \leftarrow w - \eta \frac{\partial L}{\partial w}

Every weight matrix used above, the embedding table, W_Q, W_K, W_V, W_O, the feedforward weights, starts out random.

Training feeds in real text, compares the predicted next-token distribution to the actual next token with a loss function, and measures how wrong the guess was.

Backpropagation computes how much each individual weight contributed to that error, by running the error signal back through the network, the reverse of the normal forward propagation direction. Gradient descent then nudges every weight slightly in the direction that would have made the correct answer more likely.

Repeat that across billions of examples, and the random matrices we started with become the ones that produced every visualization on this page. The full mechanics of backpropagation are their own topic, for a future explainer.