Tokenization and attention · Easy

Masked Mean Pooling

Problem 12 of 50 · Python and NumPy

Problem

Return the mean embedding over token rows whose mask entry is true. A false mask means padding and contributes neither values nor count. If all rows are masked, return a D-wide zero vector.

def masked_mean_pool(embeddings, mask):

Constraints

  • embeddings is T × D and mask contains T booleans; 1 ≤ T,D ≤ 16.
  • Entries are finite; output has shape D, without a token axis.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      1,
      2
    ],
    [
      9,
      9
    ]
  ],
  [
    true,
    false
  ]
]
Expected result: [
  1,
  2
]

Only [1,2] is valid, so the pooled vector is [1,2].

Example 2

Arguments: [
  [
    [
      1,
      2
    ],
    [
      3,
      4
    ]
  ],
  [
    false,
    false
  ]
]
Expected result: [
  0,
  0
]

No token is valid, so return one zero for each of the two features.

Sign in to solve this problem

Browse all coding problems · Learn the foundations