mindspore.numpy.squeeze

View Source On Gitee
mindspore.numpy.squeeze(a, axis=None)[source]

Return the Tensor after deleting the dimension of size 1 in the specified axis.

If \(axis=None\), it will remove all the dimensions of size 1. If axis is specified, it will remove the dimensions of size 1 in the given axis. For example, if the dimension is not specified \(axis=None\), input shape is (A, 1, B, C, 1, D), then the shape of the output Tensor is (A, B, C, D). If the dimension is specified, the squeeze operation is only performed in the specified dimension. If input shape is (A, 1, B), when \(axis=0\) or \(axis=2\), the input tensor is not changed, while when \(axis=1\), the input tensor shape is changed to (A, B).

Parameters
  • a (Tensor) – Input tensor array.

  • axis (Union[None, int, list(int), tuple(list)], optional) – The axis(axes) to squeeze, default: 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 specified axis has shape entry \(> 1\).

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.numpy as np
>>> x = np.ones((1,2,2,1))
>>> x = np.squeeze(x)
>>> print(x.shape)
(2, 2)