mindspore.RowTensor

class mindspore.RowTensor(indices=None, values=None, shape=None, row_tensor=None)[source]

A sparse representation of a set of tensor slices at given indices.

When the values of a RowTensor has a shape of \((d_0, d_1, ..., d_n)\), then this RowTensor is used to represent a subset of a larger dense tensor of shape \((l_0, d_1, ..., d_n)\), where \(d_i\) is the size of i-th axis in RowTensor, \(l_0\) is the size of 0-th axis of dense tensor and it satisfies \(l_0 > d_0\).

The parameter indices is used to specify locations from which the RowTensor is sliced in the first dimension of the dense tensor, which means the parameters indices and values have the following relationship \(dense[indices[i], :, :, :, ...] = values[i, :, :, :, ...]\).

For example, if indices is [0], values is [[1, 2]], shape is \((3, 2)\) , then the dense representation of the row tensor will be:

[[1, 2],
 [0, 0],
 [0, 0]]

Warning

This is an experimental API that is subjected to change or deletion.

Parameters
  • indices (Tensor) – A 1-D integer Tensor of shape \((d_0)\) . Default: None.

  • values (Tensor) – A Tensor of any dtype of shape \((d_0, d_1, ..., d_n)\) . Default: None.

  • shape (tuple(int)) – An integer tuple which contains the shape of the corresponding dense tensor. Default: None.

  • row_tensor (RowTensor) – A RowTensor object. Default: None.

Returns

RowTensor, composed of indices, values, and shape.

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, RowTensor
>>> indices = Tensor([0])
>>> values = Tensor([[1, 2]], dtype=ms.float32)
>>> shape = (3, 2)
>>> x = RowTensor(indices, values, shape)
>>> print(x.values)
[[1. 2.]]
>>> print(x.indices)
[0]
>>> print(x.dense_shape)
(3, 2)