mindspore.ops.strided_slice
- mindspore.ops.strided_slice(input_x, begin, end, strides, 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 based on begin/end index and strides.
Note
begin , end and strides must have the same shape.
begin , end and strides are all 1-D Tensor, and their shape size must not greater than the dim of input_x.
Example: For input_x with shape \((5, 6, 7)\), set begin, end and strides to (1, 3, 2), (3, 5, 6), (1, 1, 2) respectively, then elements from index 1 to 3 are extrected for dim 0, index 3 to 5 are extrected for dim 1 and index 2 to 6 with a stirded of 2 are extrected for dim 2, this process is equivalent to a pythonic slice input_x[1:3, 3:5, 2:6:2].
If the length of begin, end and strides is smaller than the dim of input_x, then all elements are extracted from the missing dims, it behaves like all the missing dims are filled with zeros, size of that missing dim and ones.
Example: For Tensor input_x with shape \((5, 6, 7)\), set begin, end and strides to (1, 3), (3, 5), (1, 1) respectively, then elements from index 1 to 3 are extrected for dim 0, index 3 to 5 are extrected for dim 1 and index 3 to 5 are extrected for dim 2, this process is equivalent to a pythonic slice input_x[1:3, 3:5, 0:7].
Here's how a mask works: For each specific mask, it will be converted to a binary representation internally, and then reverse the result to start the calculation. For Tensor input_x with shape \((5, 6, 7)\). Given mask value of 3 which can be represented as 0b011. Reverse that we get 0b110, which implies the first and second dim of the original Tensor will be effected by this mask. See examples below, for simplicity all mask mentioned below are all in their reverted binary form:
begin_mask and end_mask
If the ith bit of begin_mask is 1, begin[i] is ignored and the fullest possible range in that dimension is used instead. end_mask is analogous, except with the end range. For Tensor input_x with shape \((5, 6, 7, 8)\), if begin_mask is 0b110, end_mask is 0b011, the slice input_x[0:3, 0:6, 2:7:2] is produced.
ellipsis_mask
If the ith bit of ellipsis_mask is 1, as many unspecified dimensions as needed will be inserted between other dimensions. Only one non-zero bit is allowed in ellipsis_mask. For Tensor input_x with shape \((5, 6, 7, 8)\), input_x[2:,…,:6] is equivalent to input_x[2:5,:,:,0:6] , input_x[2:,…] is equivalent to input_x[2:5,:,:,:].
new_axis_mask
If the ith bit of new_axis_mask is 1, begin, end and strides are ignored and a new length 1 dimension is added at the specified position in the output Tensor. For Tensor input_x with shape \((5, 6, 7)\), if new_axis_mask is 0b110, a new dim is added to the second dim, which will produce a Tensor with shape \((5, 1, 6, 7)\).
shrink_axis_mask
If the ith bit of shrink_axis_mask is 1, begin, end and strides are ignored and dimension i will be shrunk to 0. For Tensor input_x with shape \((5, 6, 7)\), if shrink_axis_mask is 0b010, it is equivalent to slice x[:, 5, :] and results in an output shape of \((5, 7)\).
Note
new_axis_mask and shrink_axis_mask are not recommended to use at the same time, it might incur unexpected result.
- Parameters
input_x (Tensor) – The input tensor.
strides (tuple[int]) – Specify the step size for slicing in each dimension.
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
.
- Returns
Tensor
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> input = mindspore.tensor([[[1, 1, 1], ... [2, 2, 2]], ... [[3, 3, 3], ... [4, 4, 4]], ... [[5, 5, 5], ... [6, 6, 6]]]) >>> mindspore.ops.strided_slice(input, [1, 0, 0], [2, 1, 3], [1, 1, 1]) Tensor(shape=[1, 1, 3], dtype=Int64, value= [[[3, 3, 3]]]) >>> mindspore.ops.strided_slice(input, [1, 0, 0], [2, 2, 3], [1, 1, 1]) Tensor(shape=[1, 2, 3], dtype=Int64, value= [[[3, 3, 3], [4, 4, 4]]]) >>> mindspore.ops.strided_slice(input, [1, -1, 0], [2, -3, 3], [1, -1, 1]) Tensor(shape=[1, 2, 3], dtype=Int64, value= [[[4, 4, 4], [3, 3, 3]]])