0

I am trying to understand unpooling in Pytorch because I want to build a convolutional auto-encoder.

I have the following code

from torch.autograd import Variable

data = Variable(torch.rand(1, 73, 480))
pool_t = nn.MaxPool2d(2, 2, return_indices=True)
unpool_t = nn.MaxUnpool2d(2, 2)
out, indices1 = pool_t(data)
out = unpool_t(out, indices1)

But I am constantly getting this error on the last line (unpooling).

IndexError: tuple index out of range

Although the data is simulated in this example, the input has to be of that shape because of the preprocessing that has to be done.

I am fairly new to convolutional networks, but I have even tried using a ReLU and convolutional 2D layer before the pooling however, the indices always seem to be incorrect when unpooling for this shape.

4

1 回答 1

3

Your data is 1D and you are using 2D pooling and unpooling operations.

PyTorch interpret the first two dimensions of tensors as "batch dimension" and "channel"/"feature space" dimension. The rest of the dimensions are treated as spatial dimensions.
So, in your example, data is 3D tensor of size (1, 73, 480) and is interpret by pytorch as a single batch ("batch dimension" = 1) with 73 channels per sample and 480 samples.
For some reason MaxPool2d works for you and treats the channel dimension as a spatial dimension and sample this as well - I'm not sure this is a bug or a feature.

If you do want to sample along the second dimension you can add an additional dimension, making data a 4D tensor:

out, indices1 = pool_t(data[None,...])
In [11]: out = unpool_t(out, indices1, data[None,...].size())
于 2019-10-16T08:29:39.360 回答