# Torch.veiw() usage to expand channel-wise value to spatial value

Hi, I have a tensor A at size (B, C, W, H), where C=16. I want to reshape it to be tensor B at size of (B, 1, 4W, 4H). Here each pixel in tensor A (1 x 16 x 1 x 1) will be transferred into a (1 x 1 x 4 x 4) spatial block in B. And the relative location for each pixel in A will inherit to spatial blocks in B.

Is there a way to do this in PyTorch with view()? Thanks!

Could you create an example using some numbers?
Currently, I’m not sure, how exactly the channels should be shifted to the spatial positions.

Something like this:

Thanks for the sketch.
It looks like the the values for each pixel in all channels should be placed as patches (or windows) into the final image.
Here is a small code example:

``````B, C, H, W = 2, 16, 4, 4
# Create dummy input with same values in each channel
x = torch.arange(C)[None, :, None, None].repeat(B, 1, H, W)
print(x)
> tensor([[[[ 0,  0,  0,  0],
[ 0,  0,  0,  0],
[ 0,  0,  0,  0],
[ 0,  0,  0,  0]],

[[ 1,  1,  1,  1],
[ 1,  1,  1,  1],
[ 1,  1,  1,  1],
[ 1,  1,  1,  1]],
...
# Permute channel dimension to last position and view as 4x4 windows
x = x.permute(0, 2, 3, 1).view(B, H, W, 4, 4)
print(x)
> tensor([[[[[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15]],

[[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15]],
...
# Permute "window dims" with spatial dims, view as desired output
x = x.permute(0, 1, 3, 2, 4).contiguous().view(B, 1, 4*H, 4*W)
print(x)
> tensor([[[[ 0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3],
[ 4,  5,  6,  7,  4,  5,  6,  7,  4,  5,  6,  7,  4,  5,  6,  7],
[ 8,  9, 10, 11,  8,  9, 10, 11,  8,  9, 10, 11,  8,  9, 10, 11],
[12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15],
[ 0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3],
[ 4,  5,  6,  7,  4,  5,  6,  7,  4,  5,  6,  7,  4,  5,  6,  7],
[ 8,  9, 10, 11,  8,  9, 10, 11,  8,  9, 10, 11,  8,  9, 10, 11],
[12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15],
...
``````