induwara.lk
induwara.lkAI · Transformers

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.

By Induwara AshinsanaUpdated Jul 18, 2026
Scaled dot-product attention
2×2

rows = query tokens, cols = dₖ

2×2

same cols (dₖ) as Q

2×2

same rows as K, cols = dᵥ

Examples
Shapes
2×2 · 2×2 · 2×2
Q · K · V
Head dim dₖ / dᵥ
2 / 2
scale √dₖ = 1.4142
Weight rows sum to 1
✓ yes
max error 0.0e+0
Stable vs naive softmax
Δ 0.0e+0
cross-check of the output
Decimals

1 · Raw scores

S = Q·Kᵀ

K1K2
Q11.00000.0000
Q20.00001.0000

2 · Scaled scores

S / √dₖ (√2 = 1.4142)

K1K2
Q10.70710.0000
Q20.00000.7071

3 · Attention weights

A = softmax(scores), row-wise

K1K2Σ
Q10.66980.33021.000
Q20.33020.66981.000

4 · Output context vectors

O = A·V

d1d2
Q11.66052.6605
Q22.33953.3395

Trace one query row

  1. Query Q1 = [1.0000, 0.0000]
  2. Scores (dot with each key, then ÷ √2 = 1.4142):
    • Q1·K1 = 1.0000 → 0.7071
    • Q1·K2 = 0.0000 → 0.0000
  3. Softmax: subtract row max 0.7071, exponentiate, divide by Σ = 1.4931
    • weight1 = 1.0000 / 1.4931 = 0.6698
    • weight2 = 0.4931 / 1.4931 = 0.3302
  4. Output O1 = Σ weightⱼ · Vj = [1.6605, 2.6605]

Method: Attention(Q,K,V) = softmax(Q·Kᵀ / √dₖ)·V— Vaswani et al., “Attention Is All You Need” (2017), §3.2.1; decoder masking §3.2.3. Softmax is the stable subtract-the-max form (each row sums to 1). Everything runs in your browser — no data leaves this page.

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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

Example 1 — one query, two keys (dₖ = 2, scaling on)

  1. Inputs: Q = [1, 0], K = [[1,0],[0,1]], V = [[1,2],[3,4]]
  2. Raw scores: Q·K₁ = 1·1 + 0·0 = 1 ; Q·K₂ = 1·0 + 0·1 = 0 → S = [1, 0]
  3. Scale by √2 = 1.414214 → S' = [0.707107, 0]
  4. Softmax: subtract max 0.707107 → exp([0, −0.707107]) = [1, 0.493069]
  5. Σ = 1.493069 → weights = [0.669762, 0.330238] (sum = 1.000000 ✓)
  6. Output: 0.669762·[1,2] + 0.330238·[3,4] = [1.660477, 2.660477]

Example 2 — causal mask on a 2-token sequence (dₖ = 2)

  1. Inputs: Q = K = V = [[1,0],[0,1]], scaling on, causal mask on
  2. Row Q₁ (i=0): key j=1 is in the future → score K₂ set to −∞
  3. Scaled row = [0.707107, −∞] → softmax → weights [1, 0]
  4. Output O₁ = 1·[1,0] = [1, 0]
  5. Row Q₂ (i=1): nothing masked → scores [0,1] → weights [0.330238, 0.669762]
  6. Output O₂ = 0.330238·[1,0] + 0.669762·[0,1] = [0.330238, 0.669762]

Example 3 — negative and fractional inputs (dₖ = 2, scaling on)

  1. Inputs: Q = [−1, −2], K = [[0.5,0.5],[1,−1]], V = [[2,0],[0,2]]
  2. Raw scores: Q·K₁ = −0.5 − 1 = −1.5 ; Q·K₂ = −1 + 2 = 1 → S = [−1.5, 1]
  3. Scale by √2 → S' = [−1.060660, 0.707107]
  4. Softmax: subtract max 0.707107 → exp([−1.767767, 0]) = [0.170711, 1]
  5. Σ = 1.170711 → weights = [0.145821, 0.854179] (sum = 1.000000 ✓)
  6. Output: 0.145821·[2,0] + 0.854179·[0,2] = [0.291642, 1.708358]

Frequently asked questions

Sources & references

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

Rate this tool
Be the first to rate

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.