Skip to content
fn

channel_shuffle

→Tensor
channel_shuffle(input: Tensor, groups: int)

Group-then-transpose channel rearrangement (ShuffleNet).

Splits the channel axis into groups equal-sized groups, transposes the group and intra-group axes, and flattens back. This re-orders channels so that, after a subsequent grouped convolution, information from each input group reaches every output group — enabling cheap cross-group information flow that grouped convolutions alone cannot provide.

Conceptually:

(N,g⋅c,H,W)→reshape(N,g,c,H,W)→transpose(N,c,g,H,W)→reshape(N,g⋅c,H,W)(N, g \cdot c, H, W) \xrightarrow{\mathrm{reshape}} (N, g, c, H, W) \xrightarrow{\mathrm{transpose}} (N, c, g, H, W) \xrightarrow{\mathrm{reshape}} (N, g \cdot c, H, W)

where g = groups and c = C / groups.

Parameters

inputTensor
Input tensor of shape (N, C, *spatial) (at least 2-D). C must be divisible by groups.
groupsint
Number of channel groups gg.

Returns

Tensor

Channel-shuffled tensor of the same shape as input.

Notes

Introduced in Zhang et al., 2018 — "ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices". The operation is a pure reshape + permute, so it adds no FLOPs and very little memory traffic.

Examples

>>> import lucid
>>> from lucid.nn.functional import channel_shuffle
>>> x = lucid.arange(24, dtype=lucid.float32).reshape(1, 6, 2, 2)
>>> y = channel_shuffle(x, groups=3)
>>> y.shape
(1, 6, 2, 2)
>>> y[0, :, 0, 0]              # channels 0..5 reordered as 0, 2, 4, 1, 3, 5
tensor([0., 8., 16., 4., 12., 20.])

Used by 2