From 5016264e14442d8678f5bb72dca6a397db17fa63 Mon Sep 17 00:00:00 2001 From: chenfei52 Date: Mon, 22 Mar 2021 21:59:56 +0800 Subject: [PATCH] change doc of control flow --- .../static_graph_syntax_support.md | 159 ++++++++++++++---- 1 file changed, 130 insertions(+), 29 deletions(-) diff --git a/docs/note/source_zh_cn/static_graph_syntax_support.md b/docs/note/source_zh_cn/static_graph_syntax_support.md index 08738e3979..b7aa257e42 100644 --- a/docs/note/source_zh_cn/static_graph_syntax_support.md +++ b/docs/note/source_zh_cn/static_graph_syntax_support.md @@ -782,7 +782,7 @@ def generate_tensor(): ### 条件控制语句 -#### if +#### 单if 使用方式: @@ -800,6 +800,10 @@ def generate_tensor(): - 当即有`if`又有`else`时,`if`分支变量更新后数据类型和`shape`,与`else`分支更新后数据类型和`shape`必须一致。 +- 不支持高阶微分场景。 + +- 不支持elif语句。 + 示例1: ```python @@ -823,6 +827,49 @@ return out `if`分支更新后`out`和`else`分支更新后`out`,二者数据类型和`shape`必须一致。 +#### 并列if +使用方式: + +- `if (cond1):statements else:statements if(cond2):statements` + +参数:`cond1`、 `cond2`-- 与`单if`一致。 +限制: +- 继承`单if`所有限制。 +- 计算图if数量不超过50个。 + +示例: + +```python +if x > y: + out = x +else: + out = y +if z > x: + out = out + 1 +return out +``` + +#### 嵌套if +使用方式: + +- `if (cond1):if(cond2):statements...` + +参数:`cond1`、 `cond2`-- 与`单if`一致。 +限制: +- 继承`单if`所有限制。 +- 计算图if数量不超过50个。 + +示例: + +```python +if x > y: + z = z + 1 + if z > x: + return m +else: + return n +``` + ### 循环语句 #### for @@ -847,11 +894,38 @@ z: Tensor(shape=[2, 3], dtype=Int64, value=[[7, 7], [7, 7], [7, 7]]) 参数:`sequence` -- 遍历序列(`Tuple`、`List`) -#### while +#### if in for + +使用方式:`for i in sequence:if(cond)` + +参数:`cond` -- 与`单if`一致。 + +限制: +- 继承`单if`所有限制。 +- cond为变量时,不能有`if(cond):return`、`if(cond):continue`、`if(cond):break`语句。 + +示例如下: + +```python +z = Tensor(np.ones((2, 3))) +x = (1, 2, 3) +for i in x: + if i < 3: + z += i +return z +``` + +结果如下: + +```text +z: Tensor(shape=[2, 3], dtype=Int64, value=[[4, 4], [4, 4], [4, 4]]) +``` + +#### 单while 使用方式:`while(cond)` -参数:`cond` -- 与`if`一致。 +参数:`cond` -- 与`单if`一致。 限制: @@ -859,10 +933,12 @@ z: Tensor(shape=[2, 3], dtype=Int64, value=[[7, 7], [7, 7], [7, 7]]) - `while`内变量更新后数据类型和`shape`,与更新前数据类型和`shape`必须一致。 +- 不支持训练场景。 + 示例1: ```python -while x > y: +while x < y: x += 1 return m return n @@ -874,53 +950,78 @@ return n ```python out = m -while x > y: +while x < y: x += 1 - out = n + out = out + 1 return out ``` `while`内,`out`更新后和更新前的数据类型和`shape`必须一致。 -### 流程控制语句 +#### 并列while -当前流程控制语句支持了`break`、`continue`和`pass`。 +使用方式:`while(cond1):statements while(cond2)` -#### break +参数:`cond1`、 `cond2`-- 与`单if`一致。 -可用于`for`和`while`代码块里,用于终止整个循环。 +限制: +- 继承`单while`所有限制。 +- 并列while总数不超过50个。 -示例如下: +示例: ```python -for i in x: - if i == 2: - break - statement_a -statement_b +out = m +while x < y: + x += 1 + out = out + 1 +while out > 10: + out -= 10 +return out ``` -当 `i == 2`时,循环终止,执行`statement_b`。 +#### 嵌套while -#### continue +使用方式:`while(cond1):while(cond2):statements` -可用于`for`和`while`语句块里,用于终止本轮循环,直接进入下一轮循环。 +参数:`cond1`、 `cond2`-- 与`单if`一致。 -示例如下: +限制: +- 继承`单while`所有限制。 +- 嵌套while总数不超过50个。 + +示例: ```python -for i in x: - if i == 2: - continue - statement_a -statement_b - ``` +out = m +while x < y: + while z < y: + z += 1 + out = out + 1 + x += 1 +return out +``` + +#### if in while -当 `i == 2`时,本轮循环终止,不会往下执行`statement_a`,进入下一轮循环。 +使用方式:`while(cond1):if(cond2)` -#### pass +参数:`cond1`、 `cond2`-- 与`单if`一致。 -不做任何事情,占位语句。 +限制: +- 继承`单if`、`单while`所有限制。 +- cond为变量时,不能有`if(cond):return`、`if(cond):continue`、`if(cond):break`语句。 + +示例: + +```python +out = m +while x < y: + if z > 2*x: + out = out + 1 + x += 1 +return out +``` ### 函数定义语句 -- Gitee