mygrad.squeeze#

mygrad.squeeze(a: ArrayLike, axis: None | int | Tuple[int, ...] = None, *, constant: bool | None = None) Tensor[source]#

Remove single-dimensional entries from the shape of a tensor.

This docstring was adapted from numpy.squeeze

Parameters:
aArrayLike

The tensor to be reshaped

axisOptional[int, Tuple[int, …]]

Selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.

constantbool, optional(default=False)

If True, the returned tensor is a constant (it does not back-propagate a gradient)

Returns:
mygrad.Tensor
Raises:
ValueError

If axis is not None, and an axis being squeezed is not of length 1

Examples

>>> import mygrad as mg
>>> x = mg.Tensor([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> mg.squeeze(x).shape
(3,)
>>> mg.squeeze(x, axis=0).shape
(3, 1)
>>> mg.squeeze(x, axis=1).shape
Traceback (most recent call last) -> Tensor:
...
ValueError: cannot select an axis to squeeze out which has size not equal to one
>>> mg.squeeze(x, axis=2).shape
(1, 3)