Maxunpool output not correct

I was trying to use “MaxPool2d” and “MaxUnpool2d” in encoder and decoder, but the output of MaxUnpool2d is not making sense.
the following is the code

maxpool = nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True, return_indices=True)
maxunpool = nn.MaxUnpool2d(kernel_size=3, stride=2)
x = Variable(torch.rand(1,1,4,4).type(torch.FloatTensor))
y, ind = maxpool(x)
z = maxunpool(y, ind)
print(x)
print(y)
print(z)

and output is

(‘x’, Variable containing:
(0 ,0 ,.,.) =
0.3408 0.3246 0.5888 0.9761
0.0153 0.2923 0.7274 0.3501
0.7108 0.1856 0.2272 0.2173
0.3640 0.3814 0.9054 0.9198
[torch.FloatTensor of size 1x1x4x4]
)
(‘y’, Variable containing:
(0 ,0 ,.,.) =
0.7274 0.9761
0.9054 0.9198
[torch.FloatTensor of size 1x1x2x2]
)
(‘z’, Variable containing:
(0 ,0 ,.,.) =
0.0000 0.0000 0.0000 0.9761 0.0000
0.0000 0.7274 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.9054
0.9198 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000
[torch.FloatTensor of size 1x1x5x5]
)

am I missing something or is there some implementation mistake…

That is how MaxUnpool works.

From the docs

MaxPool2d is not fully invertible, since the non-maximal values are lost.

MaxUnpool2d takes in as input the output of MaxPool2d including the indices of the maximal values and computes a partial inverse in which all non-maximal values are set to zero.

That is indeed how MaxUnpool works but I would have quoted another part of the doc :

Note

MaxPool2d can map several input sizes to the same output sizes. Hence, the inversion process can get ambiguous. To accommodate this, you can provide the needed output size as an additional argument output_size in the forward call. See the Inputs and Example below.

If you want your output to be of size 4x4, you have to specify output_size=(4, 4)

Yes, if we provide output size, it will be corrected. Thank you.