mindspore.ops.squeeze

mindspore.ops.squeeze(input, 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), input Tensor will be changed to (A, B) when \(axis=1\), but when \(axis=0\) or \(axis=2\), an error will occur.

Note

  • Squeezing a dimension that is not 1 will raise an error.

  • Please note that in dynamic graph mode, the output Tensor will share data with the input Tensor, and there is no Tensor data copy process.

  • The dimension index starts at 0 and must be in the range [-input.ndim, input.ndim].

Parameters
  • input (Tensor) – The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • axis (Union[int, tuple(int), list(int)]) – Specifies the dimension indexes of shape to be removed, which will remove all the dimensions of size 1 in the given axis parameter. If specified, it must be int32 or int64. Default: None , an empty tuple will be used.

Returns

Tensor, the shape of tensor is \((x_1, x_2, ..., x_S)\).

Raises
  • TypeError – If input is not a tensor.

  • TypeError – If axis is not an int, tuple or list.

  • TypeError – If axis is a tuple or list whose elements are not all int.

  • ValueError – If the corresponding dimension of the specified axis isn’t equal to 1.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
>>> output = ops.squeeze(input)
>>> print(output)
[[1. 1.]
 [1. 1.]
 [1. 1.]]