Attention Score Calculator — Scaled Dot-Product Attention
Paste Query, Key, and Value matrices and see every step of a single attention head: the raw QKᵀ scores, the √dₖ-scaled scores, the softmax attention weights (each row summing to 1), and the output context vectors. Free, no signup, everything runs in your browser.
How it works
This tool implements scaled dot-product attention exactly as defined in Vaswani et al., “Attention Is All You Need” (2017), §3.2.1:
Attention(Q, K, V) = softmax(Q·Kᵀ / √dₖ)·V
Given a query matrix Q (n×dₖ), a key matrix K (m×dₖ), and a value matrix V (m×dᵥ), the computation runs in four stages:
- Raw scores. S = Q·Kᵀ, an n×m matrix. Each entry Sij = Σp Q[i][p]·K[j][p] is the dot product of query i with key j — how strongly that query matches that key.
- Scale.S′ = S / √dₖ. Dot products grow with the head dimension dₖ, and large values push softmax into flat regions where gradients vanish. Dividing by √dₖ (the standard deviation of a unit-variance dot product over dₖ terms) keeps the scores well-conditioned. If you turn scaling off, the tool uses the raw dot products directly.
- Causal mask (optional).For decoder self-attention, entries with j > i are set to −∞ before softmax, so a token cannot attend to future positions (Vaswani §3.2.3). Those weights become exactly 0.
- Softmax. Each row is normalised: Aij = e^{S′ij} / Σk e^{S′ik}. The implementation subtracts the row maximum before exponentiating — a value-preserving trick that prevents floating-point overflow. Every row of A sums to 1, shown as a Σ column you can eyeball.
- Output. O = A·V, an n×dᵥ matrix. Each output row Oi = Σj Aij·V[j] is a weight-averaged blend of the value vectors — the context vector the attention head passes on.
To keep the reference numbers trustworthy, every run also recomputes the output with the naive softmax (no max-subtraction) and reports the largest difference between the two — normally around 1e-16 on ordinary inputs, and an overflow on extreme scores, which is exactly why the stable form is the default. All computation is pure JavaScript in the browser; nothing is uploaded.
Worked examples
Frequently asked questions
Sources & references
- Vaswani et al., “Attention Is All You Need” (NeurIPS 2017) — §3.2.1 scaled dot-product attention
- Jurafsky & Martin, Speech and Language Processing (3rd ed. draft) — Transformers chapter
- PyTorch — scaled_dot_product_attention reference
The formula and worked examples on this page were last cross-checked against the sources above on 2026-07-18. The tool computes a single attention head only — no learned projections, positional encodings, or dropout.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.