#41Attention step 3 — weighted sum of valuesEasyTransformers
Attention step 3 — weighted sum of values
Background
The final attention step turns the weights into an output by taking a weighted sum of the value vectors:
α is (n_q, n_k) (rows sum to 1), V is (n_k, d_v), and the output is (n_q, d_v) — each query's answer is a convex combination of the values.
Problem statement
Implement aggregate(weights, V) returning .
Input
weights— array-like of shape(n_q, n_k), attention weights.V— array-like of shape(n_k, d_v), value vectors.
Output
Returns an np.ndarray of shape (n_q, d_v).
Examples
Example 1 — the lesson's worked weights
Input: weights = [[0.506, 0.186, 0.308]],
V = [[10, 0], [0, 5], [2, 2]]
Output: [[5.676, 1.546]]
Explanation: mostly , a touch of , almost none of .
Constraints
- Compute
weights @ V. - Return shape
(n_q, d_v).
Notes
- Because the weights are a probability distribution, the output is a blend of values — never a hard pick — which keeps the whole operation differentiable.
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.
- •Reference example: lesson weights
- •Sample: uniform weights give the mean of values
- •Reference one-hot weight selects a single value