fn

stack

Tensor
stack(tensors: list[Tensor], dim: DimLike = ...)
source

Stack tensors along a new dimension.

All inputs must share the same shape. The output has rank input.ndim + 1dim controls where the new axis is inserted. Contrast with cat, which concatenates along an existing dim.

Parameters

tensorslist of Tensor
Equal-shaped tensors to stack.
dimDimLike= ...
Index at which to insert the new dim.

Returns

Tensor

Tensor of shape input.shape with size len(tensors) inserted at position dim.

Notes

Equivalent to cat([t.unsqueeze(dim) for t in tensors], dim=dim) but implemented as a single fused copy.

Examples

>>> import lucid
>>> a = lucid.tensor([1, 2])
>>> b = lucid.tensor([3, 4])
>>> lucid.stack([a, b], dim=0).shape
(2, 2)