Pooling over a stack of tensors

If I have a stack of tensors of shape (20, 2048, 1, 1) How can I perform adaptive pooling over the stack to return a tensor of shape (1, 2048, 1, 1) Thank you.

just permute the tensor to get a (1,2048,1,20) or whatever shape which fits and pool.

1 Like

Thanks - here is the working example

import torch
import torch.nn.functional as F

t = torch.rand(20, 2048, 1, 1)
print(t.shape)
t = t.permute(2, 1, 0, 3)
print(t.shape)
t = F.adaptive_avg_pool2d(t, 1)
print(t.shape)
t = t.view(1, 2048)
print(t.shape)

returns

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