sparse_csr_tensor¶
- paddle.sparse. sparse_csr_tensor ( crows: list[int] | tuple[int, ...] | npt.NDArray[np.int_] | Tensor, cols: list[int] | tuple[int, ...] | npt.NDArray[np.int_] | Tensor, values: NumericSequence | npt.NDArray[Any] | Tensor, shape: ShapeLike | None = None, dtype: DTypeLike | None = None, place: CPUPlace | CUDAPinnedPlace | CUDAPlace | str | None = None, stop_gradient: bool = True ) Tensor [source]
-
Constructs a sparse
paddle.Tensor
in CSR(Compressed Sparse Row) format according to thecrows
,cols
andvalues
. Currently, the crows and cols of each batch must be incremented.- Parameters
-
crows (list|tuple|ndarray|Tensor) – 1-D array, each element in the rows represents the starting position of the first non-zero element of each row in values. Can be a list, tuple, numpy.ndarray, paddle.Tensor.
cols (list|tuple|ndarray|Tensor) – 1-D array, the column of non-zero elements. Can be a list, tuple, numpy.ndarray, paddle.Tensor.
values (list|tuple|ndarray|Tensor) – 1-D array, the non-zero elements. 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. hold all elements.
dtype (str|np.dtype|None, 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 fromget_default_type
.place (CPUPlace|CUDAPinnedPlace|CUDAPlace|str|None, optional) – The place to allocate Tensor. Can be CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place. If
place
is string, It can becpu
,gpu:x
andgpu_pinned
, wherex
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
crows
,cols
andvalues
. - Return type
-
Tensor
Examples
>>> import paddle >>> crows = [0, 2, 3, 5] >>> cols = [1, 3, 2, 0, 1] >>> values = [1, 2, 3, 4, 5] >>> dense_shape = [3, 4] >>> csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) >>> print(csr) Tensor(shape=[3, 4], dtype=paddle.int64, place=Place(cpu), stop_gradient=True, crows=[0, 2, 3, 5], cols=[1, 3, 2, 0, 1], values=[1, 2, 3, 4, 5])