Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f189ad74
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
f189ad74
编写于
1月 17, 2018
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine the defination of class GradientClipByGlobalNorm
上级
adc26dff
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
27 addition
and
20 deletion
+27
-20
python/paddle/v2/fluid/clip.py
python/paddle/v2/fluid/clip.py
+27
-20
未找到文件。
python/paddle/v2/fluid/clip.py
浏览文件 @
f189ad74
...
...
@@ -93,41 +93,48 @@ class GradientClipByNorm(BaseGradientClipAttr):
class
GradientClipByGlobalNorm
(
BaseGradientClipAttr
):
global_norm_var
=
None
clip_norm_var
=
None
ratio
_var
=
None
scale
_var
=
None
@
classmethod
def
init
(
cls
,
clip_norm
):
if
not
(
isinstance
(
clip_norm
,
int
)
or
isinstance
(
clip_norm
,
float
)):
raise
TypeError
(
"The 'clip_norm' must be a value of int or float"
)
cls
.
global_norm_var
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
"float32"
,
value
=
0.0
)
cls
.
clip_norm_var
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
"float32"
,
value
=
clip_norm
)
def
__init__
(
self
):
if
not
(
isinstance
(
self
.
__class__
.
global_norm_var
,
Variable
)
and
isinstance
(
self
.
__class__
.
clip_norm_var
,
Variable
)):
@
classmethod
def
check_init
(
cls
):
if
not
(
isinstance
(
cls
.
global_norm_var
,
Variable
)
and
isinstance
(
cls
.
clip_norm_var
,
Variable
)):
raise
ValueError
(
"Class 'GradientClipByGlobalNorm' has not been properly initialized. Please call GradientClipByGlobalNorm.init() first."
)
"Class 'GradientClipByGlobalNorm' has not been properly initialized.
\
Please call GradientClipByGlobalNorm.init() first."
)
@
classmethod
def
process_context
(
cls
,
context
,
param
,
grad
):
cls
.
check_init
()
def
process_context
(
self
,
context
,
param
,
grad
):
local_norm_var
=
layers
.
reduce_sum
(
x
=
layers
.
pow
(
x
=
grad
,
factor
=
2
),
reduce_all
=
True
)
layers
.
sums
(
input
=
[
local_norm_var
,
self
.
__class__
.
global_norm_var
],
out
=
[
self
.
__class__
.
global_norm_var
])
input
=
[
local_norm_var
,
cls
.
global_norm_var
],
out
=
[
cls
.
global_norm_var
])
def
create_operators
(
self
,
param
,
grad
):
if
self
.
__class__
.
ratio_var
is
None
:
self
.
__class__
.
global_norm_var
=
layers
.
sqrt
(
x
=
self
.
__class__
.
global_norm_var
)
self
.
__class__
.
ratio_var
=
layers
.
elementwise_div
(
x
=
self
.
__class__
.
clip_norm_var
,
@
classmethod
def
create_operators
(
cls
,
param
,
grad
):
cls
.
check_init
()
if
cls
.
scale_var
is
None
:
cls
.
global_norm_var
=
layers
.
sqrt
(
x
=
cls
.
global_norm_var
)
cls
.
scale_var
=
layers
.
elementwise_div
(
x
=
cls
.
clip_norm_var
,
y
=
layers
.
elementwise_max
(
x
=
self
.
__class__
.
clip_norm_var
,
y
=
self
.
__class__
.
global_norm_var
))
# 缺乏elementwise_max
# 没法将ratio_var送给scale_op。
# new_grad = layers.
x
=
cls
.
clip_norm_var
,
y
=
cls
.
global_norm_var
))
new_grad
=
layers
.
elementwise_mul
(
x
=
grad
,
y
=
cls
.
scale_var
)
return
param
,
new_grad
def
append_gradient_clip_ops
(
param_grad
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录