fn
moveaxis
→Tensormoveaxis(x: Tensor, source: int | Sequence[int], destination: int | Sequence[int])Move one or more axes to new positions.
More flexible than swapaxes — instead of pairwise swapping,
moveaxis re-positions a whole set of axes while preserving the
relative order of the remaining axes. Thin wrapper around
lucid.movedim that accepts either a single int or a
sequence on both endpoints.
Parameters
xTensorInput tensor.
sourceint | Sequence[int]Original position(s) of the axes to move. Negative values count
from the end.
destinationint | Sequence[int]Final position(s) for each moved axis. Must have the same length
as
source (after normalisation) and be a permutation-compatible
target set.Returns
TensorTensor whose axes have been re-ordered as requested.
Notes
Equivalent to applying a permutation to the axes such
that axis at source[i] ends up at destination[i] while the
other axes slide to fill in the gaps preserving their relative
order. Useful for vectorised code that needs to ferry a batch
dimension across the rank of a tensor.
Examples
>>> import lucid
>>> x = lucid.zeros((2, 3, 4, 5))
>>> lucid.moveaxis(x, 0, -1).shape
(3, 4, 5, 2)
>>> lucid.moveaxis(x, [0, 1], [-1, -2]).shape
(4, 5, 3, 2)