slice
- paddle.sparse. slice ( x: Tensor, axes: Sequence[int] | Sequence[Tensor] | Tensor, starts: Sequence[int] | Sequence[Tensor] | Tensor, ends: Sequence[int] | Sequence[Tensor] | Tensor, name: str | None = None ) Tensor [source]
-
This operator produces a slice of
xalong multiple axes for sparse tensors. Slice usesaxes,startsandendsattributes to specify the start and end dimension for each axis in the list of axes and Slice uses this information to slice the input sparse tensor (x). If a negative value is passed tostartsorendssuch as \(-i\), it represents the reverse position of the axis \(i-1\) (here 0 is the initial position). If the value passed tostartsorendsis greater than the number of elements in the dimension (n), it represents n. For slicing to the end of a dimension with unknown size, it is recommended to pass in INT_MAX. The size ofaxesmust be equal tostartsandends.- Parameters
-
x (Tensor) – The input Tensor (
SparseCooTensororSparseCsrTensor), it’s data type should befloat16,float32,float64,int32,int64.axes (list|tuple|Tensor) – The data type is
int32.Ifaxesis a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. Ifaxesis a Tensor, it should be a 1-D Tensor. Axes that starts and ends apply to.starts (list|tuple|Tensor) – The data type is
int32. Ifstartsis a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. Ifstartsis a Tensor, it should be a 1-D Tensor. It represents starting indices of corresponding axis inaxes.ends (list|tuple|Tensor) – The data type is
int32. Ifendsis a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. Ifendsis a Tensor, it should be a 1-D Tensor. It represents ending indices of corresponding axis inaxes.
- Returns
-
A Sparse Tensor. The data type is same as
x.
Examples
>>> import paddle >>> import numpy as np >>> format = 'coo' >>> np_x = np.asarray([[4, 0, 7, 0], [0, 0, 5, 0], [-4, 2, 0, 0]]) >>> dense_x = paddle.to_tensor(np_x) >>> if format == 'coo': ... sp_x = dense_x.to_sparse_coo(len(np_x.shape)) >>> else: ... sp_x = dense_x.to_sparse_csr() ... >>> axes = [0, 1] >>> starts = [1, 0] >>> ends = [3, -2] >>> sp_out = paddle.sparse.slice(sp_x, axes, starts, ends) >>> # sp_out is x[1:3, 0:-2] >>> print(sp_out) Tensor(shape=[2, 2], dtype=paddle.int64, place=Place(cpu), stop_gradient=True, indices=[[1, 1], [0, 1]], values=[-4, 2])
