Reshape 4-dimensional tensor

Hi All,

I have a 4 dimensional numpy input array that was created from extracting features using ResNet50.

With a batch size of 5, I have created tensors of:

torch.Size([5, 2048, 1, 1])

I’m not really sure what to do if I want to feed this into a simple autoencoder. Would anyone be able to clarify how I should reshape this tensor to feed into an nn.linear layer?


Hi Taran!

I assume that your problem is the last two length-1 dimensions
of your tensor. If so, you can use torch.squeeze() to get rid of

You could then feed your (now torch.Size([5, 2048]))
tensor to, for example, a nn.Linear (2048, 256) layer.

(Note, if you ever have a batch size of 1, this will break by also
killing off the batch-size dimension. In such a case, you would
have to use torch.squeeze()'s dim argument to specify the
length-1 dimensions your need to get rid of.)

Good luck.

K. Frank

1 Like

Hi K. Frank,

Thanks for the reply.

Great, I had a look into torch.squeeze and created an autoencoder with those inputs. It works fine so thanks :slightly_smiling_face: