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.

Note

This is an experimental feature and is subjected to change. If the length of values or indices exceeds the range indicated by indptr, its behavior will be undefined.

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 mindspore.int16, mindspore.int32 or mindspore.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 mindspore.int16, mindspore.int32 or mindspore.int64.

  • values (Tensor) – 1-D Tensor of shape [N], which has the same length as indices. values stores the data for CSRTensor. Default: None.

  • shape (Tuple) – A tuple indicates the shape of the CSRTensor, its length must be 2, as only 2-D CSRTensor is currently supported, 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. 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

astype(dtype)[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

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

property indices

Return CSRTensor’s column indices.

property indptr

Return CSRTensor’s row indices pointers.

property itemsize

Return the length of one tensor element in bytes.

mv(dense_vector)[source]

Sparse matrix-vector multiplication.

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

Return the number of tensor dimensions.

property shape

Return CSRTensor’s shape.

property size

Return the number of non-zero values.

sum(axis)[source]

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

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.

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

Return CSRTensor’s non-zero values.