PIXELBANKv9.1.0
Menu

Problem Statement

The simplest bridge is attention pooling: collapse many patch features into one vector using a set of attention weights. Compute the weighted average of the patch features.

Background

Given patch features X (N x D) and attention weights w (length N, already summing to 1), the pooled token is

p=∑iwi Xip = \sum_{i} w_i\, X_i

a convex combination of the rows. This is exactly what a one-query attention layer produces after its softmax.

Your Task

Implement:

def attention_pool(X, w):

Return the pooled D-vector as a list rounded to 4 decimals.

Input Format

  • X: N x D nested list.
  • w: list of N weights summing to 1.

Output Format

  • A list of D floats rounded to 4 decimals.

Sample

print(attention_pool([[1.0, 2.0], [3.0, 4.0]], [0.5, 0.5]))

Output:

[2.0, 3.0]

Example:

Input:
print(attention_pool([[1.0, 2.0], [3.0, 4.0]], [0.5, 0.5]))
Output:
[2.0, 3.0]
Reasoning:
  • Identify the patch features XX as a 2×22 \times 2 matrix with rows [1.0,2.0][1.0, 2.0] and [3.0,4.0][3.0, 4.0], and the attention weights ww as [0.5,0.5][0.5, 0.5].
  • Compute the pooled value for the first dimension by taking the weighted sum of the first column: 0.5×1.0+0.5×3.0=0.5+1.5=2.00.5 \times 1.0 + 0.5 \times 3.0 = 0.5 + 1.5 = 2.0.
  • Compute the pooled value for the second dimension by taking the weighted sum of the second column: 0.5×2.0+0.5×4.0=1.0+2.0=3.00.5 \times 2.0 + 0.5 \times 4.0 = 1.0 + 2.0 = 3.0.
  • Combine these results into the vector [2.0,3.0][2.0, 3.0] and round each element to 4 decimal places, which leaves the values unchanged.
  • The final output is [2.0, 3.0]

Constraints:

  • 1 <= N <= 5000, 1 <= D <= 4096.
  • w is a valid distribution over the N rows.
  • Round every entry to 4 decimals; avoid -0.0.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Attention Pooling to a Single Token - Easy | PixelBank