Super Super

Transformer Attention Playground

Self-attention, live

Attention weights — each row is a token looking at every token

strong weak selected row
Ready. Edit the sentence or press Run attention.

How do transformers work?

A transformer is a neural network architecture that reads a sequence of tokens — words, sub-words, or characters — and builds a representation of each token that depends on the tokens around it. The mechanism that does the "depends on the tokens around it" part is self-attention. Everything else in a transformer (feed-forward layers, layer norms, residual connections, positional encodings) supports or stacks that core idea.

The playground above is a small, honest demonstration of self-attention. It does not run a trained model. Instead it uses a fixed, deterministic projection to turn each token into three vectors — a query, a key, and a value — and then computes real attention weights from them. Change the sentence, the head, or the temperature and the numbers you see are recomputed from scratch. The arithmetic is the same arithmetic a trained transformer uses; only the weights are illustrative.

The three vectors: query, key, value

For every token position i, the model computes three vectors by multiplying the token's embedding by three learned matrices:

qi = xi WQ    ki = xi WK    vi = xi WV

The intuition that survives contact with real models:

  • Query — "what am I looking for?" A pronoun like it produces a query that asks for a noun.
  • Key — "what do I offer to a query?" A noun produces a key that matches noun-seeking queries.
  • Value — "what do I contribute if I am attended to?" The value is the content that gets mixed into the output.

In the playground, the projection is derived from a hash of each token so that the same token always produces the same vectors. That is why the demo is reproducible: the numbers are not random, they are a fixed function of the input.

Scaled dot-product attention

Attention weights are computed by comparing every query to every key, then normalising the scores with a softmax:

Attention(Q, K, V) = softmax( Q KT / √dk ) V

Step by step:

  1. Score — take the dot product of a query with each key. A large positive score means the query and key point in a similar direction.
  2. Scale — divide by √dk, the square root of the key dimension. Without this, dot products grow with dimension and push the softmax into a saturated, nearly one-hot regime.
  3. Softmax — turn the scores into a probability distribution over the keys. Every row of the attention matrix sums to 1.
  4. Mix — multiply the weights by the value vectors and sum. The result is the context vector for that token.

The temperature slider in the playground divides the scores before the softmax. Lower temperature sharpens attention (closer to picking one token); higher temperature flattens it (closer to averaging everything). Real models do not usually expose temperature at inference, but it is a useful knob for seeing how sensitive softmax is.

Why multiple heads?

A single attention head can only express one pattern of "who looks at whom." Real transformers run several heads in parallel, each with its own projection matrices, and concatenate the results. Different heads learn different relationships: one might track adjacent words, another might link a pronoun to its antecedent, another might connect a verb to a distant object.

The playground's three heads are hand-tuned to illustrate exactly that. Head 1 favours local neighbours, Head 2 favours subject-like tokens, and Head 3 favours long-range links. Switch between them on the same sentence and watch the heatmap change shape. In a trained model those patterns are discovered from data, not written by hand — but the mechanism is identical.

What the heatmap is showing

Each row is a token acting as a query. Each column is a token acting as a key. A dark cell at row i, column j means token i pays a lot of attention to token j. Because softmax normalises each row, the cells in any row add up to 1.

Click any token in the inspector to see its query, key and value vectors, and the full distribution of attention it places on the rest of the sentence. The context vector shown is the weighted sum of the value vectors under that distribution — the actual output that would be passed to the next layer.

What this demo does and does not claim

It doesIt does not
Compute real dot-product attention from real vectorsRun a trained language model
Show exact attention weights and context vectorsPredict the next token or produce text
Let you change the sentence, head and temperatureLearn weights from data
Run entirely in your browser with no uploadRepresent a specific published model's internals

The projection matrices are illustrative. They are chosen so that the demo produces interpretable patterns, not so that it matches any particular model. Treat the numbers as a faithful illustration of the arithmetic, not as evidence about how a specific model behaves.

How a full transformer stacks this

A single attention layer is only the beginning. A transformer block wraps attention with:

  • Residual connections — the input is added to the attention output, so gradients can flow and information is preserved.
  • Layer normalisation — stabilises the scale of activations before and after attention.
  • A feed-forward network — a two-layer MLP applied position-wise, which is where most of the parameters live.
  • Positional information — attention itself is permutation-invariant, so position must be injected via positional encodings or learned embeddings.

Stack dozens of these blocks and you get a transformer. The attention mechanism you are manipulating above is the same one running inside every one of them.

Frequently asked questions

Why divide by the square root of the key dimension?

Dot products of independent random vectors have variance proportional to the dimension. Without scaling, large dimensions produce large scores, the softmax saturates, and gradients vanish. Dividing by √dk keeps the variance roughly constant regardless of dimension.

What is the difference between self-attention and cross-attention?

In self-attention, queries, keys and values all come from the same sequence. In cross-attention, queries come from one sequence (for example, a decoder) while keys and values come from another (for example, an encoder output). The arithmetic is the same; only the source of the vectors differs.

Why does the playground use a hash instead of a trained embedding?

Because the goal is to show the attention arithmetic, not to reproduce a specific model. A hash gives every token a stable, deterministic vector, so the demo is reproducible and needs no model download. A trained model would replace that hash with a learned embedding table.

Is my sentence sent anywhere?

No. Everything runs locally in your browser. The sentence you type is used only to compute the vectors and attention weights on this page, and the download is generated from the same in-memory state.

Why do the attention rows sum to 1?

Because the softmax normalises each row of scores into a probability distribution. That is what makes the weights interpretable as "how much of my attention goes to each token."