class
MaxUnpool2d
extends
_MaxUnpoolNdMaxUnpool2d(kernel_size: int | tuple[int, ...], stride: int | tuple[int, ...] | None = None, padding: int | tuple[int, ...] = 0)Computes a partial inverse of MaxPool2d.
Reconstructs a sparse approximation of the original 2-D feature map by scattering each pooled value back to the spatial position where the maximum was found, with all other positions set to zero.
For a pooled value at output cell with flat spatial index :
Parameters
kernel_sizeint or tuple[int, int]Must match the
kernel_size of the paired MaxPool2d.strideint or tuple[int, int] or None= NoneMust match the
stride of the paired pool. Defaults to
kernel_size.paddingint or tuple[int, int]= 0Must match the
padding of the paired pool. Default: 0.Attributes
kernel_sizeint or tuple[int, int]strideint or tuple[int, int]paddingint or tuple[int, int]Notes
- Input
x:(N, C, H_pooled, W_pooled) - Input
indices:(N, C, H_pooled, W_pooled) - Output:
(N, C, H_original, W_original)
- Originally introduced as part of the SegNet encoder-decoder architecture, where max-pooling indices are passed across the skip connection to guide unpooling in the decoder.
- The reconstructed tensor is not a true inverse — non-maximum values from the original pooling input are permanently lost.
- An optional
output_sizeparameter resolves ambiguity when the original input size cannot be uniquely recovered fromstrideandkernel_sizealone.
Examples
Symmetric encoder-decoder pair:
>>> import lucid
>>> import lucid.nn as nn
>>> unpool = nn.MaxUnpool2d(kernel_size=2, stride=2)
>>> x = lucid.ones((1, 64, 14, 14))
>>> indices = lucid.zeros((1, 64, 14, 14), dtype="int32")
>>> y = unpool(x, indices)
>>> y.shape
(1, 64, 28, 28)
With explicit output_size to handle odd input dimensions:
>>> unpool = nn.MaxUnpool2d(kernel_size=2, stride=2)
>>> x = lucid.ones((1, 16, 7, 7))
>>> indices = lucid.zeros((1, 16, 7, 7), dtype="int32")
>>> y = unpool(x, indices, output_size=(1, 16, 15, 15))
>>> y.shape
(1, 16, 15, 15)Methods (1)
fn
forward
→Tensorforward(x: Tensor, indices: Tensor, output_size: tuple[int, ...] | None = None)Apply the pooling operation to the input tensor.
Parameters
inputTensorInput tensor of shape where are the
spatial dimensions appropriate for this pooling layer.
Returns
TensorPooled output tensor.