F.pad edge padding

Hi Folks,

Anyknow can you do that in torch ? I need to that while tensor on GPU can you do that ?

a = [1, 2, 3, 4, 5]
np.pad(a, (2, 3), ‘edge’)
array([1, 1, 1, 2, 3, 4, 5, 5, 5, 5])

You can try using F.pad in replicate mode. However it works only for 3D, 4D and 5D tensor inputs, won’t work for 1D or 2D. Also the tensor needs to be a float tensor.

a = torch.tensor([[[1, 2, 3, 4, 5]]]).float()
print(a)
# tensor([[[1., 2., 3., 4., 5.]]])

a_pad = F.pad(a, (2, 3), "replicate")
print(a_pad)
# tensor([[[1., 1., 1., 2., 3., 4., 5., 5., 5., 5.]]])
1 Like