What is the main idea behind multi-head attention?
Multi-head attention splits the attention mechanism into several smaller parts (heads) that run in parallel. Each head learns different relationships in the data, and their results are combined to capture richer information.
Click to reveal answer
beginner
In multi-head attention, what are the Query, Key, and Value?
Query, Key, and Value are three sets of vectors derived from the input data. The Query is what we want to find information about, the Key helps match the Query, and the Value holds the actual information to be gathered.
Click to reveal answer
intermediate
Why do we use multiple heads instead of one in attention?
Using multiple heads allows the model to focus on different parts or aspects of the input simultaneously. This helps the model understand complex patterns better than a single attention head.
Click to reveal answer
intermediate
What is the shape of the output from a multi-head attention layer in PyTorch?
The output shape is usually (sequence_length, batch_size, embedding_dim), where embedding_dim is the original input dimension after combining all heads.
Click to reveal answer
intermediate
How is the scaled dot-product attention computed inside each head?
Scaled dot-product attention is computed by taking the dot product of Query and Key, dividing by the square root of the key dimension to scale, applying softmax to get weights, and then multiplying by the Value vectors.
Click to reveal answer
What does each head in multi-head attention learn?
ADifferent relationships or features from the input
BThe same information repeatedly
COnly the first token of the input
DRandom noise
✗ Incorrect
Each head focuses on different parts or aspects of the input to capture diverse information.
What is the purpose of scaling the dot product in scaled dot-product attention?
ATo normalize the output to zero mean
BTo prevent large values that can make softmax gradients too small
CTo increase the dot product values
DTo reduce the number of heads
✗ Incorrect
Scaling by the square root of the key dimension keeps the dot products in a range that avoids very small gradients after softmax.
In PyTorch, which class implements multi-head attention?
Atorch.nn.AttentionHead
Btorch.nn.AttentionLayer
Ctorch.nn.MultiHeadLayer
Dtorch.nn.MultiheadAttention
✗ Incorrect
PyTorch provides torch.nn.MultiheadAttention for multi-head attention implementation.
What is combined after all attention heads compute their outputs?
AThe outputs are concatenated and projected back to the original dimension
BOnly the first head's output is used
CThe outputs are averaged without projection
DThe outputs are discarded
✗ Incorrect
Outputs from all heads are concatenated and then passed through a linear layer to match the original embedding size.
Which of these is NOT a component of multi-head attention?
AQuery
BValue
CBias
DKey
✗ Incorrect
Query, Key, and Value are core components; bias is not a fundamental part of multi-head attention.
Explain how multi-head attention works and why it is useful in simple terms.
Think about how looking at something from different angles helps understand it better.
You got /4 concepts.
Describe the role of Query, Key, and Value in the attention mechanism.
Imagine searching for a book in a library using a catalog.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of multi-head attention in PyTorch's MultiheadAttention layer?
easy
A. To reduce the size of the input data
B. To perform simple addition of input vectors
C. To increase the number of layers in the model
D. To allow the model to focus on different parts of the input simultaneously
Solution
Step 1: Understand the concept of multi-head attention
Multi-head attention splits the input into multiple parts (heads) to focus on different information simultaneously.
Step 2: Relate to PyTorch's MultiheadAttention layer
This layer implements this idea to improve model understanding by attending to various input features at once.
Final Answer:
To allow the model to focus on different parts of the input simultaneously -> Option D
Quick Check:
Multi-head attention = focus on multiple input parts [OK]
Hint: Multi-head means multiple focus points at once [OK]
Common Mistakes:
Thinking it reduces input size
Confusing it with adding layers
Assuming it just adds vectors
2. Which of the following is the correct way to create a multi-head attention layer with 8 heads and embedding dimension 64 in PyTorch?
easy
A. torch.nn.MultiheadAttention(8, 64)
B. torch.nn.MultiheadAttention(embed_dim=64, num_heads=8)
C. torch.nn.MultiheadAttention(num_heads=64, embed_dim=8)
D. torch.nn.MultiheadAttention(64)
Solution
Step 1: Check PyTorch MultiheadAttention parameter order
The constructor requires embed_dim first, then num_heads as keyword arguments.
Step 2: Verify correct syntax
torch.nn.MultiheadAttention(embed_dim=64, num_heads=8) correctly uses embed_dim=64 and num_heads=8. torch.nn.MultiheadAttention(num_heads=64, embed_dim=8) swaps parameters incorrectly, A and D use positional arguments wrongly.
Final Answer:
torch.nn.MultiheadAttention(embed_dim=64, num_heads=8) -> Option B
Quick Check:
embed_dim first, num_heads second [OK]
Hint: Remember: embed_dim first, num_heads second in constructor [OK]
Common Mistakes:
Swapping embed_dim and num_heads
Using positional args incorrectly
Missing one of the required parameters
3. Given the following PyTorch code, what is the shape of the output tensor attn_output?
A. The embed_dim of key does not match embed_dim of query and value
B. The batch size is incorrect
C. The sequence length is too large
D. The number of heads is too high
Solution
Step 1: Check embed_dim consistency
MultiheadAttention requires query, key, and value to have the same embedding dimension. Here, key has embed_dim 16, while query and value have 32.
Step 2: Identify the error cause
This mismatch causes a runtime error because the attention mechanism cannot align different embedding sizes.
Final Answer:
The embed_dim of key does not match embed_dim of query and value -> Option A
Quick Check:
Embed dims must match for query, key, value [OK]
Hint: Check all embed_dims match before calling attention [OK]
Common Mistakes:
Ignoring embed_dim mismatch
Assuming batch size causes error
Thinking sequence length is the problem
5. You want to apply multi-head attention to a batch of sentences with varying lengths. Which PyTorch feature helps you handle padding tokens correctly during attention computation?
hard
A. Increasing the number of heads to cover padding
B. Setting batch_first=True in MultiheadAttention
C. Using the key_padding_mask argument in MultiheadAttention
D. Manually removing padding tokens before attention
Solution
Step 1: Understand padding in variable-length sequences
Sentences have different lengths, so padding tokens are added to make batch tensors uniform.
Step 2: Use key_padding_mask to ignore padding
PyTorch's MultiheadAttention accepts key_padding_mask to mask out padded positions during attention, preventing them from affecting results.
Final Answer:
Using the key_padding_mask argument in MultiheadAttention -> Option C
Quick Check:
Mask padding tokens with key_padding_mask [OK]
Hint: Use key_padding_mask to ignore padded tokens [OK]
Common Mistakes:
Removing padding manually (inefficient)
Confusing batch_first with padding handling
Increasing heads does not fix padding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.