fn

atleast_3d

Tensor | tuple[Tensor, ...]
atleast_3d(tensors: Tensor = ())
source

Promote each input to at least 3 dimensions.

Scalars become shape (1, 1, 1), 1-D tensors become (1, N, 1), 2-D tensors gain a trailing unit axis to become (M, N, 1); tensors with rank 3\geq 3 pass through unchanged.

Parameters

*tensorsTensor
One or more input tensors of arbitrary rank.

Returns

Tensor | tuple[Tensor, ...]

A single tensor when called with one argument, otherwise a tuple of the promoted tensors in the same order as the inputs.

Notes

Promotion rule per input t:

shape(t)={(1,1,1),ndim(t)=0,(1,N,1),shape(t)=(N,),(M,N,1),shape(t)=(M,N),shape(t),ndim(t)3.\text{shape}'(t) = \begin{cases} (1, 1, 1), & \text{ndim}(t) = 0, \\ (1, N, 1), & \text{shape}(t) = (N,), \\ (M, N, 1), & \text{shape}(t) = (M, N), \\ \text{shape}(t), & \text{ndim}(t) \geq 3. \end{cases}

Note the asymmetric padding: 1-D tensors get both a leading and a trailing unit axis (so they live in the middle, image-like axis), while 2-D tensors gain only a trailing channel axis. This matches NumPy and is convenient for image-processing code that expects (H, W, C) arrays.

Examples

>>> import lucid
>>> v = lucid.tensor([1.0, 2.0])
>>> lucid.atleast_3d(v).shape
(1, 2, 1)