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]]

Note

This is an experimental feature and is subjected to change. Currently, duplicate coordinates in the indices will not be coalesced.

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. 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.

  • shape (tuple(int)) – A integer tuple of size ndims, which specifies the dense_shape of the sparse tensor.

  • coo_tensor (COOTensor) – A COOTensor object.

Returns

COOTensor, composed of indices, values, and shape.

Examples

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> 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

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

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

property indices

Return COOTensor’s indices.

property itemsize

Return the length of one tensor element in bytes.

property ndim

Return the number of tensor dimensions.

property shape

Return COOTensor’s shape.

property size

Return the number of non-zero values.

to_csr()[source]

Converts COOTensor to CSRTensor.

Returns

CSRTensor.

Supported Platforms:

GPU CPU

to_dense()[source]

Converts COOTensor to Dense Tensor.

Returns

Tensor.

Supported Platforms:

GPU

to_tuple()[source]

Return indices, values and shape as a tuple.

Returns

Tuple.

Supported Platforms:

Ascend GPU CPU

property values

Return COOTensor’s non-zero values.