#61Attention step 1 — the scores QKᵀEasy

Attention step 1 — the scores QKᵀ

Background

The first step of attention is scoring: each query is dot-producted with every key to measure content similarity.

S=QK,Sij=qikjS = QK^\top, \qquad S_{ij} = \mathbf{q}_i \cdot \mathbf{k}_j

Q is (n_q, d_k), K is (n_k, d_k), and S is (n_q, n_k) — one raw similarity per query-key pair.

Problem statement

Implement attention_scores(Q, K) returning the raw score matrix QKQK^\top.

Input

  • Q — array-like of shape (n_q, d_k).
  • K — array-like of shape (n_k, d_k).

Output

Returns an np.ndarray of shape (n_q, n_k).

Examples

Example 1 — the lesson's worked query

Input:  Q = [[1, 0, 1, 0]],
        K = [[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 0, 0]]
Output: [[2, 0, 1]]

Explanation: qk1=2q\cdot k_1=2, qk2=0q\cdot k_2=0, qk3=1q\cdot k_3=1.

Constraints

  • Compute Q @ K.T.
  • Return shape (n_q, n_k).

Notes

  • A larger score means the query and key point in more similar directions — the content match attention is about to turn into a weight.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example 1 -- lesson worked query
  • Reference: two queries against three keys
  • Sample: identical Q and K (Gram matrix)