Super Super

Transformer Attention Playground

scaled dot-product self-attention
Heads
Temperature 1.00
high attention low attention rows = query token, columns = key token

Focus token

Click any row in the matrix to inspect where that token looks.

Run summary

—

What self-attention actually computes

A transformer does not read a sentence left to right the way a recurrent network does. It treats the sentence as a set of tokens and, for every token, decides how much of every other token to pull in. That decision is the attention weight, and the whole mechanism is a small, inspectable piece of linear algebra.

Each token is first turned into three vectors by three learned projections: a query (what this token is looking for), a key (what this token offers), and a value (the content it will hand over). The attention weight between token i and token j is the dot product of query i with key j, scaled and passed through a softmax so each row sums to one.

Attention(Q, K, V) = softmax( Q·Kᵀ / √dₖ ) · V

The scaling by √dₖ matters. Dot products grow with dimension, and large logits push softmax into a near one-hot distribution where gradients vanish. Dividing by the square root of the key dimension keeps the logits in a range where softmax stays useful.

Why multiple heads

A single attention head can only express one pattern of relationships. Real transformers run several heads in parallel, each with its own projections, so one head can track syntax while another tracks coreference or position. Their outputs are concatenated and projected back to the model dimension. This playground lets you switch between one and four heads and see that each head produces a genuinely different matrix over the same sentence.

What this playground does and does not do

The tool computes real scaled dot-product attention in your browser. Because there is no trained model here, the query and key projections are deterministic pseudo-random matrices derived from a hash of your sentence and the head index. That means the numbers are honest attention arithmetic — the softmax, the scaling, the row normalization, and the head concatenation are all genuine — but the weights are not the weights a trained language model would produce. They are a faithful demonstration of the mechanism, not a claim about any specific model's behavior.

Two consequences follow. First, changing the sentence changes the projections, so the pattern shifts for reasons that are partly arbitrary. Second, the interesting comparison is structural: how sharp or soft the distribution is, how the temperature changes it, and how different heads disagree. Those properties hold for trained models too.

Reading the matrix

Each row is a query token and each column is a key token. A dark cell means that query places a lot of weight on that key. Rows always sum to one. Click any row to see its full distribution as bars, sorted by weight, so you can check the arithmetic yourself.

Temperature divides the logits before the softmax. Lower temperature sharpens the distribution toward the strongest match; higher temperature flattens it toward uniform attention. In trained models this is usually fixed at one, but it is a useful dial for understanding what the softmax is doing.

Where this fits in a full transformer block

Self-attention is one component of a block that also contains a position-wise feed-forward network, layer normalization, and residual connections. Attention mixes information across positions; the feed-forward network transforms each position independently. Stacking many such blocks is what produces the depth of modern language models. The attention matrix you see here is recomputed at every layer, and in a real model the pattern at layer 20 looks nothing like the pattern at layer 1.

Common questions

Why does the matrix look different every time I change the sentence?

Because the projections are derived from a hash of the sentence text. There is no trained weight matrix in this page, so the query and key vectors are generated deterministically from your input. The attention math on top of them is real, but the specific numbers are illustrative.

What does temperature actually change?

It divides every logit before the softmax. A temperature below one makes the largest logit dominate, producing a sharper, more concentrated row. A temperature above one flattens the row toward a uniform distribution. The row still sums to one either way.

Why are there multiple heads?

Each head has independent projections, so each head can learn a different relationship pattern. Concatenating them lets the model attend to several kinds of structure at once. In this playground each head uses a different hash seed, so the heads genuinely disagree.

Is my sentence sent anywhere?

No. All computation happens locally in your browser. Nothing is uploaded, and the export is generated from the same in-memory state that drives the visualization.