4.1.4 · D3Transformer Architecture

Worked examples — Scaled dot-product attention

2,224 words10 min readBack to topic

This is the "roll up your sleeves" child of Scaled dot-product attention. The parent told you what the formula is and why we scale. Here we throw every kind of input at it — matched keys, unmatched keys, ties, negative scores, a degenerate all-equal case, the large- saturation case, a real-world sentence, and an exam twist — and grind each one to a number.

Before we compute anything, one promise: every symbol is re-earned here, so you can read this page cold.


The scenario matrix

Every worked example below is tagged with the cell of this table it exercises. Together they touch all of them.

Cell What makes it special Where the danger is
A. Perfect match vs no match one key aligns, one is orthogonal baseline sanity
B. Tie (all scores equal) softmax must return uniform weights division-by-nothing intuition
C. Negative scores query dislikes a key () sign handling in
D. Degenerate values all values identical output must ignore weights
E. Large saturation unscaled vs scaled logits vanishing softmax gradient
F. Many keys, one dominates 4 keys, softmax near one-hot limiting behaviour
G. Real-world word problem a 3-word sentence, self-attention interpret the blend
H. Exam twist masked key (position we must forbid) logit trick

The worked examples

Cell A — perfect match vs orthogonal key


Cell B — the tie (all scores equal)


Cell C — negative scores (the query dislikes a key)


Cell D — degenerate values (weights become irrelevant)


Cell E — large : why scaling saves the gradient

This is the quantitative heart of the parent's "volume knob" story. Learning is driven by gradient descent, which needs a non-tiny gradient to make progress.

Figure — Scaled dot-product attention

Cell F — four keys, one dominant (limiting toward one-hot)


Cell G — real-world word problem (self-attention on 3 words)

Here come from the same sentence — this is self-attention. Each word produces its own query, key and value (via learned matrices, part of the Q-K-V model). We follow the query of the word "it" in "The cat slept" … pretend "it" refers back.

Figure — Scaled dot-product attention

Cell H — exam twist: a masked (forbidden) key

In decoders a query must not peek at future tokens. We forbid a key by adding to its logit before softmax — this is an attention mask. , so that key gets exactly zero weight.


Recall Self-check

Softmax of two equal scaled logits gives what weights? ::: Exactly — magnitude is irrelevant, only differences matter. A key with logit contributes what weight after softmax? ::: Zero, because (the masking trick). If all value rows are identical, what is the attention output? ::: That same value row, regardless of the weights. Why does scaling by help learning and not just prevent overflow? ::: It keeps logit differences small so softmax stays near , where the gradient is largest.

Related depth: Multi-Head Attention, Cross-Attention, Positional Encoding.