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
- create a all zero tensor
- 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?