I want to create a mask tensor given a list of lengths. This would mean that there should be k ones and all other zeros for each row in the tensor.
eg:
input :
[2, 3, 5, 1]
output
[1 1 0 0 0
1 1 1 0 0
1 1 1 1 1
1 0 0 0 0]
Is the most efficient way as below or something similar?
seq_lens = torch.tensor([2,3,5,1])
max_len = torch.max(seq_lens)
mask_tensor = torch.Tensor()
for length in seq_lens:
new_row = torch.cat((torch.ones(length), torch.zeros(max_len-length))).unsqueeze(0)
mask_tensor = torch.cat((mask_tensor, new_row), 0)
print(mask_tensor)