Skip to content
fn

squeeze

→Tensor
squeeze(input: Tensor, dim: _int | list[_int] | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

Remove dimensions of size 1.

When dim is None every size-1 axis is removed. When dim is given, only that axis is removed and only if its size is 1 (otherwise the input is returned unchanged).

Parameters

inputTensor
Source tensor.
dimint or list of int= None
Specific axis (or axes) to squeeze.

Returns

Tensor

input without the chosen size-1 axes — a view of it on the CPU, a copy on metal.

Notes

Squeeze is the inverse of unsqueeze. Be cautious in generic code: a "safe" squeeze for the last axis is x.squeeze(-1) if x.size(-1) == 1 else x.

Examples

>>> import lucid
>>> x = lucid.zeros(1, 3, 1, 4)
>>> lucid.squeeze(x).shape
(3, 4)