Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
橘蓝
Paddle
提交
03789a7d
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
03789a7d
编写于
10月 27, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
batch norm fully tortured and passed
上级
8a07aff4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
29 addition
and
17 deletion
+29
-17
paddle/operators/batch_norm_op.cu
paddle/operators/batch_norm_op.cu
+9
-2
python/paddle/v2/framework/tests/test_batch_norm_op.py
python/paddle/v2/framework/tests/test_batch_norm_op.py
+20
-15
未找到文件。
paddle/operators/batch_norm_op.cu
浏览文件 @
03789a7d
...
...
@@ -208,8 +208,15 @@ class BatchNormGradKernel<platform::GPUPlace, T>
mode_
=
CUDNN_BATCHNORM_SPATIAL
;
#endif
std
::
vector
<
int
>
dims
=
{
N
,
C
,
H
,
W
,
D
};
std
::
vector
<
int
>
strides
=
{
H
*
W
*
C
*
D
,
1
,
W
*
D
*
C
,
D
*
C
,
C
};
std
::
vector
<
int
>
dims
;
std
::
vector
<
int
>
strides
;
if
(
tensor_format
==
TensorFormat
::
NCHW
)
{
dims
=
{
N
,
C
,
H
,
W
,
D
};
strides
=
{
C
*
H
*
W
*
D
,
H
*
W
*
D
,
W
*
D
,
D
,
1
};
}
else
{
dims
=
{
N
,
C
,
H
,
W
,
D
};
strides
=
{
H
*
W
*
C
*
D
,
1
,
W
*
D
*
C
,
D
*
C
,
C
};
}
CUDNN_ENFORCE
(
platform
::
dynload
::
cudnnSetTensorNdDescriptor
(
data_desc_
,
CudnnDataType
<
T
>::
type
,
x_dims
.
size
()
>
3
?
x_dims
.
size
()
:
4
,
dims
.
data
(),
strides
.
data
()));
...
...
python/paddle/v2/framework/tests/test_batch_norm_op.py
浏览文件 @
03789a7d
...
...
@@ -96,22 +96,25 @@ def create_or_get_tensor(scope, var_name, var, place):
return
tensor
def
set_output_grad
(
scope
,
outputs
,
place
):
def
__set_tensor__
(
name
):
def
set_output_grad
(
scope
,
outputs
,
place
,
feed_dict
=
None
):
def
__set_tensor__
(
name
,
data
=
None
):
out_tensor
=
scope
.
find_var
(
name
).
get_tensor
()
grad_tensor
=
scope
.
var
(
grad_var_name
(
name
)).
get_tensor
()
out_dtype
=
out_tensor
.
dtype
()
if
out_dtype
==
core
.
DataType
.
FP64
:
data
=
np
.
ones
(
out_tensor
.
shape
(),
dtype
=
np
.
float64
)
elif
out_dtype
==
core
.
DataType
.
FP32
:
data
=
np
.
ones
(
out_tensor
.
shape
(),
dtype
=
np
.
float32
)
else
:
raise
ValueError
(
"Not supported data type "
+
str
(
out_dtype
))
if
data
is
None
:
if
out_dtype
==
core
.
DataType
.
FP64
:
data
=
np
.
ones
(
out_tensor
.
shape
(),
dtype
=
np
.
float64
)
elif
out_dtype
==
core
.
DataType
.
FP32
:
data
=
np
.
ones
(
out_tensor
.
shape
(),
dtype
=
np
.
float32
)
else
:
raise
ValueError
(
"Not supported data type "
+
str
(
out_dtype
))
grad_tensor
.
set
(
data
,
place
)
for
output
in
outputs
:
__set_tensor__
(
output
)
data
=
None
if
output
in
feed_dict
:
data
=
feed_dict
[
output
]
__set_tensor__
(
output
,
data
)
class
TestBatchNormOp
(
OpTest
):
...
...
@@ -119,7 +122,7 @@ class TestBatchNormOp(OpTest):
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
tensor
),
np_array
,
atol
=
atol
),
msg
)
def
test_python
(
self
):
data_format
=
"N
CHW
"
data_format
=
"N
HWC
"
epsilon
=
0.00001
momentum
=
0.9
...
...
@@ -214,7 +217,10 @@ class TestBatchNormOp(OpTest):
saved_variance
=
1.
/
np
.
sqrt
(
var_ref
+
epsilon
)
# for gradient test
y_grad
=
np
.
ones
(
x_shape
).
astype
(
np
.
float32
)
# y_grad = np.ones(x_shape).astype(np.float32)
y_grad
=
np
.
zeros
(
x_shape
).
astype
(
np
.
float32
)
y_grad
[
0
,
0
,
0
,
0
]
=
1.
# y_grad = np.random.random_sample(x_shape).astype(np.float32)
x_grad_ref
,
scale_grad_ref
,
bias_grad_ref
=
_reference_grad
(
x_val
,
y_grad
,
scale_val
,
saved_mean
,
var_ref
,
epsilon
,
data_format
)
...
...
@@ -283,7 +289,8 @@ class TestBatchNormOp(OpTest):
set_output_grad
(
scope
,
[
"y_out"
,
"mean"
,
"variance"
,
"saved_mean"
,
"saved_variance"
],
place
)
place
,
feed_dict
=
{
"y_out"
:
y_grad
})
batch_norm_op_grad
.
run
(
scope
,
ctx
)
x_grad_tensor
=
create_or_get_tensor
(
scope
,
...
...
@@ -297,8 +304,6 @@ class TestBatchNormOp(OpTest):
None
,
place
)
# check gradient output
print
'var x_grad tensor: '
,
str
(
place
),
np
.
array
(
x_grad_tensor
)
print
'var x_grad by python: '
,
str
(
place
),
x_grad_ref
self
.
__assert_close
(
x_grad_tensor
,
x_grad_ref
,
"x_grad"
)
self
.
__assert_close
(
scale_grad_tensor
,
scale_grad_ref
,
"scale_grad"
)
self
.
__assert_close
(
bias_grad_tensor
,
bias_grad_ref
,
"bias_grad"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录