3

I have a 3 dimension vector. I would like to perform a 1d max pool on the second dimension.

According to the documentation of pytorch the pooling is always performed on the last dimension.

https://pytorch.org/docs/stable/nn.html#maxpool1d

For example:

>>> x = torch.rand(5, 64, 32)
>>> pool = nn.MaxPool1d(2, 2)
>>> pool(x).shape
torch.Size([5, 64, 16])

My desired output:

torch.Size([5, 32, 32])

How can i do that?

Montoya
  • 2,819
  • 3
  • 37
  • 65
  • 1
    Does this answer your question? [Pytorch maxpooling over channels dimension](https://stackoverflow.com/questions/46562612/pytorch-maxpooling-over-channels-dimension) – GoodDeeds Jan 17 '20 at 13:23

1 Answers1

6

You can simply permute the dimensions:

x = torch.rand(5, 128, 32)
pool = nn.MaxPool1d(2, 2)
pool(x.permute(0,2,1)).permute(0,2,1)  # shape (5, 128, 32) -> (5, 64, 32)
Andreas K.
  • 9,282
  • 3
  • 40
  • 45