Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
151cfff9
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
151cfff9
编写于
3月 17, 2018
作者:
K
Kexin Zhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add more tests
上级
0a95a44b
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
80 addition
and
14 deletion
+80
-14
python/paddle/fluid/tests/unittests/test_batch_norm_op.py
python/paddle/fluid/tests/unittests/test_batch_norm_op.py
+80
-14
未找到文件。
python/paddle/fluid/tests/unittests/test_batch_norm_op.py
浏览文件 @
151cfff9
...
@@ -188,14 +188,27 @@ def set_output_grad(scope, outputs, place, feed_dict=None):
...
@@ -188,14 +188,27 @@ def set_output_grad(scope, outputs, place, feed_dict=None):
class
TestBatchNormOpInference
(
OpTest
):
class
TestBatchNormOpInference
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"conv2d"
self
.
is_test
=
True
self
.
dtype
=
np
.
float32
self
.
dtype
=
np
.
float32
self
.
data_layout
=
"NCHW"
init_dtype
()
init_data_layout
()
init_test_case
()
def
test_python
(
self
):
data_format
=
"NHWC"
epsilon
=
0.00001
epsilon
=
0.00001
shape
=
self
.
shape
n
,
h
,
w
,
c
=
2
,
3
,
4
,
5
if
len
(
shape
)
==
2
:
x_shape
=
shape
c
=
x_shape
[
1
]
else
:
n
,
h
,
w
,
c
=
shape
[
0
],
shape
[
1
],
shape
[
2
],
shape
[
3
]
if
self
.
data_layout
==
"NHWC"
:
x_shape
=
[
n
,
h
,
w
,
c
]
x_shape
=
[
n
,
h
,
w
,
c
]
elif
self
.
data_layout
==
"NCHW"
:
x_shape
=
[
n
,
c
,
h
,
w
]
else
:
raise
ValueError
(
"Unknown data layout."
)
scale_shape
=
[
c
]
scale_shape
=
[
c
]
x_val
=
np
.
random
.
random_sample
(
x_shape
).
astype
(
self
.
dtype
)
x_val
=
np
.
random
.
random_sample
(
x_shape
).
astype
(
self
.
dtype
)
...
@@ -205,7 +218,64 @@ class TestBatchNormOpInference(OpTest):
...
@@ -205,7 +218,64 @@ class TestBatchNormOpInference(OpTest):
mean
=
np
.
zeros
(
scale_shape
).
astype
(
self
.
dtype
)
mean
=
np
.
zeros
(
scale_shape
).
astype
(
self
.
dtype
)
variance
=
np
.
ones
(
scale_shape
).
astype
(
self
.
dtype
)
variance
=
np
.
ones
(
scale_shape
).
astype
(
self
.
dtype
)
# run forward
saved_mean
=
np
.
zeros
(
scale_shape
).
astype
(
self
.
dtype
)
saved_variance
=
np
.
ones
(
scale_shape
).
astype
(
self
.
dtype
)
y_out
=
_reference_testing
(
x_val
,
scale_val
,
bias_val
,
mean
,
variance
,
epsilon
,
self
.
data_layout
).
astype
(
self
.
dtype
)
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
x_val
),
'Scale'
:
OpTest
.
np_dtype_to_fluid_dtype
(
scale_val
),
'Bias'
:
OpTest
.
np_dtype_to_fluid_dtype
(
bias_val
),
'Mean'
:
OpTest
.
np_dtype_to_fluid_dtype
(
mean
),
'Variance'
:
OpTest
.
np_dtype_to_fluid_dtype
(
variance
)
}
self
.
attrs
=
{
'is_test'
:
self
.
is_test
,
'epsilon'
:
epsilon
,
'data_layout'
:
self
.
data_layout
}
self
.
outputs
=
{
'Y'
:
y_out
,
'MeanOut'
:
mean
,
'VarianceOut'
:
variance
,
'SavedMean'
:
saved_mean
,
'SavedVariance'
:
saved_variance
}
def
test_check_output
(
self
):
self
.
check_output
()
def
init_dtype
(
self
):
pass
def
init_data_layout
(
self
):
pass
def
init_test_case
(
self
):
self
.
shape
=
[
2
,
3
,
4
,
5
]
class
TestBatchNormOpTraining
(
OpTest
):
def
__assert_close
(
self
,
tensor
,
np_array
,
msg
,
atol
=
1e-4
):
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
tensor
),
np_array
,
atol
=
atol
),
msg
)
def
test_python_testing
(
self
):
data_format
=
"NHWC"
epsilon
=
0.00001
n
,
h
,
w
,
c
=
2
,
3
,
4
,
5
x_shape
=
[
n
,
h
,
w
,
c
]
scale_shape
=
[
c
]
x_val
=
np
.
random
.
random_sample
(
x_shape
).
astype
(
np
.
float32
)
scale_val
=
np
.
random
.
random_sample
(
scale_shape
).
astype
(
np
.
float32
)
bias_val
=
np
.
random
.
random_sample
(
scale_shape
).
astype
(
np
.
float32
)
mean
=
np
.
zeros
(
scale_shape
).
astype
(
np
.
float32
)
variance
=
np
.
ones
(
scale_shape
).
astype
(
np
.
float32
)
y_out
=
_reference_testing
(
x_val
,
scale_val
,
bias_val
,
mean
,
variance
,
y_out
=
_reference_testing
(
x_val
,
scale_val
,
bias_val
,
mean
,
variance
,
epsilon
,
"NHWC"
)
epsilon
,
"NHWC"
)
...
@@ -218,15 +288,11 @@ class TestBatchNormOpInference(OpTest):
...
@@ -218,15 +288,11 @@ class TestBatchNormOpInference(OpTest):
# transfer (N, C, H, W) back to (N, H, W, C)
# transfer (N, C, H, W) back to (N, H, W, C)
y_out2_trans
=
np
.
transpose
(
y_out2
,
(
0
,
2
,
3
,
1
))
y_out2_trans
=
np
.
transpose
(
y_out2
,
(
0
,
2
,
3
,
1
))
self
.
__assert_close
(
y_out
,
y_out2_trans
,
"inference output"
)
self
.
__assert_close
(
y_out
,
y_out2_trans
,
"inference outputs of two formats have differences"
)
print
'python: NHWC, NCHW, inference checking passed'
print
'python: NHWC, NCHW, inference checking passed'
def
test_python_training
(
self
):
class
TestBatchNormOpTraining
(
OpTest
):
def
__assert_close
(
self
,
tensor
,
np_array
,
msg
,
atol
=
1e-4
):
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
tensor
),
np_array
,
atol
=
atol
),
msg
)
def
test_python
(
self
):
data_format
=
"NHWC"
data_format
=
"NHWC"
epsilon
=
0.00001
epsilon
=
0.00001
momentum
=
0.9
momentum
=
0.9
...
@@ -264,7 +330,7 @@ class TestBatchNormOpTraining(OpTest):
...
@@ -264,7 +330,7 @@ class TestBatchNormOpTraining(OpTest):
# transfer (N, C, H, W) back to (N, H, W, C)
# transfer (N, C, H, W) back to (N, H, W, C)
y_out2_trans
=
np
.
transpose
(
y_out2
,
(
0
,
2
,
3
,
1
))
y_out2_trans
=
np
.
transpose
(
y_out2
,
(
0
,
2
,
3
,
1
))
self
.
__assert_close
(
y_out
,
y_out2_trans
,
"batch
variance
"
)
self
.
__assert_close
(
y_out
,
y_out2_trans
,
"batch
output
"
)
print
'python: NHWC, NCHW, forward checking passed'
print
'python: NHWC, NCHW, forward checking passed'
# test backward now
# test backward now
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录