Super Super

Transformer Attention Playground

runs locally

Attention weights · rows attend to columns

low medium high Click a token to inspect its row.

How do transformers work?

A transformer is a neural network architecture built almost entirely out of one operation: self-attention. Instead of reading a sentence left to right like an RNN, a transformer lets every token look at every other token at once, decide how relevant each one is, and blend their information accordingly. That single idea — learned, weighted, all-to-all lookup — is what made models like BERT and GPT possible.

The playground above is a real, if tiny, demonstration. It turns your sentence into tokens, projects each token into a query, key and value vector using fixed pseudo-random weights, and computes the actual scaled dot-product attention matrix. Nothing is uploaded; the arithmetic runs in your browser.

The four steps inside one attention head

  1. Tokenize. The sentence is split into words and punctuation. Real models use subword tokenizers (BPE, WordPiece, SentencePiece) so rare words become pieces like transform + ##er.
  2. Project. Each token embedding is multiplied by three learned matrices to produce a query Q, a key K and a value V. The query asks "what am I looking for?", the key advertises "what do I contain?", and the value carries "what will I hand over if chosen?".
  3. Score. Every query is dotted with every key, then divided by the square root of the key dimension. This scaling keeps the dot products from growing so large that the softmax saturates.
  4. Normalize and mix. A softmax over each row turns scores into weights that sum to 1. The output for a token is the weighted sum of all value vectors, so each position ends up carrying a blend of context.
Attention(Q, K, V) = softmax( Q·Kᵀ / √dk ) · V

Why the √dk scaling matters

If queries and keys have dimension d and their entries are roughly independent with unit variance, a dot product has variance about d. Without scaling, large d pushes scores into ranges where softmax becomes nearly one-hot: gradients vanish and training stalls. Dividing by √d restores unit variance, keeping the distribution of attention weights healthy.

Multiple heads, then a stack

A single attention head can only express one pattern of relevance. Transformers run several heads in parallel — the playground's head selector shows three different weight matrices — then concatenate their outputs and project them back to the model dimension. Stacking many such blocks with a feed-forward network, residual connections and layer normalization produces the deep transformer.

Because attention has no built-in notion of order, a positional signal (sinusoidal encodings, learned embeddings, or rotary position embeddings in modern models) is added so the network can tell "dog bites man" from "man bites dog".

What this demo does and does not show

Frequently asked questions

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 the decoder) while keys and values come from another (the encoder output). The arithmetic is identical; only the source of the three matrices differs.

Why does attention need a softmax instead of just using the raw scores?

Softmax converts arbitrary scores into a non-negative distribution that sums to one, so the output is a convex combination of value vectors. It also makes the operation differentiable and keeps the blend bounded regardless of how large the raw scores get.

Is attention quadratic in sequence length?

Yes. The score matrix is n × n for a sequence of n tokens, so both compute and memory grow quadratically. That is why long-context models invest heavily in efficient attention variants such as sparse, sliding-window or linear attention.

Are the weights in this playground trained?

No. They are generated deterministically from the token text so the demo is reproducible and needs no model download. The formula and normalization are real; the specific numbers are illustrative.