Make a prediction. Find the bug. See why.
LearnPyTorch · Attention Detective
Can your model see the future? Repair six attention bugs with token maps, live probabilities and tiny PyTorch experiments.
Select a token pair and follow its score into a probability and a weighted value. Fix one mistake at a time, then repair the whole attention block.
Your learning map · See all 19 challenges
Tensors: get the shapes right
9 challenges · Python basics helpful
Autograd: follow the gradients
4 challenges · derivatives and training
Attention: catch the hidden bugs
6 challenges · tokens, masks and values
Progress stays in this browser. All lessons are open; choose your own route.
Who is asking whom?
A transposed attention map can have exactly the right shape.
Make cell [i, j] equal query i dotted with key j on all three examples.
The visualization is live. Change a control, inspect the effect, then check your repair.
Challenge solved ✓
Try explaining thisFollow the computation
Your settings drive every number below. Reference mode does not change them.
Dot products
Who influences whom?
Toy vectors · not a language model
Select a matrix cell to inspect the arithmetic.
Change the last token: effect on token A
This changes the last key and value, keeping token A’s query fixed. A causal computation must leave token A’s output unchanged.
Temperature changes how concentrated the weights are. Mission checks use T = 1 on three fixtures.
Equivalent PyTorch
Inspect Q, K and V
Storage is unavailable; progress lasts for this visit.
What you will learn
Who is asking whom? ↗
Make cell [i, j] equal query i dotted with key j on all three examples.
Rows belong to queries; columns belong to keys.
If Q and K happened to be equal, why would this bug be harder to detect?
Why does attention become so confident? ↗
Apply the standard √dₖ scale before softmax. Check the example with four features.
The standard scale uses key feature width, not sequence length.
Move temperature from 0.5 to 2. How does the same row change?
Make every query spend one unit ↗
Normalize over keys so each query row sums to one.
Each query has its own probability distribution over available keys.
Why can every column sum to one while some rows do not?
Can your model see the future? ↗
Block future tokens before softmax. Changing the last token must not affect token A.
Causal attention permits the current position and earlier positions.
Why is multiplying by zero after softmax insufficient without renormalization?
Attention weights are not the answer ↗
Use the weights to combine V, matching the reference output on all examples.
Q and K produce weights; V supplies the vectors that those weights mix.
Can you change the output without changing any attention weights?
Repair the whole attention block ↗
Repair the formula, scale, axis, causal mask and value source. Pass all three fixtures and the future-token probe.
You have assembled a small causal scaled dot-product attention block.
Export the code. How would you add batch and head dimensions without mixing them?
What this lab models
This is a small, single-head, square self-attention simulator using hand-picked Q, K and V arrays. Token letters are identifiers: these vectors have not learned language. There are no learned projections, positional encodings, dropout, padding masks, training or full transformer model. Browser arithmetic uses JavaScript numbers; Python exports use float64.
Everything runs on your device. Progress stays in this browser when storage is available. Shared links contain only the visible controls and fixture number. No account is required.
Official PyTorch attention documentation ↗For a full-model walkthrough, explore Transformer Explainer. ↗
Continue with autograd →