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.Here, b indexes the batch, n indexes token position, d indexes the embedding coordinate, x is patches, and e is pos_embed. Return the result as a float64 NumPy array with the same shape as patches.
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.
All steps in ViT
Accepts: array
Accepts: array
Position Embedding
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.Here, b indexes the batch, n indexes token position, d indexes the embedding coordinate, x is patches, and e is pos_embed. Return the result as a float64 NumPy array with the same shape as patches.
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.
All steps in ViT
Accepts: array
Accepts: array