Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0c617377
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
0c617377
编写于
3月 03, 2020
作者:
S
songyouwei
提交者:
GitHub
3月 03, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add case and switch_case unittests for dygraph mode (#22790)
test=develop
上级
ddb9b46f
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
104 addition
and
0 deletion
+104
-0
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+104
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
0c617377
...
@@ -1564,6 +1564,110 @@ class TestLayer(LayerTest):
...
@@ -1564,6 +1564,110 @@ class TestLayer(LayerTest):
self
.
assertTrue
(
np
.
array_equal
(
static_res
,
dynamic_res
))
self
.
assertTrue
(
np
.
array_equal
(
static_res
,
dynamic_res
))
def
test_case
(
self
):
def
fn_1
():
return
layers
.
fill_constant
(
shape
=
[
1
,
2
],
dtype
=
'float32'
,
value
=
1
)
def
fn_2
():
return
layers
.
fill_constant
(
shape
=
[
2
,
2
],
dtype
=
'int32'
,
value
=
2
)
def
fn_3
():
return
layers
.
fill_constant
(
shape
=
[
3
],
dtype
=
'int32'
,
value
=
3
)
with
self
.
static_graph
():
x
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.3
)
y
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.1
)
z
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.2
)
pred_1
=
layers
.
less_than
(
z
,
x
)
# true: 0.2 < 0.3
pred_2
=
layers
.
less_than
(
x
,
y
)
# false: 0.3 < 0.1
pred_3
=
layers
.
equal
(
x
,
y
)
# false: 0.3 == 0.1
out_1
=
layers
.
case
(
pred_fn_pairs
=
[(
pred_1
,
fn_1
),
(
pred_2
,
fn_2
)],
default
=
fn_3
)
out_2
=
layers
.
case
(
pred_fn_pairs
=
[(
pred_2
,
fn_2
),
(
pred_3
,
fn_3
)])
place
=
fluid
.
CUDAPlace
(
0
)
if
core
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
static_res1
,
static_res2
=
exe
.
run
(
fetch_list
=
[
out_1
,
out_2
])
with
self
.
dynamic_graph
():
x
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.3
)
y
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.1
)
z
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.2
)
pred_1
=
layers
.
less_than
(
z
,
x
)
# true: 0.2 < 0.3
pred_2
=
layers
.
less_than
(
x
,
y
)
# false: 0.3 < 0.1
pred_3
=
layers
.
equal
(
x
,
y
)
# false: 0.3 == 0.1
out_1
=
layers
.
case
(
pred_fn_pairs
=
[(
pred_1
,
fn_1
),
(
pred_2
,
fn_2
)],
default
=
fn_3
)
out_2
=
layers
.
case
(
pred_fn_pairs
=
[(
pred_2
,
fn_2
),
(
pred_3
,
fn_3
)])
dynamic_res1
=
out_1
.
numpy
()
dynamic_res2
=
out_2
.
numpy
()
self
.
assertTrue
(
np
.
array_equal
(
static_res1
,
dynamic_res1
))
self
.
assertTrue
(
np
.
array_equal
(
static_res2
,
dynamic_res2
))
def
test_switch_case
(
self
):
def
fn_1
():
return
layers
.
fill_constant
(
shape
=
[
1
,
2
],
dtype
=
'float32'
,
value
=
1
)
def
fn_2
():
return
layers
.
fill_constant
(
shape
=
[
2
,
2
],
dtype
=
'int32'
,
value
=
2
)
def
fn_3
():
return
layers
.
fill_constant
(
shape
=
[
3
],
dtype
=
'int32'
,
value
=
3
)
with
self
.
static_graph
():
index_1
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int32'
,
value
=
1
)
index_2
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int32'
,
value
=
2
)
out_1
=
layers
.
switch_case
(
branch_index
=
index_1
,
branch_fns
=
{
1
:
fn_1
,
2
:
fn_2
},
default
=
fn_3
)
out_2
=
layers
.
switch_case
(
branch_index
=
index_2
,
branch_fns
=
[(
1
,
fn_1
),
(
2
,
fn_2
)],
default
=
fn_3
)
out_3
=
layers
.
switch_case
(
branch_index
=
index_2
,
branch_fns
=
[(
0
,
fn_1
),
(
4
,
fn_2
),
(
7
,
fn_3
)])
place
=
fluid
.
CUDAPlace
(
0
)
if
core
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
static_res1
,
static_res2
,
static_res3
=
exe
.
run
(
fetch_list
=
[
out_1
,
out_2
,
out_3
])
with
self
.
dynamic_graph
():
index_1
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int32'
,
value
=
1
)
index_2
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int32'
,
value
=
2
)
out_1
=
layers
.
switch_case
(
branch_index
=
index_1
,
branch_fns
=
{
1
:
fn_1
,
2
:
fn_2
},
default
=
fn_3
)
out_2
=
layers
.
switch_case
(
branch_index
=
index_2
,
branch_fns
=
[(
1
,
fn_1
),
(
2
,
fn_2
)],
default
=
fn_3
)
out_3
=
layers
.
switch_case
(
branch_index
=
index_2
,
branch_fns
=
[(
0
,
fn_1
),
(
4
,
fn_2
),
(
7
,
fn_3
)])
dynamic_res1
=
out_1
.
numpy
()
dynamic_res2
=
out_2
.
numpy
()
dynamic_res3
=
out_3
.
numpy
()
self
.
assertTrue
(
np
.
array_equal
(
static_res1
,
dynamic_res1
))
self
.
assertTrue
(
np
.
array_equal
(
static_res2
,
dynamic_res2
))
self
.
assertTrue
(
np
.
array_equal
(
static_res3
,
dynamic_res3
))
def
test_crop_tensor
(
self
):
def
test_crop_tensor
(
self
):
with
self
.
static_graph
():
with
self
.
static_graph
():
x
=
fluid
.
layers
.
data
(
name
=
"x1"
,
shape
=
[
6
,
5
,
8
])
x
=
fluid
.
layers
.
data
(
name
=
"x1"
,
shape
=
[
6
,
5
,
8
])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录