How to batch convert sentence lengths to masks in PyTorch?
For example, from

lens = [3, 5, 4]


we want to get

mask = [[1, 1, 1, 0, 0],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 0]]


Both of which are torch.LongTensors.

One way that I found is:

torch.arange(max_len).expand(len(lens), max_len) &lt; lens.unsqueeze(1)


Please share if there are better ways!
