mindspore.ops.dense_to_sparse_csr
- mindspore.ops.dense_to_sparse_csr(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
TypeError – If input is not a tensor.
ValueError – If input tensor is not 2-D.
- 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)