Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
2edb69a7
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看板
提交
2edb69a7
编写于
8月 25, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
with in-place option
上级
eb8b84b5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
15 addition
and
9 deletion
+15
-9
python/paddle/v2/framework/tests/gradient_checker.py
python/paddle/v2/framework/tests/gradient_checker.py
+13
-8
python/paddle/v2/framework/tests/test_scatter_op.py
python/paddle/v2/framework/tests/test_scatter_op.py
+2
-1
未找到文件。
python/paddle/v2/framework/tests/gradient_checker.py
浏览文件 @
2edb69a7
...
@@ -32,7 +32,8 @@ def get_numeric_gradient(op,
...
@@ -32,7 +32,8 @@ def get_numeric_gradient(op,
output_name
,
output_name
,
input_to_check
,
input_to_check
,
delta
=
0.005
,
delta
=
0.005
,
local_scope
=
None
):
local_scope
=
None
,
in_place
=
False
):
"""
"""
Get Numeric Gradient for an operator's input.
Get Numeric Gradient for an operator's input.
...
@@ -90,9 +91,10 @@ def get_numeric_gradient(op,
...
@@ -90,9 +91,10 @@ def get_numeric_gradient(op,
# we only compute gradient of one element each time.
# we only compute gradient of one element each time.
# we use a for loop to compute the gradient of every element.
# we use a for loop to compute the gradient of every element.
for
i
in
xrange
(
tensor_size
):
for
i
in
xrange
(
tensor_size
):
for
var_name
in
input_values
:
if
in_place
:
tensor_
=
local_scope
.
find_var
(
var_name
).
get_tensor
()
for
var_name
in
input_values
:
tensor_
.
set
(
numpy
.
copy
(
input_values
[
var_name
]),
core
.
CPUPlace
())
tensor_
=
local_scope
.
find_var
(
var_name
).
get_tensor
()
tensor_
.
set
(
numpy
.
copy
(
input_values
[
var_name
]),
core
.
CPUPlace
())
# get one input element throw it's index i.
# get one input element throw it's index i.
origin
=
tensor_to_check
.
get_float_element
(
i
)
origin
=
tensor_to_check
.
get_float_element
(
i
)
...
@@ -102,9 +104,10 @@ def get_numeric_gradient(op,
...
@@ -102,9 +104,10 @@ def get_numeric_gradient(op,
y_pos
=
get_output
()
y_pos
=
get_output
()
# plus delta to this element, run op and get the sum of the result tensor.
# plus delta to this element, run op and get the sum of the result tensor.
for
var_name
in
input_values
:
if
in_place
:
tensor_
=
local_scope
.
find_var
(
var_name
).
get_tensor
()
for
var_name
in
input_values
:
tensor_
.
set
(
numpy
.
copy
(
input_values
[
var_name
]),
core
.
CPUPlace
())
tensor_
=
local_scope
.
find_var
(
var_name
).
get_tensor
()
tensor_
.
set
(
numpy
.
copy
(
input_values
[
var_name
]),
core
.
CPUPlace
())
x_neg
=
origin
-
delta
x_neg
=
origin
-
delta
tensor_to_check
.
set_float_element
(
i
,
x_neg
)
tensor_to_check
.
set_float_element
(
i
,
x_neg
)
y_neg
=
get_output
()
y_neg
=
get_output
()
...
@@ -257,6 +260,7 @@ class GradientChecker(unittest.TestCase):
...
@@ -257,6 +260,7 @@ class GradientChecker(unittest.TestCase):
output_name
,
output_name
,
no_grad_set
=
None
,
no_grad_set
=
None
,
only_cpu
=
False
,
only_cpu
=
False
,
in_place
=
False
,
max_relative_error
=
0.005
):
max_relative_error
=
0.005
):
"""
"""
:param forward_op: used to create backward_op
:param forward_op: used to create backward_op
...
@@ -289,7 +293,8 @@ class GradientChecker(unittest.TestCase):
...
@@ -289,7 +293,8 @@ class GradientChecker(unittest.TestCase):
# get numerical gradients
# get numerical gradients
numeric_grads
=
[
numeric_grads
=
[
get_numeric_gradient
(
forward_op
,
input_vars
,
output_name
,
name
)
get_numeric_gradient
(
forward_op
,
input_vars
,
output_name
,
name
,
in_place
=
in_place
)
for
name
in
inputs_to_check
for
name
in
inputs_to_check
]
]
...
...
python/paddle/v2/framework/tests/test_scatter_op.py
浏览文件 @
2edb69a7
...
@@ -31,7 +31,8 @@ class TestScatterGradOp(GradientChecker):
...
@@ -31,7 +31,8 @@ class TestScatterGradOp(GradientChecker):
output_np
[
index_np
]
+=
updates_np
output_np
[
index_np
]
+=
updates_np
inputs
=
{
'Ref'
:
ref_np
,
'Index'
:
index_np
,
'Updates'
:
updates_np
}
inputs
=
{
'Ref'
:
ref_np
,
'Index'
:
index_np
,
'Updates'
:
updates_np
}
# check gradient
# check gradient
self
.
check_grad
(
op
,
inputs
,
set
([
"Updates"
,
"Ref"
]),
"Out"
)
self
.
check_grad
(
op
,
inputs
,
set
([
"Updates"
,
"Ref"
]),
"Out"
,
in_place
=
True
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录