Super Super

Transformer Attention Lab

self-attention, live

Attention weights · query rows × key columns

high low

How do transformers work?

A transformer is a stack of layers that lets every token in a sequence look at every other token and decide how much to borrow from it. That single idea — self-attention — replaced recurrence as the default way to model language, and it is the mechanism behind modern chat models, translation systems, and code assistants. The lab above is a small, honest demonstration: it uses a deterministic toy embedding (not a trained model) so you can see the arithmetic of attention without downloading weights.

The three projections: query, key, value

Each token's embedding is multiplied by three learned matrices to produce a query (what this token is looking for), a key (what this token offers), and a value (the content it will hand over if attended to). Attention scores come from comparing queries to keys:

scores = (Q · Kᵀ) / √d_k

The division by √d_k keeps the dot products from growing with dimension, which would otherwise push softmax into a near one-hot regime and kill gradients.

Softmax turns scores into weights

Each row of scores is passed through softmax so the weights are positive and sum to 1. The output for a token is the weighted average of all value vectors:

Attention(Q,K,V) = softmax(QKᵀ / √d_k) · V

In the lab, the heatmap shows those weights directly. Row i, column j answers: "when token i updates itself, how much does it take from token j?"

Why multiple heads

A single attention head can only express one pattern of relationships. Multi-head attention runs several heads in parallel with smaller dimensions and concatenates their outputs, so one head can track local syntax while another tracks long-range references. Switch the Head selector above to see four different hand-designed patterns: local, syntactic, long-range, and content-based.

Positional information

Attention itself is order-blind — it treats the input as a set. Transformers add positional encodings (sinusoidal or learned) to the embeddings so the model can distinguish "dog bites man" from "man bites dog". The lab uses a simple sinusoidal position signal mixed into the toy embeddings.

Causal masking

Decoder-style models add a mask that sets all scores for future positions to −∞ before softmax, so a token can only attend to itself and earlier tokens. That is what makes autoregressive generation possible: the model never sees the token it is about to predict.

What this lab does and does not show

Frequently asked questions

Why do the attention weights look different from a real model?

Real models learn Q/K/V matrices from data. This lab uses fixed, hand-designed projections so the patterns are interpretable and reproducible without shipping a checkpoint. The arithmetic is the same; only the learned weights are replaced.

What does the entropy number mean?

Entropy of the focus token's attention distribution, in nats. Low entropy means the token concentrates on a few keys; high entropy means it spreads attention broadly.

Can I use my own sentence?

Yes. Type any sentence up to 120 characters and press Run attention. The lab re-tokenizes, re-embeds, and recomputes all heads.

Is anything uploaded?

No. All computation runs in your browser. The download button exports the current run as JSON locally.