slice¶
- paddle.sparse. slice ( x, axes, starts, ends, name=None ) [source]
-
This operator produces a slice of
x
along multiple axes for sparse tensors. Slice usesaxes
,starts
andends
attributes 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 tostarts
orends
such as \(-i\), it represents the reverse position of the axis \(i-1\) (here 0 is the initial position). If the value passed tostarts
orends
is 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 ofaxes
must be equal tostarts
andends
.- Parameters
-
x (Tensor) – The input Tensor (
SparseCooTensor
orSparseCsrTensor
), it’s data type should befloat16
,float32
,float64
,int32
,int64
.axes (list|tuple|Tensor) – The data type is
int32
.Ifaxes
is a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. Ifaxes
is 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
. Ifstarts
is a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. Ifstarts
is 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
. Ifends
is a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. Ifends
is 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])