AI Lab

Tool 04

Attention Visualizer

Watch scaled dot-product attention connect tokens through Q, K, and V.

Learn

Attention lets each token "look at" every other token and decide how much to borrow from each. It solves the core problem: when predicting the next word, which earlier words actually matter? This mechanism is what makes transformers context-aware.

The problem: which earlier tokens matter?

In "The cat sat on the mat because it was tired," the word "it" probably refers to "cat" — not "mat." A model needs a way to connect distant words.

Before attention, RNNs processed tokens sequentially and struggled with long-range dependencies. Attention lets every token directly access every other token in one step.

Q, K, V in plain language

Each token's vector is projected into three versions:

Query (Q): "What am I looking for?" Key (K): "What do I contain / advertise?" Value (V): "What information do I pass if selected?"

Attention scores = how well each Query matches each Key. Those scores weight the Values to produce the output.

Scaled dot-product attention

The formula (in plain terms):

scores = (Q · Kᵀ) / sqrt(d_k) weights = softmax(scores) output = weights · V

Dividing by sqrt(d_k) prevents scores from getting too large (which would make softmax too sharp). Softmax turns scores into a probability distribution — each row sums to 1.

Reading the heatmap

Rows are query tokens (who is looking). Columns are key tokens (who is being looked at). Brighter cells = higher attention weight.

Look for diagonal patterns (tokens attending to themselves) and off-diagonal spikes (pronouns linking to nouns, verbs linking to subjects). Real models have many heads — each head can learn different patterns.

What to try in this tool

Start with short sentences containing pronouns — watch where "it" or "they" attends. Try lists ("apple, banana, cherry") and see if later items attend to earlier ones.

Longer sentences produce bigger heatmaps — scroll horizontally and look for non-obvious connections.

Try it above

  • Type "the cat sat on the mat because it was tired" — find what "it" attends to.
  • Try a sentence with two possible referents and compare attention patterns.
  • Use a short list of items and see how the last item connects to earlier ones.
  • Compare a 4-word vs 12-word sentence — notice how the matrix grows.