Natural Language Processing · free preview

Self-Attention

Contextual embeddings from weighted sums of neighbors, queries, keys and values, scaled dot-product scores, and multiple heads.

1. Self-Attention

Self-Attention

2. Queries, keys, values, and heads

Queries, keys, values, and heads

3. Attention weights in NumPy

Attention weights in NumPy

4. Softmaxing attention scores

A token attends to three prior tokens with raw scores 22, 11, −1-1. Compute the attention weight for the first token after the softmax, round to three decimals.

5. Why divide by the root of the dimension

A query-key dot product is 1212 with head dimension dk=25d_k = 25. Compute the scaled score q⋅kdk\frac{\mathbf{q} \cdot \mathbf{k}}{\sqrt{d_k}} and round to three decimals.

6. Queries, keys, and values

Match each projection to its role in one attention head.

7. Masking with minus infinity

In the masked attention matrix for a causal language model, future positions receive the value −∞-\infty before the softmax rather than 0. Why?

8. Head width in multi-head attention

A transformer has model dimension d=512d = 512 and A=16A = 16 attention heads, with the usual choice dv=d/Ad_v = d/A so all heads share the budget. What is dvd_v? Answer with the number.

Practice this interactively

Free account · instant grading · spaced review that schedules itself.

Start this course for free