How to lower the dimension of the tensor in pytorch?

I have tensor `[1, 3, 256, 256, 3]`. I want to reduce it to `[1, 3, 256, 256]`. How can i do this ?
Thanks!

How would you like to reduce the last dimension?
In a simple way you could just call `x.mean(4)` or another arithmetic operation.

1 Like

I could bring the tensor to the form [1, 3, 1, 256, 256], in numpy I would be able to reduce the dimension of np.squeeze and add another axis to the 0 position, but can I do it in pytorch?

Iām not sure, which dimension you would like to squeeze or add, but PyTorch has also the method `squeeze()` and `unsqueeze()` to remove and add dimensions, respectively.

2 Likes

is there any way to reduce [1, 3, 256, 256, 3] to [1, 3, 256, 128, 3] ?

How would you like to reduce this dimension? Would you like to calculate the sum (or mean) of two neighboring values or just slice the tensor?

2 Likes

I solved it by using mean.
[1, 3, 256, 256, 3].view(1, 3, 2, 256*128, 3)
and then apply mean on third dimension.

1 Like

I have lowered torch dimension of shape` torch.zeros([16, 3, 32, 32])` into` [32,32,3]` numpy array by

``````    img = image.squeeze(0).detach().cpu().numpy()
print(img.shape) #(16, 3, 32, 32)
img = img[0, :,:,:].transpose(1,2,0)
print(img.shape # (32, 32, 3)
``````
1 Like