Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
81773d0b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
提交
81773d0b
编写于
3月 08, 2019
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add imperative and declarative mode testbase and example
test=develop
上级
e233b91a
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
91 addition
and
2 deletion
+91
-2
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+91
-2
未找到文件。
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
81773d0b
...
...
@@ -15,13 +15,102 @@
from
__future__
import
print_function
import
unittest
import
paddle.fluid.layers
as
layers
import
contextlib
import
numpy
as
np
import
decorators
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.layers.device
import
get_places
import
paddle.fluid.nets
as
nets
from
paddle.fluid.framework
import
Program
,
program_guard
,
default_main_program
from
paddle.fluid.param_attr
import
ParamAttr
import
decorators
from
paddle.fluid
import
core
from
paddle.fluid.initializer
import
Constant
import
paddle.fluid.layers
as
layers
from
test_imperative_base
import
new_program_scope
from
paddle.fluid.imperative
import
nn
from
paddle.fluid.imperative
import
base
class
LayerTest
(
unittest
.
TestCase
):
@
classmethod
def
setUpClass
(
cls
):
cls
.
seed
=
111
@
classmethod
def
tearDownClass
(
cls
):
pass
def
_get_place
(
self
):
if
core
.
is_compiled_with_cuda
():
return
core
.
CUDAPlace
(
0
)
return
core
.
CPUPlace
()
@
contextlib
.
contextmanager
def
static_graph
(
self
):
with
new_program_scope
():
fluid
.
default_startup_program
().
random_seed
=
self
.
seed
fluid
.
default_main_program
().
random_seed
=
self
.
seed
yield
def
get_static_graph_result
(
self
,
feed
,
fetch_list
):
exe
=
fluid
.
Executor
(
self
.
_get_place
())
exe
.
run
(
fluid
.
default_startup_program
())
return
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feed
,
fetch_list
=
fetch_list
)
@
contextlib
.
contextmanager
def
dynamic_graph
(
self
):
with
fluid
.
imperative
.
guard
(
self
.
_get_place
()):
fluid
.
default_startup_program
().
random_seed
=
self
.
seed
fluid
.
default_main_program
().
random_seed
=
self
.
seed
yield
class
TestLayer
(
LayerTest
):
def
test_relu
(
self
):
with
self
.
static_graph
():
t
=
layers
.
data
(
name
=
't'
,
shape
=
[
3
,
3
],
dtype
=
'float32'
)
ret
=
layers
.
relu
(
t
)
static_ret
=
self
.
get_static_graph_result
(
feed
=
{
't'
:
np
.
ones
(
[
3
,
3
],
dtype
=
'float32'
)},
fetch_list
=
[
ret
])[
0
]
with
self
.
dynamic_graph
():
t
=
np
.
ones
([
3
,
3
],
dtype
=
'float32'
)
dy_ret
=
layers
.
relu
(
base
.
to_variable
(
t
))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret
.
_numpy
()))
def
test_conv2d
(
self
):
with
self
.
static_graph
():
images
=
layers
.
data
(
name
=
'pixel'
,
shape
=
[
3
,
5
,
5
],
dtype
=
'float32'
)
ret
=
layers
.
conv2d
(
input
=
images
,
num_filters
=
3
,
filter_size
=
[
2
,
2
])
static_ret
=
self
.
get_static_graph_result
(
feed
=
{
'pixel'
:
np
.
ones
(
[
2
,
3
,
5
,
5
],
dtype
=
'float32'
)},
fetch_list
=
[
ret
])[
0
]
with
self
.
static_graph
():
images
=
layers
.
data
(
name
=
'pixel'
,
shape
=
[
3
,
5
,
5
],
dtype
=
'float32'
)
conv2d
=
nn
.
Conv2D
(
'conv2d'
,
num_channels
=
3
,
num_filters
=
3
,
filter_size
=
[
2
,
2
])
ret
=
conv2d
(
images
)
static_ret2
=
self
.
get_static_graph_result
(
feed
=
{
'pixel'
:
np
.
ones
(
[
2
,
3
,
5
,
5
],
dtype
=
'float32'
)},
fetch_list
=
[
ret
])[
0
]
with
self
.
dynamic_graph
():
images
=
np
.
ones
([
2
,
3
,
5
,
5
],
dtype
=
'float32'
)
conv2d
=
nn
.
Conv2D
(
'conv2d'
,
num_channels
=
3
,
num_filters
=
3
,
filter_size
=
[
2
,
2
])
dy_ret
=
conv2d
(
base
.
to_variable
(
images
))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret
.
_numpy
()))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
static_ret2
))
class
TestBook
(
unittest
.
TestCase
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录