mindspore.ops.StridedSlice

View Source On Gitee
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.

Refer to mindspore.ops.strided_slice() for more details.

Parameters
  • begin_mask (int, optional) – Starting index of the slice. Default: 0 .

  • end_mask (int, optional) – Ending index of the slice. Default: 0 .

  • ellipsis_mask (int, optional) – An int mask, ignore slicing operation when set to 1. Default: 0 .

  • new_axis_mask (int, optional) – An int mask for adding new dims. Default: 0 .

  • shrink_axis_mask (int, optional) – An int mask for shrinking dims. Default: 0 .

Inputs:
  • input_x (Tensor) - The input Tensor to be extracted from.

  • begin (tuple[int]) - A tuple which represents the location where to start.

  • end (tuple[int]) - A tuple or which represents the maximum location where to end.

  • strides (tuple[int]) - A tuple which represents the strides is continuously added before reaching the maximum location. Only int is allowed, it can be negative which results in reversed slicing.

Outputs:

Tensor, return the extracts a strided slice of a Tensor based on begin/end index and strides.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, ops
>>> 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.]]]