mindspore.Tensor.item

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

Return the value of this tensor as standard Python number. This only works for tensors with one element.

Returns

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

Raises

ValueError – If the count of value in tensor is more than one.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor
>>> x = Tensor(1.2, ms.float32)
>>> print(x.item())
1.2