Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
1f5f79cb
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看板
提交
1f5f79cb
编写于
1月 10, 2018
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix error_clip errors and add unit test
上级
5f985000
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
70 addition
and
7 deletion
+70
-7
python/paddle/v2/fluid/clip.py
python/paddle/v2/fluid/clip.py
+8
-7
python/paddle/v2/fluid/framework.py
python/paddle/v2/fluid/framework.py
+3
-0
python/paddle/v2/fluid/tests/test_clip.py
python/paddle/v2/fluid/tests/test_clip.py
+59
-0
未找到文件。
python/paddle/v2/fluid/clip.py
浏览文件 @
1f5f79cb
...
...
@@ -3,7 +3,8 @@ import layers
from
.
import
core
__all__
=
[
'GradientClipByValue'
,
'append_gradient_clip_ops'
,
'error_clip_callback'
'GradientClipByValue'
,
'ErrorClipByValue'
,
'append_gradient_clip_ops'
,
'error_clip_callback'
]
...
...
@@ -23,12 +24,12 @@ class ErrorClipByValue(BaseErrorClipAttr):
self
.
min
=
min
def
append_clip_op
(
self
,
block
,
grad_name
):
block
.
append_op
(
type
=
"clip"
,
inputs
=
{
"X"
:
grad_name
},
outputs
=
{
"Out"
:
grad_name
},
attrs
=
{
"min"
:
self
.
min
,
"max"
:
self
.
max
}
)
clip_op_desc
=
block
.
desc
.
append_op
()
clip_op_desc
.
set_type
(
"clip"
)
clip_op_desc
.
set_input
(
"X"
,
[
grad_name
])
clip_op_desc
.
set_output
(
"Out"
,
[
grad_name
])
clip_op_desc
.
set_attr
(
"min"
,
self
.
min
)
clip_op_desc
.
set_attr
(
"max"
,
self
.
max
)
def
error_clip_callback
(
block
,
context
):
...
...
python/paddle/v2/fluid/framework.py
浏览文件 @
1f5f79cb
...
...
@@ -271,6 +271,9 @@ class Variable(object):
uid
=
core
.
unique_integer
(
prefix
)
# unique during whole process.
return
"_"
.
join
([
prefix
,
str
(
uid
)])
def
set_error_clip
(
self
,
error_clip
):
self
.
error_clip
=
error_clip
def
get_all_op_protos
():
"""
...
...
python/paddle/v2/fluid/tests/test_clip.py
0 → 100644
浏览文件 @
1f5f79cb
from
__future__
import
print_function
import
numpy
as
np
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
BATCH_SIZE
=
128
CLIP_MAX
=
2e-6
CLIP_MIN
=
-
1e-6
prog
=
fluid
.
framework
.
Program
()
with
fluid
.
program_guard
(
main_program
=
prog
):
image
=
fluid
.
layers
.
data
(
name
=
'x'
,
shape
=
[
784
],
dtype
=
'float32'
)
hidden1
=
fluid
.
layers
.
fc
(
input
=
image
,
size
=
128
,
act
=
'relu'
)
hidden2
=
fluid
.
layers
.
fc
(
input
=
hidden1
,
size
=
64
,
act
=
'relu'
)
predict
=
fluid
.
layers
.
fc
(
input
=
hidden2
,
size
=
10
,
act
=
'softmax'
)
label
=
fluid
.
layers
.
data
(
name
=
'y'
,
shape
=
[
1
],
dtype
=
'int64'
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
prog_clip
=
prog
.
clone
()
prog_clip
.
block
(
0
).
var
(
hidden1
.
name
).
set_error_clip
(
fluid
.
clip
.
ErrorClipByValue
(
max
=
CLIP_MAX
,
min
=
CLIP_MIN
))
avg_cost_clip
=
prog_clip
.
block
(
0
).
var
(
avg_cost
.
name
)
fluid
.
backward
.
append_backward
(
loss
=
avg_cost
)
fluid
.
backward
.
append_backward
(
loss
=
avg_cost_clip
,
callback
=
fluid
.
clip
.
error_clip_callback
)
hidden1_grad
=
prog
.
block
(
0
).
var
(
hidden1
.
name
+
"@GRAD"
)
hidden1_grad_clip
=
prog_clip
.
block
(
0
).
var
(
hidden1
.
name
+
"@GRAD"
)
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
8192
),
batch_size
=
BATCH_SIZE
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
image
,
label
],
place
=
place
)
exe
.
run
(
fluid
.
default_startup_program
())
count
=
0
for
data
in
train_reader
():
count
+=
1
if
count
>
5
:
break
out
=
exe
.
run
(
prog
,
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
hidden1_grad
])
out_clip
=
exe
.
run
(
prog_clip
,
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
hidden1_grad_clip
])
if
not
(
out
[
0
].
clip
(
min
=
CLIP_MIN
,
max
=
CLIP_MAX
)
==
out_clip
[
0
]).
all
():
exit
(
1
)
exit
(
0
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录