mindspore.ops.unsqueeze

mindspore.ops.unsqueeze(input, dim)[source]

Adds an additional dimension to input at the given dim.

Parameters
  • input (Tensor) – The shape of tensor is \((n_1, n_2, ..., n_R)\).

  • dim (int) – Specifies the dimension index at which to expand the shape of input. The value of dim must be in the range [-input.ndim-1, input.ndim]. Only constant value is allowed.

Returns

Tensor, the shape of tensor is \((1, n_1, n_2, ..., n_R)\) if the value of dim is 0. It has the same data type as input.

Raises
  • TypeError – If dim is not an int.

  • ValueError – If dim is not in the valid range \([-input.ndim-1, input.ndim]\).

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
>>> output = ops.unsqueeze(input_tensor, dim=0)
>>> print(output)
[[[2. 2.]
  [2. 2.]]]