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 ?
In pytorch, the dimensions of tensors are
batch-channel-length. The pooling layer operates on thelengthdimension (the last one).There are two workarounds I can think of:
1. Permuting the dimensions
You can change the order of dimension prior to pooling, using
transpose:Output:
2. Adding a singleton channel dimension and using 2D pooling
Adding an additional "empty" channel dimension will turn your 3D input tensor into a 4D one, allowing
nn.AvgPool2dto operate on theheightdimension. This adding and removing of singleton dimensions can be done usingunsqueezeandsqueeze:Output:
I believe the second option is more intuitive and might be more efficient than the first one.