mindspore.ops.Squeeze

class mindspore.ops.Squeeze(axis=())[source]

Returns a tensor with the same data type but dimensions of 1 are removed based on axis.

If axis is specified, it will remove the dimensions of size 1 in the given axis. If axis is None, it will remove all the dimensions of size 1. For example, if input is of shape: (A×1×B×C×1×D), then the out tensor will be of shape: (A×B×C×D); When dim is given, a squeeze operation is done only in the given dimension. If input is of shape: (A×1×B), squeeze(input, 0) leaves the tensor unchanged, but squeeze(input, 1) will squeeze the tensor to the shape (A×B).

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.

Note

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

Parameters

axis (Union[int, tuple(int)]) – Specifies the dimension indexes of shape to be removed, which will remove all the dimensions that are equal to 1. If specified, it must be int32 or int64. Default: (), an empty tuple.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

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

Raises
  • TypeError – If axis is neither an int nor tuple.

  • TypeError – If axis is a tuple 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

>>> input_x = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
>>> squeeze = ops.Squeeze(2)
>>> output = squeeze(input_x)
>>> print(output)
[[1. 1.]
 [1. 1.]
 [1. 1.]]