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 uses axes, starts and ends 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 to starts or ends such as \(-i\), it represents the reverse position of the axis \(i-1\) (here 0 is the initial position). If the value passed to starts or ends 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 of axes must be equal to starts and ends.

Parameters
  • x (Tensor) – The input Tensor (SparseCooTensor or SparseCsrTensor), it’s data type should be float16, float32, float64, int32, int64.

  • axes (list|tuple|Tensor) – The data type is int32.If axes is a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. If axes 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. If starts is a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. If starts is a Tensor, it should be a 1-D Tensor. It represents starting indices of corresponding axis in axes.

  • ends (list|tuple|Tensor) – The data type is int32. If ends is a list or tuple, the elements of it should be integers or a 0-D Tensor with shape []. If ends is a Tensor, it should be a 1-D Tensor. It represents ending indices of corresponding axis in axes.

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])