Challenge: Implementing Multi-Head Attention
Desliza para mostrar el menú
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.
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 5
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Sección 1. Capítulo 5