mindspore.ops.StridedSlice
- class mindspore.ops.StridedSlice(begin_mask=0, end_mask=0, ellipsis_mask=0, new_axis_mask=0, shrink_axis_mask=0)[source]
Extracts a strided slice of a tensor.
Given an input tensor, this operation inserts a dimension of length 1 at the dimension. This operation extracts a fragment of size (end-begin)/stride from the given ‘input_tensor’. Starting from the beginning position, the fragment continues adding stride to the index until all dimensions are not less than the ending position.
Given a input_x[m1, m2, …, mn], begin, end and strides will be vectors of length n.
In each mask field (begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask) the ith bit will correspond to the ith m.
If the ith bit of begin_mask is set, begin[i] is ignored and the fullest possible range in that dimension is used instead. end_mask is analogous, except with the end range.
As for a 5*6*7 tensor, x[2:,:3,:] is equivalent to x[2:5,0:3,0:7].
If the ith bit of ellipsis_mask is set, as many unspecified dimensions as needed will be inserted between other dimensions. Only one non-zero bit is allowed in ellipsis_mask.
As for a 5*6*7*8 tensor, x[2:,…,:6] is equivalent to x[2:5,:,:,0:6]. x[2:,…] is equivalent to x[2:5,:,:,:].
If the ith bit of new_axis_mask is set, begin, end and strides are ignored and a new length 1 dimension is added at the specified position in tthe output tensor.
As for a 5*6*7 tensor, x[:2, newaxis, :6] will produce a tensor with shape \((2, 1, 7)\) .
If the ith bit of shrink_axis_mask is set, ith size shrinks the dimension by 1, taking on the value at index begin[i], end[i] and strides[i] are ignored.
As for a 5*6*7 tensor, x[:, 5, :] will result in shrink_axis_mask equal to 4.
Note
The stride may be negative value, which causes reverse slicing. The shape of begin, end and strides must be the same. begin and end are zero-indexed. The element of strides must be non-zero.
- Parameters
- Inputs:
input_x (Tensor) - The input Tensor.
begin (tuple[int]) - A tuple which represents the location where to start. Only constant value is allowed.
end (tuple[int]) - A tuple or which represents the maximum location where to end. Only constant value is allowed.
strides (tuple[int]) - A tuple which represents the stride is continuously added before reaching the maximum location. Only constant value is allowed.
- Outputs:
Tensor, The output is explained by following example.
In the 0th dimension, begin is 1, end is 2, and strides is 1, because \(1+1=2\geq2\), the interval is \([1,2)\). Thus, return the element with \(index = 1\) in 0th dimension, i.e., [[3, 3, 3], [4, 4, 4]].
In the 1st dimension, similarly, the interval is \([0,1)\). Based on the return value of the 0th dimension, return the element with \(index = 0\), i.e., [3, 3, 3].
In the 2nd dimension, similarly, the interval is \([0,3)\). Based on the return value of the 1st dimension, return the element with \(index = 0,1,2\), i.e., [3, 3, 3].
Finally, the output is [3, 3, 3].
- Raises
TypeError – If begin_mask, end_mask, ellipsis_mask, new_axis_mask or shrink_axis_mask is not an int.
TypeError – If begin, end or strides is not a tuple.
ValueError – If begin_mask, end_mask, ellipsis_mask, new_axis_mask or shrink_axis_mask is less than 0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], ... [[5, 5, 5], [6, 6, 6]]], mindspore.float32) >>> # [[[1. 1. 1.] >>> # [2. 2. 2.]] >>> # >>> # [[3. 3. 3.] >>> # [4. 4. 4.]] >>> # >>> # [[5. 5. 5.] >>> # [6. 6. 6.]]] >>> # In order to visually view the multi-dimensional array, write the above as follows: >>> # [ >>> # [ >>> # [1,1,1] >>> # [2,2,2] >>> # ] >>> # [ >>> # [3,3,3] >>> # [4,4,4] >>> # ] >>> # [ >>> # [5,5,5] >>> # [6,6,6] >>> # ] >>> # ] >>> strided_slice = ops.StridedSlice() >>> output = strided_slice(input_x, (1, 0, 2), (3, 1, 3), (1, 1, 1)) >>> # Take this " output = strided_slice(input_x, (1, 0, 2), (3, 1, 3), (1, 1, 1)) " as an example, >>> # start = [1, 0, 2] , end = [3, 1, 3], stride = [1, 1, 1], Find a segment of (start, end), >>> # note that end is an open interval >>> # To facilitate understanding, this operator can be divided into three steps: >>> # Step 1: Calculation of the first dimension: >>> # start = 1, end = 3, stride = 1, So can take 1st, 2nd rows, and then gets the final output at this time. >>> # output_1th = >>> # [ >>> # [ >>> # [3,3,3] >>> # [4,4,4] >>> # ] >>> # [ >>> # [5,5,5] >>> # [6,6,6] >>> # ] >>> # ] >>> # Step 2: Calculation of the second dimension >>> # 2nd dimension, start = 0, end = 1, stride = 1. So only 0th rows can be taken, and the output at this time. >>> # output_2nd = >>> # [ >>> # [ >>> # [3,3,3] >>> # ] >>> # [ >>> # [5,5,5] >>> # ] >>> # ] >>> # Step 3: Calculation of the third dimension >>> # 3nd dimension,start = 2, end = 3, stride = 1, So can take 2th cols, >>> # and you get the final output at this time. >>> # output_3ed = >>> # [ >>> # [ >>> # [3] >>> # ] >>> # [ >>> # [5] >>> # ] >>> # ] >>> # The final output after finishing is: >>> print(output) [[[3.]] [[5.]]] >>> # another example like : >>> output = strided_slice(input_x, (1, 0, 0), (2, 1, 3), (1, 1, 1)) >>> print(output) [[[3. 3. 3.]]]