mindspore.Tensor.squeeze

Tensor.squeeze(axis=None)[source]

Remove the dimension of shape 1 from the Tensor

Parameters

axis (Union[None, int, list(int), tuple(int)], optional) – Selects a subset of the entries of length one in the shape. If an axis is selected with shape entry greater than one, an error is raised. Default is None.

Returns

Tensor, with all or a subset of the dimensions of length 1 removed.

Raises
  • TypeError – If input arguments have types not specified above.

  • ValueError – If axis is greater than one.

Supported Platforms:

Ascend GPU CPU

See also

mindspore.Tensor.expand_as(): Expand the dimension of target tensor to the dimension of input tensor.

mindspore.Tensor.reshape(): Give a new shape to a tensor without changing its data.

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((1,2,2), dtype=np.float32))
>>> print(x)
[[[1. 1.]
[1. 1.]]]
>>> print(x.shape)
(1, 2, 2)
>>> y = x.squeeze()
>>> print(y)
[[1. 1.]
[1. 1.]]
>>> print(y.shape)
(2, 2)
>>> y = x.squeeze(axis=0)
>>> print(y)
[[1. 1.]
[1. 1.]]
>>> print(y.shape)
(2, 2)