In pytorch, if you have a list of tensors, you can pad the right side using torch.nn.utils.rnn.pad_sequence

import torch'for the collate function, pad the sequences'f = [[0,1],[0, 3, 4],[4, 3, 2, 4, 3]]torch.nn.utils.rnn.pad_sequence([torch.tensor(part) for part in f],batch_first=True)
tensor([[0, 1, 0, 0, 0],[0, 3, 4, 0, 0],[4, 3, 2, 4, 3]])

How would I pad the left side? The desired solution is

tensor([[0, 0, 0, 0, 1],[0, 0, 0, 3, 4],[4, 3, 2, 4, 3]])
1

Best Answer


You can reverse the list, do the padding, and reverse the tensor. Would that be acceptable to you? If yes, you can use the code below.

torch.nn.utils.rnn.pad_sequence([torch.tensor(i[::-1]) for i in f ], # reverse the list and create tensors batch_first=True) # pad.flip(dims=[1]) # reverse/flip the padded tensor in first dimension