Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 90

Multi-Head Attention: Unpacking Representation Subspaces

Recommended resources

Hello! Welcome to your next lesson on the Transformer architecture.

In our last session, we mastered the mechanics of scaled dot-product attention. We saw how the Query, Key, and Value vectors work together to allow a token to look at other tokens in the sequence and produce a new, context-aware representation. We learned that this process is like a sophisticated information retrieval system, but one that is limited to a single perspective.

Today, we'll address that limitation. Our learning outcome is to explain the purpose of multi-head attention in capturing different representation subspaces. We'll discover how the Transformer runs multiple attention mechanisms in parallel, not just to be faster, but to allow the model to simultaneously understand a sentence from multiple viewpoints—like analyzing grammar, semantics, and word relationships all at once.

The Limitation of a Single Perspective

Imagine you're analyzing the sentence: "The mole on his arm was not a mole but a rare insect."

A single attention mechanism might learn to focus on the biological context, connecting "mole" to "arm" and "insect". But it would struggle to also capture the grammatical structure (e.g., connecting "was" to "mole") or other nuances at the same time. It's forced to average its attention across all these different types of relationships.

This is the core limitation of what we've learned so far, which is often called "single-head attention."

Multi-Head Attention Explained: Queries, Keys, and Values ...

This article, 'Multi-Head Attention Explained', succinctly describes this exact problem.

Please read the short section titled 'Why Single-Head Attention Is Limited'. It sets the stage perfectly for why we need something more powerful.


The Solution: Multiple Heads Working in Parallel

The solution proposed in the original "Attention Is All You Need" paper is elegant: instead of one attention mechanism, let's use several in parallel. This is called Multi-Head Attention.

Each "head" is essentially an independent scaled dot-product attention mechanism. The key idea is that each head will be encouraged to learn a different type of relationship from the data.

This diagram provides a clear visual comparison between the single-head architecture we know and the multi-head architecture we're about to explore.

Single Head vs. Multi-Head Attention Architecture
On the left is the Single Head Attention model we studied previously. On the right, Multi-Head Attention is shown to have multiple parallel Scaled Dot-Product Attention layers, whose outputs are concatenated and passed through a final linear layer.

As the diagram shows, the process involves three main stages:

  1. Projecting the Q, K, and V inputs for each head.
  2. Running scaled dot-product attention in parallel for each head.
  3. Concatenating the results and projecting them back to the expected dimension.

How Multi-Head Attention Works

Let's watch a video that provides a fantastic high-level intuition for why this parallel structure is so powerful.

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

The 3Blue1Brown channel has an excellent explanation of the motivation behind using multiple attention heads.

Watch the segment from 19:28 to 22:16. Pay close attention to the examples given for different types of contextual updates that different heads could learn (e.g., adjectives updating nouns, or distinguishing between 'Harry Potter' and 'Prince Harry').

The video brilliantly illustrates the goal: to give the model the capacity to learn many distinct ways that context can change meaning. Now, let's look at the mechanics of how this is achieved.

1. Creating Different Representation Subspaces

This is the most critical step. If we simply copied the same Q, K, and V matrices for each head, they would all perform the same calculation and learn the same thing. To prevent this, we first project the original Q, K, and V matrices into different, lower-dimensional spaces for each head.

For a model with heads, we create sets of unique, learnable weight matrices:

For each head , we calculate its specific query, key, and value:


These projection matrices () are the key. They act like different lenses, each transforming the original embedding into a different representation subspace. In each subspace, the model can focus on a different aspect of the word's meaning or its relationship with other words.

2. Calculating Attention in Parallel

Now, inside each subspace, we perform the same scaled dot-product attention calculation that we learned in the last lesson:

Since each head is working with its own projected version of Q, K, and V, each one will produce a different attention score matrix and, consequently, a different output.

3. Concatenating and Projecting

Once we have the output from all heads, we concatenate them back together into one large matrix.

This concatenated matrix now contains the information learned from all the different subspaces. However, its dimension is too large. So, we use one final, learnable weight matrix, , to project it back down to the model's original embedding size.

This final output vector is a rich representation that has incorporated context from multiple perspectives simultaneously.

Let's watch a more detailed walkthrough that shows the matrix operations and dimensions, which should resonate with your engineering background.

Attention is all you need (Transformer) - Model explanation (including math), Inference and Training

This video from Umar Jamil gives a clear, step-by-step mathematical view of the process, including how the matrices are split, processed, and recombined.

Watch from 28:34 to 33:43. Focus on how the input matrix (sequence x D_model) is projected and then 'split' along the embedding dimension (D_model) to feed into the different heads. This directly illustrates the creation of subspaces.


Capturing Richer Interpretations

So, what do these different subspaces actually learn? It's hard to say definitively, as the model learns whatever is optimal for its task. But we can build some intuition.

Transformers Explained Visually (Part 3): Multi-head ...

The article 'Transformers Explained Visually' provides an excellent summary and a great analogy for what different heads might learn.

Please read the sections 'Multiple Attention Heads' and 'Multi-head split captures richer interpretations'. The analogy about one head capturing 'gender-ness' and another 'cardinality' is a powerful way to understand the concept of different representation subspaces.

As the article explains, by splitting the embedding into different subspaces, each head can learn to focus on different aspects of a word's meaning and relationships. One head might focus on syntactic structure, another on semantic categories, and another on long-distance dependencies.

We can even visualize this. The different attention patterns produced by each head show what they are "paying attention" to.

This visualization from the video by Umar Jamil (based on the original paper) shows how different heads (represented by colors) create different connections between words. For the word 'making', some heads (blue, green) strongly connect it to 'difficult', while others (pink) connect it to '2009', demonstrating they've learned to focus on different contextual relationships.

Test your understanding!

An engineer on your team proposes a "simplification" to multi-head attention. Instead of having unique projection matrices () for each head, they suggest using the same Q, K, and V for all heads to save parameters.

Why would this "simplification" completely defeat the purpose of multi-head attention? What would happen to the "representation subspaces"?

Show answer

This would defeat the purpose because all heads would learn the exact same attention patterns. The unique projection matrices for each head are what create the different representation subspaces. Without them, every head receives the same input and performs the same calculation, producing identical outputs.

The concept of "representation subspaces" would collapse. There would only be one subspace, and all heads would be redundant. The model would lose its ability to capture diverse relationships in parallel, effectively reverting to a more computationally expensive version of single-head attention.


Conclusion

In this lesson, we've unpacked the "multi-head" part of the attention mechanism, which is central to the power of Transformers.

Key Takeaways:

  • Single-Head Limitation: A single attention head struggles to capture multiple types of relationships (e.g., syntactic, semantic) simultaneously.
  • Parallel Processing: Multi-head attention addresses this by running multiple attention mechanisms in parallel.
  • Representation Subspaces: The key to multi-head attention is the use of unique, learnable projection matrices () for each head. These matrices project the input embeddings into different subspaces, allowing each head to learn a unique perspective.
  • Richer Context: By combining the outputs from all heads, the model produces a final representation that is enriched with contextual information from many different viewpoints.

Preview of the Next Lesson:

So far, our attention mechanism is a "bag of words" model—it considers all words in the context but has no inherent sense of their order. If we shuffled a sentence, the attention output would be the same! For language, sequence order is crucial. In our next lesson, "Describe the role of positional encodings," we will explore the clever technique Transformers use to inject this vital sequential information into the model.