mindspore.CSRTensor

class mindspore.CSRTensor(indptr=None, indices=None, values=None, shape=None, csr_tensor=None)[source]

Constructs a sparse tensor in CSR (Compressed Sparse Row) format, with specified values indicated by values and row and column positions indicated by indptr and indices.

For example, if indptr is [0, 1, 2, 2], indices is [1, 2], values is [1., 2.], shape is (3, 4), then the dense representation of the sparse tensor will be:

[[0., 1., 0., 0.],
 [0., 0., 2., 0.],
 [0., 0., 0., 0.]]

Common arithmetic operations include: addition (+), subtraction (-), multiplication (*), and division (/). For details about operations supported by CSRTensor, see operators.

Warning

  • This is an experimental API that is subjected to change.

  • If the values given by indptr or indices are invalid, the results may be undefined. Invalid values include when the length of values or indices exceeds the range indicated by indptr, and when the columns indicated by indices are repeated on the same row.

Parameters
  • indptr (Tensor) – 1-D Tensor of shape \((M)\), which equals to shape[0] + 1, which indicates the start and end point for values in each row. Default: None. If provided, must be int16, int32 or int64.

  • indices (Tensor) – 1-D Tensor of shape \((N)\), which has the same length as values. indices indicates the which column values should be placed. Default: None. If provided, must be int16, int32 or int64.

  • values (Tensor) – Tensor, which has the same length as indices (values.shape[0] == indices.shape[0]). values stores the data for CSRTensor. Default: None.

  • shape (tuple(int)) – A tuple indicates the shape of the CSRTensor, and shape[0] must equal to M - 1, which all equal to number of rows of the CSRTensor. Default: None.

  • csr_tensor (CSRTensor) – A CSRTensor object. Values’ feature dimension should match with CSRTensor’s feature dimension (values.shape[1:] == csr_tensor.shape[2:]). Default: None.

Outputs:

CSRTensor, with shape defined by shape, and dtype inferred from value.

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, CSRTensor
>>> # initialize a csr_tensor with indptr, indices, values and shape
>>> indptr = Tensor([0, 1, 2], dtype=ms.int32)
>>> indices = Tensor([0, 1], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, shape)
>>> # access a data member of CSRTensor
>>> print(indptr == csr_tensor.indptr)
[ True  True  True]
abs()[source]

Return absolute value element-wisely.

Returns

CSRTensor, with all values being non-negative.

Supported Platforms:

Ascend GPU CPU

add(b: CSRTensor, alpha: Tensor, beta: Tensor)[source]

Addition of two CSR Tensors : C = alpha * A + beta * B

Parameters
  • b (CSRTensor) – Sparse CSR Tensor.

  • alpha (Tensor) – Dense Tensor, its shape must be able to broadcast to self.

  • beta (Tensor) – Dense Tensor, its shape must be able to broadcast to b.

Returns

CSRTensor.

Supported Platforms:

GPU CPU

Examples

>>> from mindspore import Tensor, CSRTensor
>>> import mindspore.common.dtype as mstype
>>> indptr = Tensor([0, 1, 2], dtype=mstype.int32)
>>> indices = Tensor([0, 1], dtype=mstype.int32)
>>> values_a = Tensor([2, 1], dtype=mstype.float32)
>>> values_b = Tensor([1, 2], dtype=mstype.float32)
>>> dense_shape = (2, 4)
>>> alpha = Tensor(1, mstype.float32)
>>> beta = Tensor(1, mstype.float32)
>>> a = CSRTensor(indptr, indices, values_a, dense_shape)
>>> b = CSRTensor(indptr, indices, values_b, dense_shape)
>>> print(a.add(b, alpha, beta))
    CSRTensor(shape=[2, 4], dtype=Float32,                           indptr=Tensor(shape=[3], dtype=Int32, value=[0 1 2]),                           indices=Tensor(shape=[2], dtype=Int32, value=[0 1]),                           values=Tensor(shape=[2], dtype=Float32, value=[ 3.00000000e+00  3.00000000e+00]))
astype(dtype: mstype)[source]

Return a copy of the CSRTensor, cast its values to a specified type.

Parameters

dtype (Union[mindspore.dtype, numpy.dtype, str]) – Designated tensor dtype.

Returns

CSRTensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, CSRTensor
>>> indptr = Tensor([0, 1, 2], dtype=ms.int32)
>>> indices = Tensor([0, 1], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, shape)
>>> print(csr_tensor.astype(ms.float64).dtype)
Float64
property dtype: mstype

Return the dtype of the values of CSRTensor (mindspore.dtype).

property indices: mindspore.common.tensor.Tensor

Return CSRTensor’s column indices.

property indptr: mindspore.common.tensor.Tensor

Return CSRTensor’s row indices pointers.

property itemsize: int

Return the length of one tensor element in bytes.

mm(matrix: Union[Tensor, CSRTensor])[source]

Return the matrix multiplication result of the right-multiply matrix(dense or CSRTensor) of the CSRTensor. The CSRTensor with shape [M, N] needs to adapt the right matrix with shape [N, K] to get the dense matrix or CSRTensor with result [M, K].

Note

If right matrix is CSRTensor, currently only supports GPU backend. If right matrix is Tensor, currently supports CPU backend with LLVM no lower than 12.0.1, and GPU backend.

Parameters

matrix (Tensor or CSRTensor) – A dense Tensor or CSRTensor, its shape[0] should be equal to csr_tensor.shape[1]

Returns

Tensor or CSRTensor.

Supported Platforms:

GPU CPU

Examples

>>> from mindspore import Tensor, CSRTensor
>>> from mindspore import dtype as mstype
>>> indptr = Tensor([0, 1, 2], dtype=mstype.int32)
>>> indices = Tensor([0, 1], dtype=mstype.int32)
>>> values = Tensor([2, 1], dtype=mstype.float32)
>>> dense_shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape)
>>> dense_matrix = Tensor([[1., 2.], [1, 2.], [1, 2.], [1., 2.]], dtype=mstype.float32)
>>> print(csr_tensor.mm(dense_matrix))
[[2. 4.]
[1. 2.]]
mv(dense_vector: Tensor)[source]

Return the matrix multiplication result of the right-multiply dense matrix of the CSRTensor. The CSRTensor with shape [M, N] needs to adapt the dense vector with shape [N, 1] to get the dense vector with result [M, 1].

Note

Currently only supports CPU backend with LLVM 12.0.1 installed.

Parameters

dense_vector (Tensor) – A dense Tensor, its shape must be (csr_tensor.shape[1], 1)

Returns

Tensor.

Supported Platforms:

GPU CPU

Examples

>>> from mindspore import Tensor, CSRTensor
>>> from mindspore import dtype as mstype
>>> indptr = Tensor([0, 1, 2], dtype=mstype.int32)
>>> indices = Tensor([0, 1], dtype=mstype.int32)
>>> values = Tensor([2, 1], dtype=mstype.float32)
>>> dense_shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape)
>>> dense = Tensor([[1], [1], [1], [1]], dtype=mstype.float32)
>>> print(csr_tensor.mv(dense))
[[2.]
[1.]]
property ndim: int

Return the number of tensor dimensions.

property shape: Tuple[int, ...]

Return CSRTensor’s shape.

property size: int

Return the number of non-zero values.

sum(axis: int)[source]

Reduces a dimension of a CSRTensor by summing all elements in the dimension.

Note

Currently only supports CPU backend with LLVM 12.0.1 installed.

Parameters

axis (int) – The dimensions to reduce.

Returns

Tensor, the dtype is the same as CSRTensor.values.

Supported Platforms:

GPU CPU

Examples

>>> from mindspore import Tensor, CSRTensor
>>> from mindspore import dtype as mstype
>>> indptr = Tensor([0, 1, 2], dtype=mstype.int32)
>>> indices = Tensor([0, 1], dtype=mstype.int32)
>>> values = Tensor([2, 1], dtype=mstype.float32)
>>> dense_shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape)
>>> print(csr_tensor.sum(1))
[[2.]
[1.]]
to_coo()[source]

Converts CSRTensor to COOTensor.

Note

Currently only supports CPU backend with LLVM 12.0.1 installed.

Returns

COOTensor.

Supported Platforms:

GPU CPU

to_dense()[source]

Converts CSRTensor to Dense Tensor.

Returns

Tensor.

Supported Platforms:

GPU

to_tuple()[source]

Return indptr, indices, values and shape as a tuple.

Returns

Tuple.

Supported Platforms:

Ascend GPU CPU

property values: mindspore.common.tensor.Tensor

Return CSRTensor’s non-zero values.