mindspore.ops.dense_to_sparse_coo

mindspore.ops.dense_to_sparse_coo(tensor: Tensor)[source]

Convert a Tensor to COOTensor.

Note

Only 2-D tensor is supported for now.

Parameters

tensor (Tensor) – A dense tensor, must be 2-D.

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
Supported Platforms:

GPU

Examples

>>> from mindspore import Tensor, ops
>>> import mindspore as ms
>>> x = Tensor([[1, 0], [-5, 0]], ms.float32)
>>> output = ops.dense_to_sparse_coo(x)
>>> print(output.indices)
[[0 0]
[1 0]]
>>> print(output.values)
[ 1. -5.]
>>> print(output.shape)
(2, 2)