fn

max_unpool1d

Tensor
max_unpool1d(x: Tensor, indices: Tensor, kernel_size: int | tuple[int], stride: int | tuple[int] | None = None, padding: int | tuple[int] = 0, output_size: tuple[int, ...] | None = None)
source

Inverse of max_pool1d via scatter at saved argmax indices.

Restores a "sparse" higher-resolution feature map by placing each value of x at the spatial position recorded in indices (i.e. the argmax positions captured by the corresponding max-pool forward pass). Non-selected positions remain zero. Standard component of encoder-decoder networks such as SegNet that propagate pooling indices through skip connections.

Parameters

xTensor
Pooled values, shape (N, C, L_in).
indicesTensor
Per-window argmax positions (flattened spatial coordinate), same shape as x.
kernel_sizeint or tuple of int
Original pooling window size (recorded for symmetry — not used directly in the scatter math).
strideint or tuple of int= None
Original stride. Recorded for symmetry.
paddingint or tuple of int= 0
Original padding. Recorded for symmetry.
output_sizetuple of int= None
Required. Target spatial shape — typically the length of the tensor that was originally fed to max_pool1d.

Returns

Tensor

Output of shape (N, C, L_out) containing x scattered into a zero buffer at the positions recorded in indices.

Notes

Math (let ϕ\phi be the bijection from pooled-cell index to saved flat input position):

yn,c,ϕ(n,c,l)=xn,c,l,yn,c,j=0 otherwisey_{n,c,\phi(n,c,l)} = x_{n,c,l},\qquad y_{n,c,j} = 0 \text{ otherwise}

Implemented as a differentiable scatter_add over a flattened spatial axis — gradients flow back to x. output_size is required because Lucid does not yet emit argmax indices from the forward pool, so we cannot infer the original spatial extent.

Examples

>>> import lucid
>>> from lucid.nn.functional import max_unpool1d
>>> x = lucid.randn(1, 2, 4)
>>> idx = lucid.tensor([[[0, 2, 4, 6], [1, 3, 5, 7]]], dtype=lucid.int64)
>>> y = max_unpool1d(x, idx, kernel_size=2, output_size=(8,))
>>> y.shape
(1, 2, 8)