Challenge: Implementing Multi-Head Attention
Swipe to show menu
Task
You have all the building blocks: scaled dot-product attention from the previous challenge, and the intuition behind multiple heads from the last chapter. Now put them together.
Implement a MultiHeadAttention module as an nn.Module class. It should:
- Accept
d_modelandnum_headsas constructor arguments – assert thatd_model % num_heads == 0; - Define separate linear projections for
Q,K,V, and a final output projection; - In
forward(x), split the projections intonum_headsheads of dimensiond_model // num_heads; - Run scaled dot-product attention independently per head;
- Concatenate the head outputs and pass through the output projection.
Implement the module locally.
Everything was clear?
Thanks for your feedback!
Section 1. Chapter 5
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Section 1. Chapter 5