Custom upsampling 4d tensor

I created a 4D tensor of the following size. (B, C, H, W)
And I want to reshape this tensor to the following size. (B, C, 2H, 2W)

Each value expands into 4 values, but only the elements in the original tensor that have an index corresponding to a value in the index tensor remain identical to the original tensor’s value.

The index follows the following rule.

0 1

2 3

And here is an example below:

original tensor:
torch.Size([1, 1, 2, 2])
tensor([[[[1.0000, 0.4000],
          [0.2000, 0.5000]]]])

index tensor:
torch.Size([1, 1, 2, 2])
tensor([[[[0, 2],
          [1, 1]]]])
output tensor:
torch.Size([1, 1, 4, 4])
tensor([[[[1.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.0000, 0.4000, 0.0000],
          [0.0000, 0.2000, 0.0000, 0.5000],
          [0.0000, 0.0000, 0.0000, 0.0000]]]])

How can I efficiently transform this tensor while making good utility of GPU? (maybe we can use torch.tensor.scatter_)

Hi Core!

I don’t follow your upsampling algorithm.

Rather than give examples, could you post a (complete, runnable) script that
performs the upsampling, using for-loops, etc., as necessary?

Best.

K. Frank