提交 967dcd88 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!71 add tensor assignment document

Merge pull request !71 from candanzg/master
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
- [System Functions](#system-functions) - [System Functions](#system-functions)
- [Function Parameters](#function-parameters) - [Function Parameters](#function-parameters)
- [Operators](#operators) - [Operators](#operators)
- [Slicing Operations](#slicing-operations) - [Index operation](#index-operation)
- [Unsupported Syntax](#unsupported-syntax) - [Unsupported Syntax](#unsupported-syntax)
- [Network Definition Constraints](#network-definition-constraints) - [Network Definition Constraints](#network-definition-constraints)
- [Instance Types on the Entire Network](#instance-types-on-the-entire-network) - [Instance Types on the Entire Network](#instance-types-on-the-entire-network)
...@@ -86,32 +86,41 @@ ...@@ -86,32 +86,41 @@
| `%` |Scalar and `Tensor` | `%` |Scalar and `Tensor`
| `[]` |The operation object type can be `list`, `tuple`, or `Tensor`. Accessed multiple subscripts of lists and dictionaries can be used as r-values instead of l-values. The index type cannot be Tensor. For details about access constraints for the tuple and Tensor types, see the description of slicing operations. | `[]` |The operation object type can be `list`, `tuple`, or `Tensor`. Accessed multiple subscripts of lists and dictionaries can be used as r-values instead of l-values. The index type cannot be Tensor. For details about access constraints for the tuple and Tensor types, see the description of slicing operations.
### Slicing Operations ### Index operation
* `tuple` slicing operation: `tuple_x[start:stop:step]` The index operation includes `tuple` and` Tensor`. The following focuses on the index value assignment and assignment operation of `Tensor`. The value takes` tensor_x [index] `as an example, and the assignment takes` tensor_x [index] = u` as an example for detailed description. Among them, tensor_x is a `Tensor`, which is sliced; index means the index, u means the assigned value, which can be` scalar` or `Tensor (size = 1)`. The index types are as follows:
- `tuple_x` indicates a tuple on which the slicing operation is performed.
- `start`: index where the slice starts. The value is of the `int` type, and the value range is `[-length(tuple_x), length(tuple_x) - 1]`. Default values can be used. The default settings are as follows: - Slice index: index is `slice`
- When `step > 0`, the default value is `0`. - Value: `tensor_x[start: stop: step]`, where Slice (start: stop: step) has the same syntax as Python, and will not be repeated here.
- When `step < 0`, the default value is `length(tuple_x) - 1`. - Assignment: `tensor_x[start: stop: step] = u`.
- `end`: index where the slice ends. The value is of the `int` type, and the value range is `[-length(tuple_x) - 1, length(tuple_x)]`. Default values can be used. The default settings are as follows:
- When `step > 0`, the default value is `length(tuple_x)`. - Ellipsis index: index is `ellipsis`
- When `step < 0`, the default value is `-1`. - Value: `tensor_x [...]`.
- `step`: slicing step. The value is of the `int` type, and its range is `step! = 0`. The default value `1` can be used. - Assignment: `tensor_x [...] = u`.
* `Tensor` slicing operation: `tensor_x[start0:stop0:step0, start1:stop1:step1, start2:stop2:step2]` - Boolean constant index: index is `True`, index is `False` is not supported temporarily.
- `tensor_x` indicates a `Tensor` with at least three dimensions. The slicing operation is performed on it. - Value: `tensor_x[True]`.
- `start0`: index where the slice starts in dimension 0. The value is of the `int` type. Default values can be used. The default settings are as follows: - Assignment: Not supported yet.
- When `step > 0`, the default value is `0`. - Tensor index: index is `Tensor`
- When `step < 0`, the default value is `-1`. - Value: Not supported yet.
- `end0`: index where the slice ends in dimension 0. The value is of the `int` type. Default values can be used. The default settings are as follows: - Assignment: `tensor_x[index] = u`, `index` only supports `Tensor` of type` bool`.
- When `step > 0`, the default value is `length(tuple_x)`. - None constant index: index is `None`
- When `step < 0`, the default value is `-(1 + length(tuple_x))`. - Value: `tensor_x[None]`, results are consistent with numpy.
- `step0`: slicing step in dimension 0. The value is of the `int` type, and its range is `step! = 0`. The default value `1` can be used. - Assignment: Not supported yet.
- If the number of dimensions for slicing is less than that for `Tensor`, all elements are used by default if no slice dimension is specified. - tuple index: index is `tuple`
- Slice dimension reduction operation: If an integer index is transferred to a dimension, the elements of the corresponding index in the dimension is obtained and the dimension is eliminated. For example, after `tensor_x[2:4:1, 1, 0:5:2]` with shape (4, 3, 6) is sliced, a `Tensor` with shape (2, 3) is generated. The first dimension of the original `Tensor` is eliminated. - The tuple element is a slice:
* Ellipsis as indexing: Get the all elements of the dimensions which is ignored by ellipsis. For example, tensor_x with shape(3, 4, 5, 6), `tensor_x[1:3:1, ..., 0:5:2]` will result in a shape of (2,4,5,3). - Value: for example `tensor_x[::,: 4, 3: 0: -1]`.
* None as indexing: For a tensor shape with (3,4,5), operation `tensor_x[None]` will result in a tensor with shape (1, 3, 4, 5). - Assignment: for example `tensor_x[::,: 4, 3: 0: -1] = u`.
* True as indexing: For a tensor shape with (3,4,5), operation `tensor_x[True]` will result in a tensor with shape (1, 3, 4, 5). - The tuple element is Number:
- Value: for example `tensor_x[2,1]`.
- Assignment: for example `tensor_x[1,4] = u`.
- The tuple element is a mixture of slice and ellipsis:
- Value: for example `tensor_x[..., ::, 1:]`.
- Assignment: for example `tensor_x[..., ::, 1:] = u`.
- Not supported in other situations
In addition, tuple also supports slice value operation, `tuple_x [start: stop: step]`, which has the same effect as Python, and will not be repeated here.
### Unsupported Syntax ### Unsupported Syntax
Currently, the following syntax is not supported in network constructors: Currently, the following syntax is not supported in network constructors:
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
- [系统函数](#系统函数) - [系统函数](#系统函数)
- [函数参数](#函数参数) - [函数参数](#函数参数)
- [操作符](#操作符) - [操作符](#操作符)
- [切片操作](#切片操作) - [索引操作](#索引操作)
- [不支持的语法](#不支持的语法) - [不支持的语法](#不支持的语法)
- [网络定义约束](#网络定义约束) - [网络定义约束](#网络定义约束)
- [整网实例类型](#整网实例类型) - [整网实例类型](#整网实例类型)
...@@ -86,32 +86,39 @@ ...@@ -86,32 +86,39 @@
| `%` |标量、`Tensor` | `%` |标量、`Tensor`
| `[]` |操作对象类型支持`list``tuple``Tensor`,支持多重下标访问作为右值,但不支持多重下标访问作为左值,且索引类型不支持Tensor;Tuple、Tensor类型访问限制见切片操作中的说明。 | `[]` |操作对象类型支持`list``tuple``Tensor`,支持多重下标访问作为右值,但不支持多重下标访问作为左值,且索引类型不支持Tensor;Tuple、Tensor类型访问限制见切片操作中的说明。
### 切片操作 ### 索引操作
* `tuple`切片操作:`tuple_x[start:stop:step]` 索引操作包含`tuple``Tensor`的索引操作。下面重点介绍一下`Tensor`的索引取值和赋值操作,取值以`tensor_x[index]`为例,赋值以`tensor_x[index] = u`为例进行详细说明。其中tensor_x是一个`Tensor`,对其进行切片操作;index表示索引,u表示赋予的值,可以是`scalar`或者`Tensor(size=1)`。索引类型如下:
- `tuple_x`为一个元组,是被执行切片操作的目标。
- `start`:切片的起始位置索引,类型为`int`,取值范围为`[-length(tuple_x), length(tuple_x) - 1]`。可缺省,缺省配置如下: - 切片索引:index为`slice`
-`step > 0`时,缺省值为`0` - 取值:`tensor_x[start:stop:step]`,其中Slice(start:stop:step)与Python的语法相同,这里不再赘述。
-`step < 0`时,缺省值为`length(tuple_x) - 1` - 赋值:`tensor_x[start:stop:step]=u`
- `end`:切片的结束位置索引,类型为`int`,取值范围为`[-length(tuple_x) - 1, length(tuple_x)]`。可缺省,缺省配置如下: - Ellipsis索引:index为`ellipsis`
-`step > 0`时,缺省值为`length(tuple_x)` - 取值:`tensor_x[...]`
-`step < 0`是,缺省值为`-1` - 赋值:`tensor_x[...]=u`
- `step`:切片的步长,类型为`int`,取值范围为`step != 0`。可缺省,缺省值为`1` - 布尔常量索引:index为`True`,index为`False`暂不支持。
- 取值:`tensor_x[True]`
* `Tensor`切片操作:`tensor_x[start0:stop0:step0, start1:stop1:step1, start2:stop2:step2]` - 赋值:暂不支持。
- `tensor_x`是一个维度不低于3维的`Tensor`,对其进行切片操作。 - Tensor索引:index为`Tensor`
- `start0`:在第0维上进行切片的起始位置索引,类型为`int`,可缺省,缺省配置如下: - 取值:暂不支持。
-`step > 0`时,缺省值为`0` - 赋值:`tensor_x[index]=u``index`仅支持`bool`类型的`Tensor`
-`step < 0`时,缺省值为`-1` - None常量索引:index为`None`
- `end0`:在第0维上进行切片的结束位置索引,类型为`int`,可缺省,缺省配置如下: - 取值:`tensor_x[None]`,结果与numpy保持一致。
-`step > 0`时,缺省值为`length(tuple_x)` - 赋值:暂不支持。
-`step < 0`是,缺省值为`-(1 + length(tuple_x))` - tuple索引:index为`tuple`
- `step0`:在第0维上进行切片的步长,类型为`int`,取值范围为`step != 0`。可缺省,缺省值为`1` - tuple元素为slice:
- 如果进行切片的维数少于`Tensor`的维数,则未指定切片的维度默认取全部元素。 - 取值:例如`tensor_x[::, :4, 3:0:-1]`
- 切片降维操作:在某维度上传入整数索引,则取出该维度上对应索引的元素,且消除该维度,如shape为(4, 3, 6)的`tensor_x[2:4:1, 1, 0:5:2]`切片之后,生成一个shape为(2, 3)的`Tensor`,原`Tensor`的第1维被消除。 - 赋值:例如`tensor_x[::, :4, 3:0:-1]=u`
- Ellipsis作为索引:与numpy保持一致,未明确指定如何操作的维度,都对应取全部元素,shape为(3, 4, 5, 6)的`tensor_x[1:3:1, ..., 0:5:2]`切片之后,第1维和第2维取全部元素,生成一个shape为(2, 4, 5, 3)的`Tensor` - tuple元素为Number:
- None作为索引:与numpy保持一致,如shape为(3, 4, 5)的`tensor_x[None]`返回的是一个维度扩展之后,shape为(1, 3, 4, 5)的`Tensor` - 取值:例如`tensor_x[2,1]`
- True作为索引:与numpy保持一致,如shape为(3, 4, 5)的`tensor_x[True]`返回的是一个维度扩展之后,shape为(1, 3, 4, 5)的`Tensor` - 赋值:例如`tensor_x[1,4]=u`
- tuple元素为slice和ellipsis混合情况:
- 取值:例如`tensor_x[..., ::, 1:]`
- 赋值:例如`tensor_x[..., ::, 1:]=u`
- 其他情况暂不支持
另外tuple也支持切片取值操作,`tuple_x[start:stop:step]`,与Python的效果相同,这里不再赘述。
### 不支持的语法 ### 不支持的语法
目前在网络构造函数里面暂不支持以下语法: 目前在网络构造函数里面暂不支持以下语法:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册