mindspore.nn.SparseToDense

class mindspore.nn.SparseToDense[source]

Converts a sparse tensor(COOTensor) into dense.

In Python, for the ease of use, three tensors are collected into a SparseTensor class. MindSpore uses three independent dense tensors: indices, value and dense shape to represent the sparse tensor. Separate indexes, values and dense shape tensors can be wrapped in a Sparse Tensor object before being passed to the OPS below.

Inputs:
Outputs:

Tensor, converted from sparse tensor.

Raises
  • TypeError – If sparse_tensor.indices is not a Tensor.

  • TypeError – If sparse_tensor.values is not a Tensor.

  • TypeError – If sparse_tensor.shape is not a tuple.

Supported Platforms:

CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> import mindspore.nn as nn
>>> import mindspore.context as context
>>> context.set_context(mode=context.PYNATIVE_MODE)
>>> indices = Tensor([[0, 1], [1, 2]])
>>> values = Tensor([1, 2], dtype=ms.int32)
>>> dense_shape = (3, 4)
>>> class Net(nn.Cell):
...     def __init__(self, dense_shape):
...         super(Net, self).__init__()
...         self.dense_shape = dense_shape
...         self.op = nn.SparseToDense()
...
...     def construct(self, indices, values):
...         x = COOTensor(indices, values, self.dense_shape)
...         return self.op(x)
...
>>> print(Net(dense_shape)(indices, values))
[[0 1 0 0]
 [0 0 2 0]
 [0 0 0 0]]