文档反馈

问题文档片段

问题文档片段包含公式时,显示为空格。

提交类型
issue

有点复杂...

找人问问吧。

请选择提交类型

问题类型
规范和低错类

- 规范和低错类:

- 错别字或拼写错误,标点符号使用错误、公式错误或显示异常。

- 链接错误、空单元格、格式错误。

- 英文中包含中文字符。

- 界面和描述不一致,但不影响操作。

- 表述不通顺,但不影响理解。

- 版本号不匹配:如软件包名称、界面版本号。

易用性

- 易用性:

- 关键步骤错误或缺失,无法指导用户完成任务。

- 缺少主要功能描述、关键词解释、必要前提条件、注意事项等。

- 描述内容存在歧义指代不明、上下文矛盾。

- 逻辑不清晰,该分类、分项、分步骤的没有给出。

正确性

- 正确性:

- 技术原理、功能、支持平台、参数类型、异常报错等描述和软件实现不一致。

- 原理图、架构图等存在错误。

- 命令、命令参数等错误。

- 代码片段错误。

- 命令无法完成对应功能。

- 界面错误,无法指导操作。

- 代码样例运行报错、运行结果不符。

风险提示

- 风险提示:

- 对重要数据或系统存在风险的操作,缺少安全提示。

内容合规

- 内容合规:

- 违反法律法规,涉及政治、领土主权等敏感词。

- 内容侵权。

请选择问题类型

问题描述

点击输入详细问题描述,以帮助我们快速定位问题。

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)[源代码]

对输入Tensor根据步长和索引进行切片提取。

该算子在给定的 input_x 中提取大小为 (end-begin)/strides 的片段。根据起始索引、结束索引和步长进行提取,直到所有维度的索引都不小于结束索引为止, 返回提取出的切片。

说明

  • beginendstrides 的shape必须相同。

  • beginendstrides 是一维Tensor,且shape小于或等于 input_x 的维度。

切片时,第i维从索引 begin[i] 开始取到索引 end[i] ,步长为 strides[i]

例:input_x 是shape为 (5,6,7) 的三维Tensor, begin 为(1, 3, 2), end 为(3, 5, 6), strides 为(1, 1, 2)。切片时,第零维从索引1开始取到3,步长为1;第一维从索引3开始取到5,步长为1;第二维从索引2开始取到6,步长为2。相当于Python式切片 input_x[1:3, 3:5, 2:6:2]

如果 beginendstrides 的长度小于 input_x 的维度, 则缺失的维度默认切取所有的元素, 相当于 begin 用0补足, end 用相应维度的长度补足, strides 用1补足。

例:input_x 是shape为 (5,6,7) 的三维Tensor, begin 为(1, 3), end 为(3, 5), strides 为(1, 1)。切片时,第零维从索引1开始取到3,步长为1;第一维从索引3开始取到5,步长为1;第二维从索引0开始取到6,步长为1。相当于Python式切片 input_x[1:3, 3:5, 0:7]

mask(掩码)的工作原理:

对于每个特定的mask,内部先将其转化为二进制表示,然后倒序排布后进行计算。比如,对于一个shape为 (5,6,7) 的Tensor,mask设置为3,3转化为二进制表示为0b011,倒序后为0b110,则该mask只在第零维和第一维产生作用。下面各自举例说明,为简化表达,后面提到的mask都表示转换为二进制并且倒序后的值。

  • begin_maskend_mask

    如果 begin_mask 的第i位为1,则忽略 begin[i],第i维从索引0开始取;若 end_mask 的第i位为1,则忽略 end[i],结束的位置为可以取到的最大范围。 对于shape为 (5,6,7,8) 的Tensor,若 begin_mask 为0b110,end_mask 为0b011,将得到切片 input_x[0:3, 0:6, 2:7:2]

  • ellipsis_mask

    如果 ellipsis_mask 的第i位为1,则将在其他维度之间插入所需的任意数量的未指定维度。 ellipsis_mask 中只允许一个非零位。 对于shape为 (5,6,7,8) 的Tensor, input_x[2:,…,:6] 等同于 input_x[2:5,:,:,0:6]input_x[2:,…] 等同于 input_x[2:5,:,:,:]

  • new_axis_mask

    如果 new_axis_mask 的第i位为1,则在输出的第i维添加新的长度为1的维度,并忽略第i维的 beginendstrides 。 对于shape为 (5,6,7) 的Tensor,若 new_axis_mask 为0b010,则第二维将新增一维,输出shape为 (5,1,6,7) 的Tensor。

  • shrink_axis_mask

    如果 shrink_axis_mask 的第i位为1,则第i维被收缩掉,忽略 begin[i]end[i]strides[i] 索引处的值。 对于shape为 (5,6,7) 的Tensor,若 shrink_axis_mask 为0b010, 则第一维收缩掉,相当于切片 x[:, 5, :] 使得输出shape为 (5,7)

说明

new_axis_maskshrink_axis_mask 不建议同时使用,可能会产生预料之外的结果。

参数:
  • input_x (Tensor) - 需要切片处理的输入Tensor。

  • begin (tuple[int]) - 指定开始切片的索引。仅支持大于或等于0的int值。

  • end (tuple[int]) - 指定结束切片的索引。仅支持大于0的int值。

  • strides (tuple[int]) - 指定各维度切片的步长。输入为一个tuple,仅支持int值。strides 的元素必须非零。可能为负值,这会导致反向切片。

  • begin_mask (int,可选) - 表示切片的起始索引掩码。默认值:0。

  • end_mask (int,可选) - 表示切片的结束索引掩码。默认值:0。

  • ellipsis_mask (int,可选) - 维度掩码值为1说明不需要进行切片操作。为int型掩码。默认值:0。

  • new_axis_mask (int,可选) - 表示切片的新增维度掩码。默认值:0。

  • shrink_axis_mask (int,可选) - 表示切片的收缩维度掩码。为int型掩码。默认值:0。

返回:

返回根据起始索引、结束索引和步长进行提取出的切片Tensor。

异常:
  • TypeError - begin_maskend_maskellipsis_masknew_axis_maskshrink_axis_mask 不是int。

  • TypeError - beginendstrides 不是数据类型为int的tuple。

  • ValueError - begin_maskend_maskellipsis_masknew_axis_maskshrink_axis_mask 小于0。

  • ValueError - beginendstrides 的shape不同。

支持平台:

Ascend GPU CPU

样例:

>>> input_x = Tensor([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]],
...                   [[5, 5, 5], [6, 6, 6]]], mindspore.float32)
>>> output = ops.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], strides = [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, strides = 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, strides = 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, strides = 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.]]]