Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
5a159f34
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看板
未验证
提交
5a159f34
编写于
3月 14, 2018
作者:
C
chengduo
提交者:
GitHub
3月 14, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8934 from chengduoZH/feature/Enhance_regularizer_py
Enhance regularizer.py
上级
ba65d54d
93107ce1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
6 deletion
+38
-6
python/paddle/fluid/regularizer.py
python/paddle/fluid/regularizer.py
+34
-5
python/paddle/fluid/tests/book/test_machine_translation.py
python/paddle/fluid/tests/book/test_machine_translation.py
+4
-1
未找到文件。
python/paddle/fluid/regularizer.py
浏览文件 @
5a159f34
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
# limitations under the License.
# limitations under the License.
import
framework
import
framework
from
.
import
core
__all__
=
[
__all__
=
[
'append_regularization_ops'
,
'append_regularization_ops'
,
...
@@ -46,9 +47,9 @@ def append_regularization_ops(parameters_and_grads, regularization=None):
...
@@ -46,9 +47,9 @@ def append_regularization_ops(parameters_and_grads, regularization=None):
regularization_term
=
None
regularization_term
=
None
if
param
.
regularizer
is
not
None
:
if
param
.
regularizer
is
not
None
:
# Add variable for regularization term in grad block
# Add variable for regularization term in grad block
regularization_term
=
param
.
regularizer
(
param
,
grad
.
block
)
regularization_term
=
param
.
regularizer
(
param
,
grad
,
grad
.
block
)
elif
regularization
is
not
None
:
elif
regularization
is
not
None
:
regularization_term
=
regularization
(
param
,
grad
.
block
)
regularization_term
=
regularization
(
param
,
grad
,
grad
.
block
)
# If no gradient or no regularization specified,
# If no gradient or no regularization specified,
# then we don't need to do anything
# then we don't need to do anything
...
@@ -82,7 +83,7 @@ class WeightDecayRegularizer(object):
...
@@ -82,7 +83,7 @@ class WeightDecayRegularizer(object):
def
__init__
(
self
):
def
__init__
(
self
):
pass
pass
def
__call__
(
self
,
param
,
block
):
def
__call__
(
self
,
param
,
grad
,
block
):
"""Add corresponding weight decay operations to the network
"""Add corresponding weight decay operations to the network
"""
"""
raise
NotImplementedError
()
raise
NotImplementedError
()
...
@@ -102,7 +103,7 @@ class L2DecayRegularizer(WeightDecayRegularizer):
...
@@ -102,7 +103,7 @@ class L2DecayRegularizer(WeightDecayRegularizer):
super
(
L2DecayRegularizer
,
self
).
__init__
()
super
(
L2DecayRegularizer
,
self
).
__init__
()
self
.
_regularization_coeff
=
regularization_coeff
self
.
_regularization_coeff
=
regularization_coeff
def
__call__
(
self
,
param
,
block
):
def
__call__
(
self
,
param
,
grad
,
block
):
"""Add L2 weight decay ops to network
"""Add L2 weight decay ops to network
Adds L2 weight decay ops.
Adds L2 weight decay ops.
...
@@ -117,8 +118,23 @@ class L2DecayRegularizer(WeightDecayRegularizer):
...
@@ -117,8 +118,23 @@ class L2DecayRegularizer(WeightDecayRegularizer):
"""
"""
assert
isinstance
(
param
,
framework
.
Parameter
)
assert
isinstance
(
param
,
framework
.
Parameter
)
assert
isinstance
(
block
,
framework
.
Block
)
assert
isinstance
(
block
,
framework
.
Block
)
decay
=
block
.
create_var
(
decay
=
block
.
create_var
(
dtype
=
"float32"
,
shape
=
param
.
shape
,
lod_level
=
param
.
lod_level
)
dtype
=
"float32"
,
shape
=
param
.
shape
,
lod_level
=
param
.
lod_level
)
if
grad
.
type
==
core
.
VarDesc
.
VarType
.
SELECTED_ROWS
:
decay
=
block
.
create_var
(
dtype
=
"float32"
,
shape
=
param
.
shape
,
type
=
core
.
VarDesc
.
VarType
.
SELECTED_ROWS
)
block
.
append_op
(
type
=
'lookup_table'
,
inputs
=
{
'W'
:
param
,
'Ids'
:
grad
},
outputs
=
{
'Out'
:
decay
},
attrs
=
{
'is_sparse'
:
True
})
param
=
decay
# Append Op to calculate decay
# Append Op to calculate decay
block
.
append_op
(
block
.
append_op
(
type
=
'scale'
,
type
=
'scale'
,
...
@@ -141,7 +157,7 @@ class L1DecayRegularizer(WeightDecayRegularizer):
...
@@ -141,7 +157,7 @@ class L1DecayRegularizer(WeightDecayRegularizer):
super
(
L1DecayRegularizer
,
self
).
__init__
()
super
(
L1DecayRegularizer
,
self
).
__init__
()
self
.
_regularization_coeff
=
regularization_coeff
self
.
_regularization_coeff
=
regularization_coeff
def
__call__
(
self
,
param
,
block
):
def
__call__
(
self
,
param
,
grad
,
block
):
"""Add L1 weight decay ops to network
"""Add L1 weight decay ops to network
Adds L1 weight decay ops.
Adds L1 weight decay ops.
...
@@ -158,6 +174,19 @@ class L1DecayRegularizer(WeightDecayRegularizer):
...
@@ -158,6 +174,19 @@ class L1DecayRegularizer(WeightDecayRegularizer):
assert
isinstance
(
block
,
framework
.
Block
)
assert
isinstance
(
block
,
framework
.
Block
)
decay
=
block
.
create_var
(
decay
=
block
.
create_var
(
dtype
=
"float32"
,
shape
=
param
.
shape
,
lod_level
=
param
.
lod_level
)
dtype
=
"float32"
,
shape
=
param
.
shape
,
lod_level
=
param
.
lod_level
)
if
grad
.
type
==
core
.
VarDesc
.
VarType
.
SELECTED_ROWS
:
decay
=
block
.
create_var
(
dtype
=
"float32"
,
shape
=
param
.
shape
,
type
=
core
.
VarDesc
.
VarType
.
SELECTED_ROWS
)
block
.
append_op
(
type
=
'lookup_table'
,
inputs
=
{
'W'
:
param
,
'Ids'
:
grad
},
outputs
=
{
'Out'
:
decay
},
attrs
=
{
'is_sparse'
:
True
})
# Append sign op
# Append sign op
block
.
append_op
(
block
.
append_op
(
type
=
'sign'
,
inputs
=
{
"X"
:
param
},
outputs
=
{
"Out"
:
decay
})
type
=
'sign'
,
inputs
=
{
"X"
:
param
},
outputs
=
{
"Out"
:
decay
})
...
...
python/paddle/fluid/tests/book/test_machine_translation.py
浏览文件 @
5a159f34
...
@@ -181,7 +181,10 @@ def train_main(use_cuda, is_sparse, is_local=True):
...
@@ -181,7 +181,10 @@ def train_main(use_cuda, is_sparse, is_local=True):
cost
=
pd
.
cross_entropy
(
input
=
rnn_out
,
label
=
label
)
cost
=
pd
.
cross_entropy
(
input
=
rnn_out
,
label
=
label
)
avg_cost
=
pd
.
mean
(
cost
)
avg_cost
=
pd
.
mean
(
cost
)
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
1e-4
)
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
1e-4
,
regularization
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.1
))
optimize_ops
,
params_grads
=
optimizer
.
minimize
(
avg_cost
)
optimize_ops
,
params_grads
=
optimizer
.
minimize
(
avg_cost
)
train_data
=
paddle
.
batch
(
train_data
=
paddle
.
batch
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录