mindspore.Tensor.item

Tensor.item(index=None)[source]

Get the item at the specified index of the tensor.

Parameters

index (Union[None, int, tuple(int)]) – The index in Tensor. Default: None.

Returns

A scalar, type is defined by the dtype of the Tensor.

Raises

ValueError – If the length of the index is not equal to self.ndim.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor
>>> x = Tensor([[1, 2, 3], [4, 5, 6]], ms.float32)
>>> print(x.item((0, 1)))
2.0
>>> x = Tensor(1.2, ms.float32)
>>> print(x.item())
1.2