mindspore.Tensor.expand_dims

Tensor.expand_dims(axis)[source]

Insert a dimension of shape 1 at the specified axis of Tensor

Parameters

axis (int) – the axis at which to insert the singleton dimension.

Returns

Tensor, with inserted dimension of length 1.

Raises
  • TypeError – If axis is not an int.

  • ValueError – If axis is not in range [-self.ndim - 1, self.ndim + 1).

Supported Platforms:

Ascend GPU CPU

Examples

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