What a transformer actually does with your sentence
A transformer is a stack of identical blocks. Each block has two jobs: self-attention, which lets every token look at every other token, and a small feed-forward network, which transforms each token on its own. The tool above isolates the first job so you can see it happen.
Every token is turned into three vectors by learned weight matrices:
- Query (Q) â what this token is looking for.
- Key (K) â what this token offers to others.
- Value (V) â the information this token will pass along if it is attended to.
Attention scores are the dot product of a query with every key, scaled by 1/âd_k so the numbers stay in a workable range. A softmax turns those scores into weights that sum to 1. The output for a token is the weighted sum of all value vectors.
Try this: type the cat sat on the mat, then change it to the dog sat on the rug. Watch how the attention pattern for sat shifts â the model is not reading grammar, it is comparing learned vector directions.
Why the softmax temperature matters
Dividing the scores by a temperature before the softmax changes how peaked the distribution is. A low temperature makes attention nearly one-hot: each token looks at a single other token. A high temperature flattens the distribution so every token contributes roughly equally. Real transformers use a fixed 1/âd_k scale, but exposing the temperature makes the mechanism visible.
Multi-head attention
One attention head can only express one pattern. Transformers run several heads in parallel, each with its own Q/K/V projections, then concatenate the outputs. Different heads tend to specialize: some track syntax, some track nearby tokens, some track long-range references. The head selector above simulates three such specializations so you can compare their matrices.
What this demo simplifies
- Vectors are deterministic hashes of the token text, not learned embeddings.
- There is no positional encoding, no layer norm, and no feed-forward sublayer.
- Attention is computed on the surface tokens you type, not on subword pieces.
- The three heads are hand-tuned patterns, not trained weights.
Those simplifications are deliberate. The goal is to make the arithmetic of scaled dot-product attention inspectable, not to reproduce a specific pretrained model.
Frequently asked questions
Why does the attention matrix have a diagonal?
Each token attends to itself as well as to every other token. The diagonal cell is the weight a token places on its own key. In real transformers this is common and often strong, especially in early layers.
What does the weighted value vector show?
It is the sum of every value vector, weighted by the attention row. That vector is what the token passes to the next sublayer. The panel shows the first four dimensions so the numbers stay readable.
Is my sentence sent anywhere?
No. All computation runs in your browser with plain JavaScript. Nothing is uploaded, logged, or stored beyond the current page session.
Why do the numbers change when I change the temperature?
Temperature divides the raw scores before the softmax. Lower values sharpen the distribution; higher values flatten it. The underlying query and key vectors do not change.