fn

swapdims

Tensor
swapdims(x: Tensor, dim0: int, dim1: int)
source

Swap two named dimensions of a tensor.

Returns a view (or, if not contiguous, a freshly materialised tensor) whose dimensions dim0 and dim1 have been exchanged. All other dimensions retain their position.

Parameters

xTensor
Input tensor.
dim0int
First dimension to swap. Negative values count from the end.
dim1int
Second dimension to swap. Negative values count from the end.

Returns

Tensor

Tensor with the same data as x but with axes dim0 and dim1 exchanged.

Notes

For a tensor with shape (s0,,sn1)(s_0, \dots, s_{n-1}), the result has shape

si={sd1,i=d0,sd0,i=d1,si,otherwise.s'_i = \begin{cases} s_{d_1}, & i = d_0, \\ s_{d_0}, & i = d_1, \\ s_i, & \text{otherwise.} \end{cases}

Equivalent to swapaxes; both names are provided for parity with NumPy and reference-framework conventions.

Examples

>>> import lucid
>>> x = lucid.zeros((2, 3, 4))
>>> lucid.swapdims(x, 0, 2).shape
(4, 3, 2)