fn

pixel_shuffle

Tensor
pixel_shuffle(x: Tensor, upscale_factor: int)
source

Sub-pixel upsampling: rearrange channels into spatial resolution.

Reshapes a low-resolution multi-channel feature map into a higher-resolution map with fewer channels, without introducing any interpolation. Originally proposed by Shi et al., 2016 ("Real-Time Single Image and Video Super-Resolution"), this is the canonical decoder layer in single-image super-resolution and many GAN / diffusion upsamplers.

Concretely, with upscale factor rr:

(N,Cr2,H,W)    (N,C,Hr,Wr)(N, C \cdot r^2, H, W) \;\longrightarrow\; (N, C, H \cdot r, W \cdot r)

Each r×rr \times r patch in the output is taken from a block of r2r^2 channels in the input.

Parameters

xTensor
4-D input of shape (N, C·r², H, W). The channel dimension must be divisible by upscale_factor².
upscale_factorint
Spatial upscaling factor rr.

Returns

Tensor

Upsampled tensor of shape (N, C, H·r, W·r).

Notes

The transformation is a pure reshape + permute + reshape — autograd flows through ReshapeBackward and PermuteBackward with no custom gradient. Because no convolution is involved, the operator is essentially free; it is most often preceded by a convolution that produces the required C·r² channels.

Examples

>>> import lucid
>>> from lucid.nn.functional import pixel_shuffle
>>> x = lucid.randn(1, 12, 8, 8)      # C=3, r=2 → C·r²=12
>>> y = pixel_shuffle(x, upscale_factor=2)
>>> y.shape
(1, 3, 16, 16)