Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-104

Attention Mask from Padding

Problem

Build an attention mask from already padded sequences. Return 1 for every real token and 0 wherever the token equals pad_value.

Preserve the batch shape and return an empty list for an empty batch.

Hint

A nested list comprehension mirrors the batch and sequence dimensions.

Test cases

case 1
in: [[[1,2,0],[3,0,0]],0]
expected: [[1,1,0],[1,0,0]]
case 2
in: [[[5,6]],9]
expected: [[1,1]]
case 3
in: [[],0]
expected: []
case 4
in: [[[0,0]],0]
expected: [[0,0]]
Starter codePython
def attention_mask_from_padding(sequences, pad_value):
    # Your code here
    pass

4 test cases · run your code against them in the editor