mindspore.COOTensor

class mindspore.COOTensor(indices=None, values=None, shape=None, coo_tensor=None)[source]

A sparse representation of a set of nonzero elements from a tensor at given indices.

For a tensor dense, its COOTensor(indices, values, shape) has dense[indices[i]] = values[i].

For example, if indices is [[0, 1], [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 COOTensor, see operators.

Warning

  • This is an experimental API that is subject to change or deletion.

  • Currently, duplicate coordinates in the indices will not be coalesced. If the indices contain out-of-bound values, the result will be undefined.

Parameters
  • indices (Tensor) – A 2-D integer Tensor of shape \((N, ndims)\), where N and ndims are the number of values and number of dimensions in the COOTensor, respectively. Currently, ndims must be 2. Default: None . Please make sure that the indices are in range of the given shape.

  • values (Tensor) – A 1-D tensor of any type and shape \((N)\), which supplies the values for each element in indices. Default: None .

  • shape (tuple(int)) – An integer tuple of shape \((ndims)\), which specifies the dense_shape of the sparse tensor. Default: None .

  • coo_tensor (COOTensor) – A COOTensor object. Default: None .

Returns

COOTensor, composed of indices, values, and shape.

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (3, 4)
>>> x = COOTensor(indices, values, shape)
>>> print(x.values)
[1. 2.]
>>> print(x.indices)
[[0 1]
 [1 2]]
>>> print(x.shape)
(3, 4)
abs()[source]

Return absolute value element-wisely.

Returns

COOTensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1, 2], [1, 0, 2]], dtype=ms.int32)
>>> values = Tensor([1, -5, -4], dtype=ms.float32)
>>> shape = (3, 3)
>>> coo_tensor = COOTensor(indices.transpose(), values, shape)
>>> res = coo_tensor.abs()
>>> print(res.values)
[1. 5. 4.]
add(other: COOTensor, thresh: Tensor)[source]

Return the sum with another COOTensor.

Parameters
  • other (COOTensor) – the second SparseTensor to sum.

  • thresh (Tensor) – A 0-D Tensor, represents the magnitude threshold that determines if an output value/index pair take space, Its dtype should match that of the values if they are real. If output’s value is less than the thresh, it will vanish.

Returns

COOTensor, representing the sum.

Raises
  • ValueError – If any input(self/other)’s indices’s dim is not equal to 2.

  • ValueError – If any input(self/other)’s values’s dim is not equal to 1.

  • ValueError – If any input(self/other)’s shape’s dim is not equal to 1.

  • ValueError – If thresh’s dim is not equal to 0.

  • TypeError – If any input(self/other)’s indices’s type is not equal to int64.

  • TypeError – If any input(self/other)’s shape’s type is not equal to int64.

  • ValueError – If any input(self/other)’s indices’s length is not equal to its values’s length.

  • TypeError – If any input(self/other)’s values’s type is not equal to anf of (int8/int16/int32/int64/float32/float64/complex64/complex128)

  • TypeError – If thresh’s type is not equal to anf of (int8/int16/int32/int64/float32/float64)

  • TypeError – If self’s indices’s type is not equal to other’s indices’s type

  • TypeError – If self’s values’s type is not equal to other’s values’s type

  • TypeError – If self’s shape’s type is not equal to other’s shape’s type

  • TypeError – If (self/other)’s value’s type is not matched with thresh’s type

Supported Platforms:

GPU CPU

Examples

>>> from mindspore import Tensor, COOTensor
>>> from mindspore import dtype as mstype
>>> indics0 = Tensor([[0, 1], [1, 2]], dtype=mstype.int64)
>>> values0 = Tensor([1, 2], dtype=mstype.int32)
>>> shape0 = (3, 4)
>>> input0 = COOTensor(indics0, values0, shape0)
>>> indics1 = Tensor([[0, 0], [1, 1]], dtype=mstype.int64)
>>> values1 = Tensor([3, 4], dtype=mstype.int32)
>>> shape1 = (3, 4)
>>> input1 = COOTensor(indics1, values1, shape1)
>>> thres = Tensor(0, dtype=mstype.int32)
>>> out = input0.add(input1, thres)
>>> print(out)
COOTensor(shape=[3, 4], dtype=Int32, indices=Tensor(shape=[4, 2], dtype=Int64, value=
[[0 0]
 [0 1]
 [1 1]
 [1 2]]), values=Tensor(shape=[4], dtype=Int32, value=[3 1 4 2]))
astype(dtype: mstype)[source]

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

Parameters

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

Returns

COOTensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (3, 4)
>>> coo_tensor = COOTensor(indices, values, shape)
>>> print(coo_tensor.astype(ms.float64).dtype)
Float64
coalesce()[source]

Returns a coalesced copy of an uncoalesced sparse tensor.

Returns

A COOTensor.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> x_indices = Tensor([[0, 0, 1], [1, 1, 2]], dtype=ms.int64)
>>> x_values = Tensor([1, 5, 4], dtype=ms.float32)
>>> x_shape = (3, 3)
>>> coo_tensor = COOTensor(x_indices.transpose(), x_values, x_shape)
>>> res = coo_tensor.coalesce()
>>> print(res)
COOTensor(shape=[3, 3], dtype=Float32, indices=Tensor(shape=[2, 2], dtype=Int64,
    value=[[0 1] [1 2]]), values=Tensor(shape=[2], dtype=Float32, value=[6.00000000e+00 4.00000000e+00]))
property dtype: mstype

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

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (3, 4)
>>> coo_tensor = COOTensor(indices, values, shape)
>>> print(coo_tensor.dtype)
Float32
property indices: mindspore.common.tensor.Tensor

Return COOTensor’s indices.

property itemsize: int

Return the length of one tensor element in bytes.

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float64)
>>> shape = (3, 4)
>>> coo_tensor = COOTensor(indices, values, shape)
>>> print(coo_tensor.itemsize)
8
property ndim: int

Return the number of tensor dimensions.

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> coo_tensor = COOTensor(indices, values, (3, 4))
>>> print(coo_tensor.ndim)
2
property shape: Tuple[int, ...]

Return COOTensor’s shape.

property size: int

Return the number of non-zero values.

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1, 2], [1, 0, 2]], dtype=ms.int32)
>>> values = Tensor([1, 5, 4], dtype=ms.float32)
>>> shape = (3, 3)
>>> coo_tensor = COOTensor(indices.transpose(), values, shape)
>>> print(coo_tensor.size)
3
to_csr()[source]

Converts COOTensor to CSRTensor.

Note

Currently only supports CPU backend with LLVM 12.0.1 installed.

Returns

CSRTensor.

Supported Platforms:

GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.int32)
>>> shape = (3, 4)
>>> coo_tensor = COOTensor(indices, values, shape)
>>> print(coo_tensor.to_csr())
CSRTensor(shape=[3, 4], dtype=Int32, indptr=Tensor(shape=[4], dtype=Int32, value=[0 1 2 2]),
    indices=Tensor(shape=[2], dtype=Int32, value=[1 2]), values=Tensor(shape=[2], dtype=Int32, value=[1 2]))
to_dense()[source]

Converts COOTensor to Dense Tensor.

Returns

Tensor.

Supported Platforms:

GPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1, 2], [1, 0, 2]], dtype=ms.int32)
>>> values = Tensor([1, 5, 4], dtype=ms.float32)
>>> shape = (3, 3)
>>> coo_tensor = COOTensor(indices.transpose(), values, shape)
>>> print(coo_tensor.to_dense())
[[0. 1. 0.]
 [5. 0. 0.]
 [0. 0. 4.]]
to_tuple()[source]

Return indices, values and shape as a tuple.

Returns

Tuple.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (3, 4)
>>> coo_tensor = COOTensor(indices, values, shape)
>>> print(coo_tensor.to_tuple())
(Tensor(shape=[2, 2], dtype=Int32, value=
[[0, 1],
 [1, 2]]), Tensor(shape=[2], dtype=Float32, value= [ 1.00000000e+00,  2.00000000e+00]), (3, 4))
property values: mindspore.common.tensor.Tensor

Return COOTensor’s non-zero values.