Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Multi-Head Attention: Beyond Single Focus

Hello! Welcome back to our journey through the Transformer architecture.

In our last lesson, we explored scaled dot-product attention, the mechanism that allows a model to weigh the importance of different words in a sequence to create context-aware embeddings. We saw how Query, Key, and Value vectors interact to produce an output that is highly parallelizable, overcoming a major limitation of RNNs.

However, a single attention mechanism can be limiting. Consider the sentence: "The animal didn't cross the street because it was too tired." When processing the word "it," the model needs to understand that "it" refers to "the animal" (its antecedent) but also that its state is "tired." A single attention head might be forced to average its focus between these two different kinds of relationships.

Today, we will address this limitation by exploring multi-head attention. You will learn how the Transformer runs multiple attention calculations in parallel to capture a richer variety of contextual relationships. This lesson directly addresses the learning outcome: Describe multi-head attention and its advantage over single-head attention.

Why One Head Isn't Always Enough

The core idea behind multi-head attention is to give the model more capacity to understand complex relationships. It improves upon single-head attention in two primary ways:

  1. It expands the model’s ability to focus on different positions. As in our example above, it allows the model to jointly attend to information from different parts of the sentence. One "attention head" could learn to connect "it" to "animal," while another connects "it" to "tired."
  2. It gives the attention layer multiple “representation subspaces.” This is a more abstract but powerful concept. Instead of having just one set of Query, Key, and Value projection matrices (), we will have multiple sets. Each set projects the input embeddings into a different learned subspace. Think of this as looking at the sentence from multiple perspectives simultaneously. One head might focus on syntactic structure, another on semantic meaning, and a third on co-reference.

The Illustrated Transformer

The article 'The Illustrated Transformer' provides an excellent explanation of these two advantages. Please read the following short section to build a solid intuition for why multi-head attention is so effective.

Read from the heading 'The Beast With Many Heads' down to the image showing the attention pattern for the word 'it'. Focus on the two numbered points explaining the benefits and the example of what 'it' refers to.

To use a simple analogy from the resource "The Math Behind Multi-Head Attention," you can think of single-head attention as one spotlight on a stage. It can only illuminate one area at a time. Multi-head attention is like having multiple spotlights, each able to shine on a different performer, allowing the model to see the whole scene more clearly.

The Mechanism of Multi-Head Attention

Now, let's break down how multi-head attention is calculated. The process can be summarized in four main steps, which elegantly combine the single-head attention mechanism we've already learned with the concept of parallel processing.

Take a look at this diagram, which contrasts single-head (Scaled Dot-Product) attention with multi-head attention. Keep it in mind as we go through the steps.

Scaled Dot-Product Attention vs. Multi-Head Attention
This diagram illustrates how Scaled Dot-Product Attention is a component within the larger Multi-Head Attention architecture. Notice the parallel 'heads' and the final concatenation and linear steps on the right.

Step 1: Create Per-Head Projections

Instead of just one set of weight matrices for Q, K, and V, we now have h sets, where h is the number of attention heads (a hyperparameter, typically 8 or 12). For each head , we have its own weight matrices: .

The original input embeddings are used h times, each time being projected by a different set of matrices to create the queries, keys, and values for each head:

Step 2: Calculate Attention in Parallel

Each head now performs the exact same scaled dot-product attention calculation we learned in the last lesson, completely independently of the others.

Because these calculations are independent, they can be executed in parallel, making the mechanism highly efficient on modern hardware like GPUs.

Step 3: Concatenate the Head Outputs

The outputs from all h heads are concatenated into a single, large matrix.

Step 4: Final Linear Projection

This large concatenated matrix is passed through a final linear layer, with its own weight matrix , to project it back down to the model's expected dimension (). This final projection allows the information learned by all the different heads to be integrated and combined.

The full formula for multi-head attention is:


Let's watch a short video segment that visualizes this process beautifully.

Attention in transformers, step-by-step | Deep Learning Chapter 6

The 3Blue1Brown video on attention provides a fantastic animation of the multi-head mechanism. Watching it will help solidify your understanding of how the parallel heads operate and are then combined.

Watch from 20:31 to 22:16. Pay close attention to how the single-head process is duplicated for multiple heads and how their outputs (the 'proposed changes') are summed up to create the final refined embedding.

The Mathematics and Implementation

Given your math background, it's valuable to see the formal mathematical representation and how it maps to a practical implementation.

The Math Behind Multi-Head Attention in Transformers

Let's dive a bit deeper into the math. This article provides a clear, step-by-step mathematical formulation and then shows how it's implemented from scratch in Python with NumPy.

Please read section '2.2: Multi-Head Attention'. Focus on the equations for projecting the inputs for each head, computing the attention output for each head, and then concatenating and projecting the final result. Notice how it formalizes the four steps we just discussed.

In practice, frameworks like PyTorch and TensorFlow don't literally create h separate matrix multiplications. For efficiency, they perform a single large matrix multiplication to get the Q, K, and V projections for all heads at once, and then reshape the resulting tensor to add a "number of heads" dimension. This achieves the same outcome with fewer, larger operations, which is much faster on a GPU.

Let's see how this is done in code.

Multi Head Attention in Transformer Neural Networks with Code!

This video by CodeEmporium walks through a PyTorch implementation. Watching these clips will bridge the gap between the theory and how multi-head attention is practically built.

Please watch the following three segments: Conceptual Breakdown (2:30 - 4:35): Focus on how the input vector is conceptually broken down into components for each of the 8 heads. Implementation - Reshaping for Heads (4:35 - 7:35): This is the most important part. Pay close attention to how reshape and transpose operations are used to create the num_heads dimension in the tensor. This is the efficient implementation trick mentioned earlier. Implementation - Concatenation and Final Layer (13:15 - 13:45): See how the outputs of the heads are concatenated and passed through the final linear layer (W^O).

Conclusion

In this lesson, we expanded on our knowledge of self-attention by introducing the multi-head mechanism. By running multiple attention heads in parallel, the Transformer can capture a much richer set of relationships and features from the input data.

Key Takeaways:

  • Single-head attention can be limiting, as it may need to average its focus across different types of contextual relationships.
  • Multi-head attention addresses this by running multiple self-attention mechanisms in parallel, each with its own set of learnable weight matrices ().
  • The primary advantages are:
    1. It allows the model to focus on different parts of the input sequence simultaneously.
    2. It gives the model multiple "representation subspaces" to analyze the input from different perspectives.
  • The process involves projecting inputs for each head, calculating attention in parallel, concatenating the results, and a final linear projection to integrate the information.

Preview of the Next Lesson

So far, our attention mechanism is "permutation invariant"—if we shuffled the words in a sentence, it would produce nearly the same output because it lacks any sense of order or position. This is a problem, as word order is crucial for meaning. In our next lesson, we will explore positional encoding, the clever technique the Transformer uses to inject information about the position of each word into the model.

Can't find a good explanation? Sign up and we'll make it for you

Sign up