Multi-head attention runs attention several times in parallel with different learned projections, then concatenates the head outputs. Separate heads can focus on different relationships, such as sentence subjects or punctuation, instead of washing all source information into one softmax sum. section 15.3.1
A reusable Keras layer
A multi-head attention layer can replace the explicit per-head implementation: multi_head_attention <- layer_multi_head_attention(num_heads = num_heads, head_dim = head_dim); multi_head_attention(query = target, key = source, value = source). Each head projects queries, keys, and values, computes scaled dot-product attention, and contributes a partition of the final output. section 15.3.1
With self-attention, query, key, and value all come from the same sequence, allowing every token to incorporate contextual information from every other token. Transformer encoder blocks combine this layer with residual connections, layer normalization, and a two-layer feed-forward network. section 15.3.2 section 15.3.2
Figure 15.7 Multiheaded attention allows each target word to attend to different parts of the source sequence in separate partitions of the eventual output vector.
Shows how separate attention heads let each target token attend to different source regions within separate output partitions. section 15.3.1













