How to use pooling on rows, i.e: from (512, 20, 32) to (512, 10, 32)?

I have data with the shape: (512, 20, 32) and I want to use AvgPool and get the shape of: (512, 10, 32).

I have tried with no success:

pool = nn.AvgPool1d(kernel_size=2, stride=2)
data = torch.rand(512, 20, 32)
out  = pool(data)
print(out.shape)

output:

torch.size([512, 20, 16])

How can I run pool on the horizontal data ?

Hi Amit!

Use .transpose() to make your “horizontal” dimension the last dimension
of your tensor, apply AvgPool1d, and transpose back:

out = pool (data.transpose (1, 2)).transpose (1, 2)

Best.

K. Frank

1 Like