I have multiple torch tensors with the following shapes
x1 = torch.Size([1, 512, 177])
x2 = torch.Size([1, 512, 250])
x3 = torch.Size([1, 512, 313])
How I can pad all these tensors by 0 over the last dimension, to have a unique shape like ([1, 512, 350]).
What I tried to do is to convert them into NumPy arrays and use these two lines of code:
if len(x1) < 350:
ff = np.pad(f, [(0, self.max_len - f.shape[0]), ], mode='constant')
f = ff
But unfortunately, it doesn't affect the last dim and still, the shapes are not equal. Any help will be appreciated Thanks
CodePudding user response:
You can simply do:
import torch.nn.functional as F
x = F.pad(x, (0, self.max_len - x.size(2)), "constant", 0)