Skip to content
fn

unsqueeze

→Tensor
unsqueeze(input: Tensor, dim: DimLike)

Implementing kernel

C++ engine symbols that back this Python API.

Insert a new size-1 dimension at position dim.

The output has rank input.ndim + 1. Negative dim indexes from the right (after the new axis is added), so unsqueeze(x, -1) appends a trailing axis.

Parameters

inputTensor
Source tensor.
dimDimLike
Position of the inserted axis.

Returns

Tensor

input with an extra size-1 axis — a view of it on the CPU, a copy on metal.

Notes

Inverse of squeeze (when squeeze is called with the same dim). Pair with expand to broadcast scalars or vectors into higher-rank shapes.

Examples

>>> import lucid
>>> x = lucid.tensor([1, 2, 3])
>>> lucid.unsqueeze(x, 0).shape
(1, 3)