Padding 0 in specific index

torch.nn.functional.pad could only pad number at the edge of tensors. How can I insert numbers, e.g. zeros in specific index?

For example, after pad 0 in index [1 2 3], the tensor [1000 1001 1002 1003] comes [1000 0 0 0 1001 1002 1003].

Now I implement it by

  1. create a all zero tensor
  2. assign the value by index
# assume x is a tensor with shape NCHW
data = torch.zeros(x.size()[0], full_channel_num, x.size()[2], x.size()[3], device=x.device)
data[:, idx, :, :] = x

However, I don’t know if it’s recommended to create a new tensor in forward method. What’s the best practice for this operation?

1 Like