slice¶
- paddle. slice ( input, axes, starts, ends ) [source]
-
This operator produces a slice of
inputalong multiple axes. Similar to numpy: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html 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 data tensor. 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 n (the number of elements in this dimension), 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. Following examples will explain how slice works:Case1: Given: data = [ [1, 2, 3, 4], [5, 6, 7, 8], ] axes = [0, 1] starts = [1, 0] ends = [2, 3] Then: result = [ [5, 6, 7], ] Case2: Given: data = [ [1, 2, 3, 4], [5, 6, 7, 8], ] axes = [0, 1] starts = [0, 1] ends = [-1, 1000] # -1 denotes the reverse 0th position of dimension 0. Then: result = [ [2, 3, 4], ] # result = data[0:1, 1:4]- Parameters
-
input (Tensor) – A
Tensor. The data type isfloat16,float32,float64,int32orint64.axes (list|tuple) – The data type is
int32. 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 Tensors with shape [1]. Ifstartsis an Tensor, it should be an 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 Tensors with shape [1]. Ifendsis an Tensor, it should be an 1-D Tensor . It represents ending indices of corresponding axis inaxes.
- Returns
-
A
Tensor. The data type is same asinput. - Return type
-
Tensor
- Raises
-
TypeError – The type of
startsmust be list, tuple or Tensor.TypeError – The type of
endsmust be list, tuple or Tensor.
Examples
import paddle input = paddle.rand(shape=[4, 5, 6], dtype='float32') # example 1: # attr starts is a list which doesn't contain tensor. axes = [0, 1, 2] starts = [-3, 0, 2] ends = [3, 2, 4] sliced_1 = paddle.slice(input, axes=axes, starts=starts, ends=ends) # sliced_1 is input[0:3, 0:2, 2:4]. # example 2: # attr starts is a list which contain tensor. minus_3 = paddle.full([1], -3, "int32") sliced_2 = paddle.slice(input, axes=axes, starts=[minus_3, 0, 2], ends=ends) # sliced_2 is input[0:3, 0:2, 2:4].
