Problems
Loading...
Step 2 of 6
Position Embedding
Easy

Add one learned position vector to every token position. The supplied position embedding has a batch dimension of one, so the same position vectors are broadcast across every sequence.

zb,n,d=xb,n,d+en,d.z_{b,n,d} = x_{b,n,d} + e_{n,d}.

Here, bb indexes the batch, nn indexes token position, dd indexes the embedding coordinate, xx is patches, and ee is pos_embed. Return the result as a float64 NumPy array with the same shape as patches.

Loading visualization...

Examples

Input: patches = [[[1,2],[3,4]]], pos_embed = [[[0.1,0.2],[0.3,0.4]]]

Output: [[[1.1,2.2],[3.3,4.4]]]

Explanation: The position array broadcasts across the batch while preserving token and embedding axes.

Input: patches = [[[1,0],[0,1]],[[2,1],[1,2]]], pos_embed = [[[0.5,-0.5],[1,-1]]]

Output: [[[1.5,-0.5],[1,0]],[[2.5,0.5],[2,1]]]

Input: patches = [[[0,1,2]]], pos_embed = [[[-1,-2,-3]]]

Output: [[[-1,-1,-1]]]

Hint 1

NumPy broadcasts the leading size-one dimension across the batch.

Hint 2

Use elementwise addition without reshaping either input.

Requirements

  • Use NumPy.
  • Add the supplied position vectors elementwise.
  • Broadcast pos_embed across the batch only.
  • Preserve the token count and embedding width.
  • Return a float64 NumPy array with the same shape as patches.

Constraints

  • patches has shape (B, N, D) and dtype float64.
  • pos_embed has shape (1, N, D) and dtype float64.
  • B, N, and D are positive.