mindspore.Tensor.to_coo

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

Convert a Tensor to COOTensor.

Note

Only 2-D tensor is supported for now.

Returns

COOTensor, a sparse representation of the original dense tensor, containing the following parts.

  • indices (Tensor): 2-D integer tensor, indicates the positions of values of the dense tensor.

  • values (Tensor): 1-D tensor, indicates the non-zero values of the dense tensor.

  • shape (tuple(int)): the shape of the COOTensor, is the same as the original dense tensor.

Raises

ValueError – If input tensor is not 2-D.

Supported Platforms:

GPU

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[1,  0], [-5, 0]]), mindspore.float32)
>>> output = x.to_coo()
>>> print(output.indices, output.values, output.shape)
[[0 0]
 [1 0]] [ 1. -5.] (2, 2)