Rate this Page

MaxUnpool2d#

classtorch.nn.modules.pooling.MaxUnpool2d(kernel_size,stride=None,padding=0)[source]#

Computes a partial inverse ofMaxPool2d.

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

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

Note

This operation may behave nondeterministically when the input indices has repeat values.Seepytorch/pytorch#80827 andReproducibility for more information.

Note

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

Parameters
  • kernel_size (int ortuple) – Size of the max pooling window.

  • stride (int ortuple) – Stride of the max pooling window.It is set tokernel_size by default.

  • padding (int ortuple) – Padding that was added to the input

Inputs:
  • input: the input Tensor to invert

  • indices: the indices given out byMaxPool2d

  • output_size (optional): the targeted output size

Shape:

Example:

>>>pool=nn.MaxPool2d(2,stride=2,return_indices=True)>>>unpool=nn.MaxUnpool2d(2,stride=2)>>>input=torch.tensor([[[[1.,2.,3.,4.],                            [ 5.,  6.,  7.,  8.],                            [ 9., 10., 11., 12.],                            [13., 14., 15., 16.]]]])>>>output,indices=pool(input)>>>unpool(output,indices)tensor([[[[  0.,   0.,   0.,   0.],          [  0.,   6.,   0.,   8.],          [  0.,   0.,   0.,   0.],          [  0.,  14.,   0.,  16.]]]])>>># Now using output_size to resolve an ambiguous size for the inverse>>>input=torch.tensor([[[[1.,2.,3.,4.,5.],                            [ 6.,  7.,  8.,  9., 10.],                            [11., 12., 13., 14., 15.],                            [16., 17., 18., 19., 20.]]]])>>>output,indices=pool(input)>>># This call will not work without specifying output_size>>>unpool(output,indices,output_size=input.size())tensor([[[[ 0.,  0.,  0.,  0.,  0.],          [ 0.,  7.,  0.,  9.,  0.],          [ 0.,  0.,  0.,  0.,  0.],          [ 0., 17.,  0., 19.,  0.]]]])
forward(input,indices,output_size=None)[source]#

Runs the forward pass.

Return type

Tensor