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. 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. 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
- add(other, thresh)[source]
Return the sum with another COOTensor.
- Parameters
- 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:
CPU
GPU
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)[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]
Return the coalesced sparse tensor of the input.
- Returns
COOTensor.
- Supported Platforms:
GPU
- 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.
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Returns
CSRTensor.
- Supported Platforms:
GPU
CPU
- 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.