Hong Kong Baptist University · FACULTY OF COMPUTER SCIENCE

MATH7005 Chap.8 Scaled Dot-Product Attention

- one subject, every graph, every model, every mark
6 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 8 of 10 · MATH7005

Scaled Dot-Product Attention

Addressing separated from content

Attention compares a query with key rows, turns those scores into a probability distribution and uses the weights to mix value rows. Keys decide relevance while values carry returned information.

Scaling by the square root of key width prevents large dot products from forcing a sharply saturated softmax.

What to verify

Check Q, K and V dimensions, normalise each query row across keys, apply masks before softmax and hand-calculate one weighted value sum.

The output width follows the values rather than the keys.

Rows carry independent questions

With several queries, each score row is normalised separately across the available keys. Batch rows and unrelated queries must never share probability mass.

Write the score-matrix dimensions before applying softmax and check that every permitted row sums to one after masking.

Scaling protects useful gradients

Larger key width tends to produce higher-variance dot products. Without the square-root divisor, softmax can become sharply peaked and leave very small gradients for most positions.

Apply scaling to query-key scores before normalisation, not to the values or to the completed weighted sum.

Interpretability needs intervention

An attention heatmap shows internal routing for a stated layer and head. It is not automatically a causal explanation. Replace or mask content associated with a high weight and measure the output change under fixed evaluation settings.

If the result barely moves, the visual weight did not establish the stronger claim.

In this chapter

What this chapter covers

  • 01

    Query-key dot product

  • 02

    Score scaling

  • 03

    Rowwise softmax

  • 04

    Weighted value sum

  • 05

    Attention masking

  • 06

    Self and cross-attention

Worked example · free

Mix two scalar values by attention

Q [7 marks]. A query produces scaled scores [0, ln(3)] for values [2,10]. Find the attention output. AskSia authored this numerical drill; the mark allocation does not come from the University.
Attention separates addressing from content.Attention separates addressing from content. Query-key dot products form the weights, while the final vector is assembled from values using those weights.A query scores keys, then uses those weights to mix valuesquery qkeys Kk₁k₂k₃values Vv₁v₂v₃dotsoftmaxoutputEach output is a weighted sum of value rows, not of key rows.
Attention separates addressing from content. Query-key dot products form the weights, while the final vector is assembled from values using those weights.
  • 3Apply softmax to the scores.
  • 2Multiply values by their weights.
  • 2Add the contributions.
Exponentials are [1,3], so weights are [0.25,0.75]. The output is 0.25 times 2 plus 0.75 times 10, which equals 8.
Sia tip — The weights multiply values, not keys.
Glossary

Key terms

Query Vector
The representation that asks which key rows are relevant.
Key Vector
The representation compared with a query to create a relevance score.
Value Vector
The content mixed using attention weights after relevance is calculated.
Attention Matrix
The rowwise probability distributions connecting queries to key positions.
Scaling Factor
The square-root key-width divisor that stabilises dot-product magnitude.
FAQ

Scaled Dot-Product Attention FAQ

Why are attention scores scaled?

Dot-product variance grows with key dimension, making softmax excessively peaked and leaving weak gradients at most positions. Dividing by the square root of key width keeps the score scale more stable.

Why do keys and values need equal row counts?

Every key position receives one attention weight, and that weight must have a corresponding value row to mix. Their feature widths may differ, but their position counts must align.

Are attention weights explanations?

They are evidence about internal routing, not automatic causal explanations of a prediction. A stronger interpretability claim needs tests showing that changing the attended content changes the result in the asserted way.

Where should an attention mask be applied?

Apply it to disallowed scores before softmax so their exponentials contribute zero and permitted cells renormalise to one. Zeroing weights afterward changes the row scale and no longer performs the intended probability normalisation.

How can I hand-check batched attention code?

Remove batch and head axes for one small query-key-value example, reproduce the result by hand and then restore those axes. Assert that the same row remains unchanged, which isolates attention arithmetic from reshape and broadcasting errors.

What does the attention output dimension follow?

It keeps one row per query and the feature width of the values. Keys determine the number of scored positions and their similarity space, but the weighted content returned by the operation comes from V.

How can I detect a wrong softmax axis?

Check that every query row sums to one across key positions and compare one row with a hand calculation. Normalising across queries or batches may still produce plausible numbers, but it mixes independent predictions and violates the attention definition.

Study strategy

Assessment move

Choose one query, three keys and simple value vectors. Calculate dot products, divide by the square root of key width, apply stable softmax and form the weighted value sum. Check that the result lies within the coordinate range of the values before later projections. Add a mask to one key before normalisation and confirm that its weight becomes zero while the permitted cells renormalise.

Extend the example to two queries and label the rowwise axis. In code, assert Q, K, V, score and output shapes and compare one row with the hand result. For interpretation, label the layer and head and perform a content-removal test instead of treating colour intensity as proof.

Working through Scaled Dot-Product Attention in MATH7005? Sia is AskSia’s AI Computer Science tutor — ask any MATH7005 Scaled Dot-Product Attention question and get a clear, step-by-step explanation grounded in how MATH7005 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 12 of your Hong Kong Baptist University subjects - and 1,000+ Bibles across every Australian university.
Sia - your MATH7005 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works