Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
c957445c
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c957445c
编写于
8月 09, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
A better error message for gradient checker
* Give which parameter, which element are wrong. And what max_diff is.
上级
be9867f9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
20 deletion
+30
-20
paddle/framework/pybind.cc
paddle/framework/pybind.cc
+7
-2
python/paddle/v2/framework/tests/gradient_checker.py
python/paddle/v2/framework/tests/gradient_checker.py
+23
-18
未找到文件。
paddle/framework/pybind.cc
浏览文件 @
c957445c
...
...
@@ -22,6 +22,7 @@ limitations under the License. */
#include "paddle/operators/net_op.h"
#include "paddle/platform/enforce.h"
#include "paddle/platform/place.h"
#include "paddle/string/to_string.h"
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
...
...
@@ -205,9 +206,13 @@ All parameter, weight, gradient are variables in Paddle.
});
// clang-format on
py
::
class_
<
paddle
::
platform
::
GPUPlace
>
(
m
,
"GPUPlace"
).
def
(
py
::
init
<
int
>
());
py
::
class_
<
platform
::
GPUPlace
>
(
m
,
"GPUPlace"
)
.
def
(
py
::
init
<
int
>
())
.
def
(
"__str__"
,
string
::
to_string
<
const
platform
::
GPUPlace
&>
);
py
::
class_
<
paddle
::
platform
::
CPUPlace
>
(
m
,
"CPUPlace"
).
def
(
py
::
init
<>
());
py
::
class_
<
paddle
::
platform
::
CPUPlace
>
(
m
,
"CPUPlace"
)
.
def
(
py
::
init
<>
())
.
def
(
"__str__"
,
string
::
to_string
<
const
platform
::
CPUPlace
&>
);
py
::
class_
<
OperatorBase
,
std
::
shared_ptr
<
OperatorBase
>>
operator_base
(
m
,
"Operator"
);
...
...
python/paddle/v2/framework/tests/gradient_checker.py
浏览文件 @
c957445c
...
...
@@ -92,15 +92,26 @@ def get_numeric_gradient(op,
class
GradientChecker
(
unittest
.
TestCase
):
def
__is_close
(
self
,
numeric_grads
,
scope
,
max_relative_error
):
def
__is_close
(
self
,
numeric_grads
,
scope
,
max_relative_error
,
msg_prefix
):
for
name
in
numeric_grads
:
op_grad
=
numpy
.
array
(
scope
.
find_var
(
grad_var_name
(
name
)).
get_tensor
())
is_close
=
numpy
.
allclose
(
numeric_grads
[
name
],
op_grad
,
rtol
=
max_relative_error
,
atol
=
100
)
if
not
is_close
:
return
False
return
True
b
=
numpy
.
array
(
scope
.
find_var
(
grad_var_name
(
name
)).
get_tensor
())
a
=
numeric_grads
[
name
]
abs_a
=
numpy
.
abs
(
a
)
# if abs_a is nearly zero, then use abs error for a, not relative
# error.
abs_a
[
abs_a
<
1e-3
]
=
1
diff_mat
=
numpy
.
abs
(
a
-
b
)
/
abs_a
max_diff
=
numpy
.
max
(
diff_mat
)
def
err_msg
():
offset
=
numpy
.
argmax
(
diff_mat
>
max_relative_error
)
return
"%s Variable %s max gradient diff %f over limit %f, the first "
\
"error element is %d"
%
(
msg_prefix
,
name
,
max_diff
,
max_relative_error
,
offset
)
self
.
assertLessEqual
(
max_diff
,
max_relative_error
,
err_msg
())
def
check_grad
(
self
,
forward_op
,
...
...
@@ -145,7 +156,8 @@ class GradientChecker(unittest.TestCase):
# get numeric gradient
for
check_name
in
inputs_to_check
:
numeric_grad
[
check_name
]
=
\
get_numeric_gradient
(
forward_op
,
input_vars
,
output_name
,
check_name
)
get_numeric_gradient
(
forward_op
,
input_vars
,
output_name
,
check_name
)
# get operator gradient according to different device
for
place
in
places
:
...
...
@@ -187,15 +199,8 @@ class GradientChecker(unittest.TestCase):
backward_op
.
infer_shape
(
scope
)
backward_op
.
run
(
scope
,
ctx
)
if
isinstance
(
place
,
core
.
CPUPlace
):
msg
=
"CPU kernel gradient is not close to numeric gradient"
else
:
if
isinstance
(
place
,
core
.
GPUPlace
):
msg
=
"GPU kernel gradient is not close to numeric gradient"
else
:
raise
ValueError
(
"unknown place "
+
type
(
place
))
self
.
assertTrue
(
self
.
__is_close
(
numeric_grad
,
scope
,
max_relative_error
),
msg
)
self
.
__is_close
(
numeric_grad
,
scope
,
max_relative_error
,
"Gradient Check On %s"
%
str
(
place
))
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录