Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
97c432ee
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
97c432ee
编写于
9月 05, 2017
作者:
T
Travis CI
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Deploy to GitHub Pages:
b64aac54
上级
f3278e8d
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
73 addition
and
30 deletion
+73
-30
develop/doc_cn/_sources/howto/dev/new_op_cn.md.txt
develop/doc_cn/_sources/howto/dev/new_op_cn.md.txt
+35
-13
develop/doc_cn/howto/dev/new_op_cn.html
develop/doc_cn/howto/dev/new_op_cn.html
+37
-16
develop/doc_cn/searchindex.js
develop/doc_cn/searchindex.js
+1
-1
未找到文件。
develop/doc_cn/_sources/howto/dev/new_op_cn.md.txt
浏览文件 @
97c432ee
...
...
@@ -286,28 +286,50 @@ class TestMulOp(unittest.TestCase):
反向Op单测继承自`GradientChecker`,而`GradientChecker`集成自`unittest.TestCase`,所以反向单测函数需要`test_`开头。
```
class MulGradOpTest
(GradientChecker):
def
test_mul
(self):
op = create_op("mul")
inputs = {
```
class TestMulGradOp
(GradientChecker):
def
setUp
(self):
self.
op = create_op("mul")
self.
inputs = {
'X': np.random.random((32, 84)).astype("float32"),
'Y': np.random.random((84, 100)).astype("float32")
}
self.compare_grad(op, inputs)
def test_cpu_gpu_compare(self):
self.compare_grad(self.op, self.inputs)
def test_normal(self):
# mul op will enlarge the relative error
self.check_grad(
op, inputs, set(["X", "Y"]), "Out", max_relative_error=0.5)
```
self.op, self.inputs, ["X", "Y"], "Out", max_relative_error=0.5)
def test_ignore_x(self):
self.check_grad(
self.op,
self.inputs, ["Y"],
"Out",
max_relative_error=0.5,
no_grad_set={"X"})
def test_ignore_y(self):
self.check_grad(
self.op,
self.inputs, ["X"],
"Out",
max_relative_error=0.5,
no_grad_set={"Y"})
```
下面解释一些关键的地方:
- 调用`create_op("mul")`创建反向Op对应的前向Op。
- 定义输入`inputs`。
- 调用`compare_grad`函数对比CPU、GPU计算结果。
- 调用`check_grad`检查梯度稳定性,这里采用数值法检测梯度正确性。
- 第一个参数`
op` : 前向o
p。
- 第二个参数`inputs` : 输入词典,词典的Key和`ProtoMaker`定义保持一致。
- 第三个参数`
set(["X", "Y"])
` : 指定对输入变量`X`、`Y`做梯度检测。
-
`test_normal`中
调用`check_grad`检查梯度稳定性,这里采用数值法检测梯度正确性。
- 第一个参数`
self.op` : 前向O
p。
- 第二个参数`
self.
inputs` : 输入词典,词典的Key和`ProtoMaker`定义保持一致。
- 第三个参数`
["X", "Y"]
` : 指定对输入变量`X`、`Y`做梯度检测。
- 第四个参数`"Out"` : 指定前向网络最终的输出目标变量`Out`
- `test_ignore_x`和`test_ignore_y`分支测试只需要计算一个输入梯度的情况。
### 编译和执行
...
...
develop/doc_cn/howto/dev/new_op_cn.html
浏览文件 @
97c432ee
...
...
@@ -439,30 +439,51 @@ Kernel实现 | CPU、GPU共享Kernel在<code class="docutils literal"><spa
<div
class=
"section"
id=
"operator"
>
<span
id=
"id7"
></span><h3>
反向Operator单测
<a
class=
"headerlink"
href=
"#operator"
title=
"永久链接至标题"
>
¶
</a></h3>
<p>
反向Op单测继承自
<code
class=
"docutils literal"
><span
class=
"pre"
>
GradientChecker
</span></code>
,而
<code
class=
"docutils literal"
><span
class=
"pre"
>
GradientChecker
</span></code>
集成自
<code
class=
"docutils literal"
><span
class=
"pre"
>
unittest.TestCase
</span></code>
,所以反向单测函数需要
<code
class=
"docutils literal"
><span
class=
"pre"
>
test_
</span></code>
开头。
</p>
<div
class=
"highlight-default"
><div
class=
"highlight"
><pre><span></span><span
class=
"k"
>
class
</span>
<span
class=
"nc"
>
MulGradOpTest
</span><span
class=
"p"
>
(
</span><span
class=
"n"
>
GradientChecker
</span><span
class=
"p"
>
):
</span>
<span
class=
"k"
>
def
</span>
<span
class=
"nf"
>
test_mul
</span><span
class=
"p"
>
(
</span><span
class=
"bp"
>
self
</span><span
class=
"p"
>
):
</span>
<span
class=
"n"
>
op
</span>
<span
class=
"o"
>
=
</span>
<span
class=
"n"
>
create_op
</span><span
class=
"p"
>
(
</span><span
class=
"s2"
>
"
mul
"
</span><span
class=
"p"
>
)
</span>
<span
class=
"n"
>
inputs
</span>
<span
class=
"o"
>
=
</span>
<span
class=
"p"
>
{
</span>
<span
class=
"s1"
>
'
X
'
</span><span
class=
"p"
>
:
</span>
<span
class=
"n"
>
np
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"p"
>
((
</span><span
class=
"mi"
>
32
</span><span
class=
"p"
>
,
</span>
<span
class=
"mi"
>
84
</span><span
class=
"p"
>
))
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
astype
</span><span
class=
"p"
>
(
</span><span
class=
"s2"
>
"
float32
"
</span><span
class=
"p"
>
),
</span>
<span
class=
"s1"
>
'
Y
'
</span><span
class=
"p"
>
:
</span>
<span
class=
"n"
>
np
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"p"
>
((
</span><span
class=
"mi"
>
84
</span><span
class=
"p"
>
,
</span>
<span
class=
"mi"
>
100
</span><span
class=
"p"
>
))
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
astype
</span><span
class=
"p"
>
(
</span><span
class=
"s2"
>
"
float32
"
</span><span
class=
"p"
>
)
</span>
<span
class=
"p"
>
}
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
compare_grad
</span><span
class=
"p"
>
(
</span><span
class=
"n"
>
op
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
inputs
</span><span
class=
"p"
>
)
</span>
<span
class=
"c1"
>
# mul op will enlarge the relative error
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
check_grad
</span><span
class=
"p"
>
(
</span>
<span
class=
"n"
>
op
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
inputs
</span><span
class=
"p"
>
,
</span>
<span
class=
"nb"
>
set
</span><span
class=
"p"
>
([
</span><span
class=
"s2"
>
"
X
"
</span><span
class=
"p"
>
,
</span>
<span
class=
"s2"
>
"
Y
"
</span><span
class=
"p"
>
]),
</span>
<span
class=
"s2"
>
"
Out
"
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
max_relative_error
</span><span
class=
"o"
>
=
</span><span
class=
"mf"
>
0.5
</span><span
class=
"p"
>
)
</span>
<div
class=
"highlight-default"
><div
class=
"highlight"
><pre><span></span><span
class=
"k"
>
class
</span>
<span
class=
"nc"
>
TestMulGradOp
</span><span
class=
"p"
>
(
</span><span
class=
"n"
>
GradientChecker
</span><span
class=
"p"
>
):
</span>
<span
class=
"k"
>
def
</span>
<span
class=
"nf"
>
setUp
</span><span
class=
"p"
>
(
</span><span
class=
"bp"
>
self
</span><span
class=
"p"
>
):
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
op
</span>
<span
class=
"o"
>
=
</span>
<span
class=
"n"
>
create_op
</span><span
class=
"p"
>
(
</span><span
class=
"s2"
>
"
mul
"
</span><span
class=
"p"
>
)
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
inputs
</span>
<span
class=
"o"
>
=
</span>
<span
class=
"p"
>
{
</span>
<span
class=
"s1"
>
'
X
'
</span><span
class=
"p"
>
:
</span>
<span
class=
"n"
>
np
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"p"
>
((
</span><span
class=
"mi"
>
32
</span><span
class=
"p"
>
,
</span>
<span
class=
"mi"
>
84
</span><span
class=
"p"
>
))
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
astype
</span><span
class=
"p"
>
(
</span><span
class=
"s2"
>
"
float32
"
</span><span
class=
"p"
>
),
</span>
<span
class=
"s1"
>
'
Y
'
</span><span
class=
"p"
>
:
</span>
<span
class=
"n"
>
np
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
random
</span><span
class=
"p"
>
((
</span><span
class=
"mi"
>
84
</span><span
class=
"p"
>
,
</span>
<span
class=
"mi"
>
100
</span><span
class=
"p"
>
))
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
astype
</span><span
class=
"p"
>
(
</span><span
class=
"s2"
>
"
float32
"
</span><span
class=
"p"
>
)
</span>
<span
class=
"p"
>
}
</span>
<span
class=
"k"
>
def
</span>
<span
class=
"nf"
>
test_cpu_gpu_compare
</span><span
class=
"p"
>
(
</span><span
class=
"bp"
>
self
</span><span
class=
"p"
>
):
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
compare_grad
</span><span
class=
"p"
>
(
</span><span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
op
</span><span
class=
"p"
>
,
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
inputs
</span><span
class=
"p"
>
)
</span>
<span
class=
"k"
>
def
</span>
<span
class=
"nf"
>
test_normal
</span><span
class=
"p"
>
(
</span><span
class=
"bp"
>
self
</span><span
class=
"p"
>
):
</span>
<span
class=
"c1"
>
# mul op will enlarge the relative error
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
check_grad
</span><span
class=
"p"
>
(
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
op
</span><span
class=
"p"
>
,
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
inputs
</span><span
class=
"p"
>
,
</span>
<span
class=
"p"
>
[
</span><span
class=
"s2"
>
"
X
"
</span><span
class=
"p"
>
,
</span>
<span
class=
"s2"
>
"
Y
"
</span><span
class=
"p"
>
],
</span>
<span
class=
"s2"
>
"
Out
"
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
max_relative_error
</span><span
class=
"o"
>
=
</span><span
class=
"mf"
>
0.5
</span><span
class=
"p"
>
)
</span>
<span
class=
"k"
>
def
</span>
<span
class=
"nf"
>
test_ignore_x
</span><span
class=
"p"
>
(
</span><span
class=
"bp"
>
self
</span><span
class=
"p"
>
):
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
check_grad
</span><span
class=
"p"
>
(
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
op
</span><span
class=
"p"
>
,
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
inputs
</span><span
class=
"p"
>
,
</span>
<span
class=
"p"
>
[
</span><span
class=
"s2"
>
"
Y
"
</span><span
class=
"p"
>
],
</span>
<span
class=
"s2"
>
"
Out
"
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
max_relative_error
</span><span
class=
"o"
>
=
</span><span
class=
"mf"
>
0.5
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
no_grad_set
</span><span
class=
"o"
>
=
</span><span
class=
"p"
>
{
</span><span
class=
"s2"
>
"
X
"
</span><span
class=
"p"
>
})
</span>
<span
class=
"k"
>
def
</span>
<span
class=
"nf"
>
test_ignore_y
</span><span
class=
"p"
>
(
</span><span
class=
"bp"
>
self
</span><span
class=
"p"
>
):
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
check_grad
</span><span
class=
"p"
>
(
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
op
</span><span
class=
"p"
>
,
</span>
<span
class=
"bp"
>
self
</span><span
class=
"o"
>
.
</span><span
class=
"n"
>
inputs
</span><span
class=
"p"
>
,
</span>
<span
class=
"p"
>
[
</span><span
class=
"s2"
>
"
X
"
</span><span
class=
"p"
>
],
</span>
<span
class=
"s2"
>
"
Out
"
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
max_relative_error
</span><span
class=
"o"
>
=
</span><span
class=
"mf"
>
0.5
</span><span
class=
"p"
>
,
</span>
<span
class=
"n"
>
no_grad_set
</span><span
class=
"o"
>
=
</span><span
class=
"p"
>
{
</span><span
class=
"s2"
>
"
Y
"
</span><span
class=
"p"
>
})
</span>
</pre></div>
</div>
<p>
下面解释一些关键的地方:
</p>
<ul
class=
"simple"
>
<li>
调用
<code
class=
"docutils literal"
><span
class=
"pre"
>
create_op(
"
mul
"
)
</span></code>
创建反向Op对应的前向Op。
</li>
<li>
定义输入
<code
class=
"docutils literal"
><span
class=
"pre"
>
inputs
</span></code>
。
</li>
<li>
调用
<code
class=
"docutils literal"
><span
class=
"pre"
>
compare_grad
</span></code>
函数对比CPU、GPU计算结果。
</li>
<li>
调用
<code
class=
"docutils literal"
><span
class=
"pre"
>
check_grad
</span></code>
检查梯度稳定性,这里采用数值法检测梯度正确性。
<ul>
<li>
第一个参数
<code
class=
"docutils literal"
><span
class=
"pre"
>
op
</span></code>
: 前向o
p。
</li>
<li>
第二个参数
<code
class=
"docutils literal"
><span
class=
"pre"
>
inputs
</span></code>
: 输入词典,词典的Key和
<code
class=
"docutils literal"
><span
class=
"pre"
>
ProtoMaker
</span></code>
定义保持一致。
</li>
<li>
第三个参数
<code
class=
"docutils literal"
><span
class=
"pre"
>
set([
"
X
"
,
</span>
<span
class=
"pre"
>
"
Y
"
])
</span></code>
: 指定对输入变量
<code
class=
"docutils literal"
><span
class=
"pre"
>
X
</span></code>
、
<code
class=
"docutils literal"
><span
class=
"pre"
>
Y
</span></code>
做梯度检测。
</li>
<li>
<code
class=
"docutils literal"
><span
class=
"pre"
>
test_normal
</span></code>
中
调用
<code
class=
"docutils literal"
><span
class=
"pre"
>
check_grad
</span></code>
检查梯度稳定性,这里采用数值法检测梯度正确性。
<ul>
<li>
第一个参数
<code
class=
"docutils literal"
><span
class=
"pre"
>
self.op
</span></code>
: 前向O
p。
</li>
<li>
第二个参数
<code
class=
"docutils literal"
><span
class=
"pre"
>
self.
inputs
</span></code>
: 输入词典,词典的Key和
<code
class=
"docutils literal"
><span
class=
"pre"
>
ProtoMaker
</span></code>
定义保持一致。
</li>
<li>
第三个参数
<code
class=
"docutils literal"
><span
class=
"pre"
>
[
"
X
"
,
</span>
<span
class=
"pre"
>
"
Y
"
]
</span></code>
: 指定对输入变量
<code
class=
"docutils literal"
><span
class=
"pre"
>
X
</span></code>
、
<code
class=
"docutils literal"
><span
class=
"pre"
>
Y
</span></code>
做梯度检测。
</li>
<li>
第四个参数
<code
class=
"docutils literal"
><span
class=
"pre"
>
"
Out
"
</span></code>
: 指定前向网络最终的输出目标变量
<code
class=
"docutils literal"
><span
class=
"pre"
>
Out
</span></code></li>
</ul>
</li>
<li><code
class=
"docutils literal"
><span
class=
"pre"
>
test_ignore_x
</span></code>
和
<code
class=
"docutils literal"
><span
class=
"pre"
>
test_ignore_y
</span></code>
分支测试只需要计算一个输入梯度的情况。
</li>
</ul>
</div>
<div
class=
"section"
id=
""
>
...
...
develop/doc_cn/searchindex.js
浏览文件 @
97c432ee
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录