Challenge: Implementing Multi-Head Attention
Stryg for at vise menuen
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.
Var alt klart?
Tak for dine kommentarer!
Sektion 1. Kapitel 5
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Sektion 1. Kapitel 5