Padding zeros along a dimension


I have a tensor X: torch.float32, torch.Size([64, 3, 240, 320]).

I need to pad zeros and add an extra column(at the beginning) such that the resultant shape is torch.Size([64, 3, 240, 321]), i.e. all the extra elements are zeros(so an added column of zeros in the first column).

Is there a generic way to add zeros along a dimension? Later I would need to add a row(first row) of zeros as well.


Hy man, you can try the below

import torch.nn.functional as F
data = torch.ones((64, 3, 240, 320))
# pad(left, right, top, bottom)
new_data = F.pad(input=data, pad=(0, 1, 0, 0), mode='constant', value=0)

Reference reshaping a tensor with padding in pytorch

1 Like

Thanks @AbdulsalamBande