Skip to content
tezvyn:
Menu

Multi-Head Attention: Seeing Data From Multiple Angles

Source: Wikipedia: Multi-head attentionMediumHow cards are made

Multi-Head Attention: Seeing Data From Multiple Angles

Multi-head attention lets a model analyze a sequence from multiple perspectives at once. It runs several "attention heads" in parallel, each focusing on different relationships, like syntax vs. semantics.

Why it exists

A single attention mechanism can struggle to capture the many overlapping relationships in a sequence. For example, a sentence has grammatical structure, semantic meaning, and long-range dependencies all at once. A model needs a way to process these different layers of meaning simultaneously.

The mental model

Imagine you're reading a complex legal document. You might read it once to identify the parties involved, a second time to understand the core obligations, and a third time to note the deadlines. Multi-head attention does this in parallel. It creates several "heads," where each head is its own attention mechanism that learns to focus on a different aspect or relationship within the input data. The model then combines these different perspectives for a richer understanding.

How it works

Instead of calculating one set of attention weights for an input sequence, multi-head attention first splits the model's representational space into several subspaces. For each subspace, it initializes a separate attention "head". Each head independently calculates attention scores for the sequence, learning to focus on a specific type of relationship. The outputs from all heads are then concatenated and passed through a final linear layer to produce a unified output that incorporates insights from all perspectives.

When to use it

Use it when your data contains complex, multi-layered relationships. It is the core component of the Transformer architecture, making it the default for state-of-the-art NLP tasks like translation, summarization, and question answering. It is also foundational to Vision Transformers (ViTs) for finding relationships between different patches of an image.

When not to use it

For very simple sequences or when computational cost is a primary constraint. The parallel computations add significant overhead compared to a single attention mechanism. For simpler problems or small datasets where it might overfit, a more basic model like an RNN or a single-head attention mechanism could be more efficient and just as effective.

One canonical example

In the sentence "The animal didn't cross the street because it was too tired," one attention head might learn that "it" refers to "the animal." Simultaneously, another head might learn the causal link between "didn't cross" and "tired." Multi-head attention allows the model to understand both the grammatical reference and the semantic reason in parallel, which a single attention mechanism might miss or conflate.

Interview question

What is the main reason to use multi-head attention instead of a single attention mechanism?

  • a.To ensure that each token in the input sequence receives equal attention weight.
  • b.To process input sequences with fewer computational resources.
  • c.To enable the model to focus on various aspects and relationships within the data concurrently.Correct
  • d.To increase the model's capacity to handle extremely long input sequences.
Why?

Multi-head attention's primary benefit is allowing the model to analyze different types of relationships (like syntax and semantics) simultaneously, providing a more comprehensive understanding. Option B is incorrect because the card states that multi-head attention adds significant computational overhead.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on machine learning — each one lists the topics its interview covers.

See open roles