fn

unsqueeze

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

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

View into input with an extra size-1 axis.

Notes

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

Examples

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