Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
60aaa715
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
60aaa715
编写于
2月 25, 2020
作者:
S
songyouwei
提交者:
GitHub
2月 25, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dygraph support while_loop op (#22714)
* dygraph support while_loop op test=develop * refine assign test=develop
上级
f97f3f93
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
50 addition
and
1 deletion
+50
-1
python/paddle/fluid/layers/control_flow.py
python/paddle/fluid/layers/control_flow.py
+15
-1
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+35
-0
未找到文件。
python/paddle/fluid/layers/control_flow.py
浏览文件 @
60aaa715
...
...
@@ -18,7 +18,7 @@ from ..wrapped_decorator import signature_safe_contextmanager
from
.layer_function_generator
import
autodoc
,
templatedoc
from
.tensor
import
assign
,
cast
,
fill_constant
from
..
import
core
from
..framework
import
Program
,
Variable
,
Operator
from
..framework
import
Program
,
Variable
,
Operator
,
in_dygraph_mode
from
..layer_helper
import
LayerHelper
,
unique_name
from
.nn
import
logical_and
,
logical_not
,
logical_or
from
.utils
import
assert_same_structure
,
map_structure
...
...
@@ -999,6 +999,20 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
"the shape of the variable returned by cond should be [],"
"but given shape as {0}."
.
format
(
list
(
pre_cond
.
shape
)))
if
in_dygraph_mode
():
now_cond
=
pre_cond
.
numpy
()[
0
]
while
(
now_cond
):
output_vars
=
body
(
*
loop_vars
)
if
not
isinstance
(
output_vars
,
(
list
,
tuple
)):
output_vars
=
[
output_vars
]
if
len
(
output_vars
)
!=
len
(
loop_vars
):
raise
ValueError
(
"body in while_loop should return the same arity "
"(length and structure) and types as loop_vars"
)
now_cond
=
cond
(
*
output_vars
).
numpy
()[
0
]
loop_vars
=
output_vars
return
loop_vars
while_loop_block
=
While
(
pre_cond
,
is_test
,
name
)
with
while_loop_block
.
block
():
output_vars
=
body
(
*
loop_vars
)
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
60aaa715
...
...
@@ -1393,6 +1393,41 @@ class TestLayer(LayerTest):
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret_rlt
))
def
test_while_loop
(
self
):
with
self
.
static_graph
():
i
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
0
)
ten
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
10
)
def
cond
(
i
):
return
layers
.
less_than
(
i
,
ten
)
def
body
(
i
):
return
i
+
1
out
=
layers
.
while_loop
(
cond
,
body
,
[
i
])
static_ret
=
self
.
get_static_graph_result
(
feed
=
{},
fetch_list
=
out
)
with
self
.
dynamic_graph
():
i
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
0
)
ten
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
10
)
def
cond
(
i
):
return
layers
.
less_than
(
i
,
ten
)
def
body
(
i
):
return
i
+
1
dy_ret
=
layers
.
while_loop
(
cond
,
body
,
[
i
])
with
self
.
assertRaises
(
ValueError
):
j
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int64'
,
value
=
0
)
def
body2
(
i
):
return
i
+
1
,
i
+
2
layers
.
while_loop
(
cond
,
body2
,
[
j
])
self
.
assertTrue
(
np
.
array_equal
(
static_ret
[
0
],
dy_ret
[
0
].
numpy
()))
def
test_compare
(
self
):
value_a
=
np
.
arange
(
3
)
value_b
=
np
.
arange
(
3
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录