I am looking for a way to reduce the length of a 1D tensor by applying a pooling operation. How can I do it? If I apply MaxPool1d
, I get the error max_pool1d() input tensor must have 2 or 3 dimensions but got 1
.
Here is my code:
JavaScript
x
8
1
import numpy as np
2
import torch
3
4
A = np.random.rand(768)
5
m = nn.MaxPool1d(4,4)
6
A_tensor = torch.from_numpy(A)
7
output = m(A_tensor)
8
Advertisement
Answer
Your initialization is fine, you’ve defined the first two parameters of nn.MaxPool1d
: kernel_size
and stride
. For one-dimensional max-pooling both should be integers, not tuples.
The issue is with your input, it should be two-dimensional (the batch axis is missing):
JavaScript
1
3
1
>>> m = nn.MaxPool1d(4, 4)
2
>>> A_tensor = torch.rand(1, 768)
3
Then inference will result in:
JavaScript
1
4
1
>>> output = m(A_tensor)
2
>>> output.shape
3
torch.Size([1, 192])
4