Combine values with supplied attention weights
Type the indices of each value in their original order. · 175 words
After scores become attention weights, the transformer combines value vectors. If alpha_1 through alpha_n are the weights for one query, its context vector is c = sum alpha_i v_i. Each weight says how much of a value to include. The weights are nonnegative and normally sum to 1, so the output is a blend rather than a simple total. Values are learned projections of token representations, just like queries and keys. The context vector can then be passed to later layers, where it helps the model predict or interpret the next token.
For a travel app, suppose the query 'beach and museums' attends to three facts. Let the values be v1=[2,0] for 'beach', v2=[0,3] for 'museums', and v3=[4,1] for 'cafes'. Supplied weights are 0.2, 0.3, and 0.5, which sum to 1. The combined vector is 0.2[2,0] + 0.3[0,3] + 0.5[4,1] = [0.4,0] + [0,0.9] + [2,0.5] = [2.4,1.4]. The first coordinate is larger because beach and cafe values contribute strongly there. The second comes mainly from museums. Each query token receives such a weighted blend.
Check your recall
Using weights [0.2,0.3,0.5] and values [2,0], [0,3], and [4,1], what is the combined context vector?
Check your answer
The combined context vector is [2.4,1.4]. It is found by multiplying each value by its weight and adding the three resulting vectors.
Sources and credits
Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.