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(), full_channel_num, x.size(), x.size(), 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?