mindspore.ops.dense_to_sparse_csr

View Source On Gitee
mindspore.ops.dense_to_sparse_csr(tensor: Tensor)[source]

Convert a Tensor to CSRTensor.

Note

Only 2-D tensor is supported for now.

Parameters

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

Returns

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

  • indptr (Tensor): 1-D integer tensor, indicates the start and end point for values in each row.

  • indices (Tensor): 1-D integer tensor, indicates the column positions of all non-zero values of the input.

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

  • shape (tuple(int)): the shape of the CSRTensor, 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_csr(x)
>>> print(output.indptr)
[0 1 2]
>>> print(output.indices)
[0 0]
>>> print(output.shape)
(2, 2)