Splitting and padding a tensor

Hi all, is there any way to change A to B using pytorch’s built-in functions?

A=[1,2,0,4,5,6,7,0,8,9,10]
B = [[1,2,0,0,0],[4,5,6,7,0][8,9,10,0,0]]

Basically I’m trying to split A by value (0 in this case) and pad results.

A -> split(0) -> [1,2], [4,5,6,7], [8,9,10] -> pad -> [1,2,0,0,0],[4,5,6,7,0], [8,9,10,0,0]

Thanks

1 Like

did you manage to find such a function?

You can check out [torch.nn.utils.rnn.pad_sequence] (torch.nn.utils.rnn.pad_sequence — PyTorch 1.9.1 documentation)