fn

atleast_2d

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

Promote each input to at least 2 dimensions.

Scalars become shape (1, 1), 1-D tensors gain a leading unit axis to become (1, N); tensors that already have rank 2\geq 2 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 with original shape s\mathbf{s}:

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

The leading axis insertion (rather than trailing) matches NumPy's np.atleast_2d convention.

Examples

>>> import lucid
>>> v = lucid.tensor([1.0, 2.0, 3.0])
>>> lucid.atleast_2d(v).shape
(1, 3)