mindspore.Tensor.to_csr

Tensor.to_csr()[source]

Convert a Tensor to CSRTensor.

Note

Only 2-D tensor is supported for now.

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

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_csr()
>>> print(output.indptr, output.indices, output.values, output.shape)
[0 1 2] [0 0] [ 1. -5.] (2, 2)