torch.nn.MaxUnpool2dOptions
2D max unpooling: reconstructs spatial tensor from MaxPool2d output and indices.
Inverse operation of MaxPool2d. Places pooled values back at recorded max positions, zeros elsewhere. Essential for:
- Deconvolution networks (common in segmentation: FCN, SegNet, U-Net)
- Upsampling spatial feature maps
- Encoder-decoder architectures with pooling/unpooling
- Feature visualization and attribution
Usage pattern: MaxPool2d(return_indices=true) saves where max came from, MaxUnpool2d uses those indices to reconstruct.
Definition
export interface MaxUnpool2dOptions {
/** Stride of the pooling operation (default: kernel_size) */
stride?: number | [number, number];
/** Padding used in MaxPool2d (default: 0) */
padding?: number | [number, number];
}stride(number | [number, number])optional- – Stride of the pooling operation (default: kernel_size)
padding(number | [number, number])optional- – Padding used in MaxPool2d (default: 0)
Examples
// Segmentation with pooling/unpooling
const pool = new torch.nn.MaxPool2d(2, 2, 0, true); // return_indices=true
const x = torch.randn([8, 64, 112, 112]);
const [pooled, indices] = pool.forward(x) as [torch.Tensor, torch.Tensor]; // [8, 64, 56, 56]
const unpool = new torch.nn.MaxUnpool2d(2, 2);
const upsampled = unpool.unpool(pooled, indices); // [8, 64, 112, 112]// SegNet-style encoder-decoder with skip pooling indices
const pool1 = new torch.nn.MaxPool2d(2, 2, 0, true);
const x = torch.randn([16, 128, 224, 224]);
const [p1, idx1] = pool1.forward(x) as [torch.Tensor, torch.Tensor]; // [16, 128, 112, 112]
// ... more processing ...
const unpool1 = new torch.nn.MaxUnpool2d(2, 2);
const up1 = unpool1.unpool(p1, idx1); // [16, 128, 224, 224] - restored spatial dims