sparse_coo_tensor

paddle.sparse. sparse_coo_tensor ( indices, values, shape=None, dtype=None, place=None, stop_gradient=True ) [source]

Constructs a sparse paddle.Tensor in coordinate format according to the indices and values of the specified non-zero elements.

Parameters
  • indices (list|tuple|ndarray|Tensor) – the indices of non-zero elements. Can be a list, tuple, numpy.ndarray, paddle.Tensor. The indices must be 2-D.

  • values (list|tuple|ndarray|Tensor) – Initial values for the tensor. Can be a scalar, list, tuple, numpy.ndarray, paddle.Tensor.

  • shape (list|tuple, optional) – The shape of the sparse tensor also represents the shape of original dense tensor. If not provided the smallest shape will be inferred to hold all elements.

  • dtype (str|np.dtype, optional) – The desired data type of returned tensor. Can be ‘bool’ , ‘float16’ , ‘float32’ , ‘float64’ , ‘int8’ , ‘int16’ , ‘int32’ , ‘int64’ , ‘uint8’, ‘complex64’ , ‘complex128’. Default: None, infers dtype from data except for python float number which gets dtype from get_default_type .

  • place (CPUPlace|CUDAPinnedPlace|CUDAPlace|str, optional) – The place to allocate Tensor. Can be CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place. If place is string, It can be cpu, gpu:x and gpu_pinned, where x is the index of the GPUs.

  • stop_gradient (bool, optional) – Whether to block the gradient propagation of Autograd. Default: True.

Returns

A Tensor constructed from indices and values .

Return type

Tensor

Examples

>>> import paddle

>>> indices = [[0, 1, 2], [1, 2, 0]]
>>> values = [1.0, 2.0, 3.0]
>>> dense_shape = [3, 3]
>>> coo = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape)
>>> print(coo)
Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(cpu), stop_gradient=True,
       indices=[[0, 1, 2],
                [1, 2, 0]],
       values=[1., 2., 3.])