Trace two heads with different supplied weights
Type the indices of each value in their original order. · 180 words
A transformer block can run several attention heads in parallel. Each head may focus on a different relationship, and then the block combines their results. Take two tokens with value vectors: cat = [1, 0] and dog = [0, 1]. The current query is cat. For Head A, the supplied normalized attention weights are [0.7, 0.3]. Its supplied value projection is the identity, so its output is 0.7[1, 0] + 0.3[0, 1] = [0.7, 0.3]. In plain terms, Head A mostly copies information about cat.
Head B uses different supplied weights, [0.2, 0.8], and a supplied value projection that swaps the two coordinates. Its output is 0.2[0, 1] + 0.8[1, 0] = [0.8, 0.2], which mostly copies information about dog. Concatenating the heads gives [0.7, 0.3, 0.8, 0.2]. With the supplied output projection [[0.5, 0.5, 0, 0], [0, 0, 0.5, 0.5]], the attention result is [0.5, 0.5]. Adding the original cat vector as a residual gives [1.5, 0.5] before the feed-forward layer and normalization. The heads are not averaged before their work. Their separate outputs are concatenated and then mixed.
Check your recall
For Head B, what is the head output when the attention weights are [0.2, 0.8] and the value projection swaps the coordinates?
Check your answer
The output is 0.2[0, 1] + 0.8[1, 0] = [0.8, 0.2]. Head B gives more weight to the dog token even though the current query is cat.
Sources and credits
Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.