mindspore.Tensor.item

View Source On Gitee
Tensor.item(index=None)[source]

Get the item at the specified index of the tensor.

Note

Tensor.item returns a Tensor scalar instead of a Python scalar. And if the tensor is a Tensor scalar, Tensor.item will return the numpy.ndarray.

Parameters

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

Returns

A Tensor scalar, dtype is the same with the original 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