The core operation of every Transformer. Computes **weighted sum of values** based on query-key similarity.
Attention(Q, K, V) = softmax(Q ร Kแต / โd_k) ร V1. Compute attention scores: Q ร Kแต
2. Scale by โd_k (prevents vanishing softmax gradients)
3. Apply softmax row-wise โ attention weights
4. Multiply by V โ weighted context
Q = K = [[1,0],[0,1]] V = [[1,2],[3,4]] d_k = 2 โ Output โ [[1.66082, 2.66082], [2.33918, 3.33918]]
Round to **5 decimal places**.
Test Cases (2 visible ยท 1 hidden)
Case 1: 2ร2 identity Q,K
Input: attention([[1,0],[0,1]],[[1,0],[0,1]],[[1,2],[3,4]])
Expected: [[1.66082, 2.66082], [2.33918, 3.33918]]
Case 2: Single query
Input: attention([[1,0]],[[1,0],[0,1]],[[10,20],[30,40]])
Expected: [[16.60882, 26.60882]]
โโต Run ยท โโงโต Submit