Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
d6d33fd7
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
d6d33fd7
编写于
6月 04, 2019
作者:
Y
Yibing Liu
提交者:
GitHub
6月 04, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add update method for ema (#17812)
上级
c10157a5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
28 addition
and
15 deletion
+28
-15
paddle/fluid/API.spec
paddle/fluid/API.spec
+1
-0
python/paddle/fluid/optimizer.py
python/paddle/fluid/optimizer.py
+27
-15
未找到文件。
paddle/fluid/API.spec
浏览文件 @
d6d33fd7
...
...
@@ -528,6 +528,7 @@ paddle.fluid.optimizer.LambOptimizer.minimize (ArgSpec(args=['self', 'loss', 'st
paddle.fluid.optimizer.ExponentialMovingAverage.__init__ (ArgSpec(args=['self', 'decay', 'thres_steps', 'name'], varargs=None, keywords=None, defaults=(0.999, None, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.optimizer.ExponentialMovingAverage.apply (ArgSpec(args=['self', 'executor', 'need_restore'], varargs=None, keywords=None, defaults=(True,)), ('document', '30f494752ac8921dc5835a63637f453a'))
paddle.fluid.optimizer.ExponentialMovingAverage.restore (ArgSpec(args=['self', 'executor'], varargs=None, keywords=None, defaults=None), ('document', '8c8a1791608b02a1ede53d6dd3a4fcec'))
paddle.fluid.optimizer.ExponentialMovingAverage.update (ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None), ('document', 'ea10f08af6d7aac3b7974aa976e4085f'))
paddle.fluid.backward.append_backward (ArgSpec(args=['loss', 'parameter_list', 'no_grad_set', 'callbacks'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', '08a5dd9f6f376ff3d55e0b1d92115cbd'))
paddle.fluid.regularizer.L1DecayRegularizer.__init__ (ArgSpec(args=['self', 'regularization_coeff'], varargs=None, keywords=None, defaults=(0.0,)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.regularizer.L2DecayRegularizer.__init__ (ArgSpec(args=['self', 'regularization_coeff'], varargs=None, keywords=None, defaults=(0.0,)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
...
...
python/paddle/fluid/optimizer.py
浏览文件 @
d6d33fd7
...
...
@@ -2333,10 +2333,10 @@ class ExponentialMovingAverage(object):
\\
text{EMA}_t & =
\\
text{decay} *
\\
text{EMA}_{t-1} + (1 -
\\
text{decay}) *
\\
theta_t
The average results
will be saved in temporary variables which are created
and maintained by the object, and can be applied to parameters of current
model by calling **apply()** method. And the **restore()** method is used to
restore the parameters.
The average results
calculated by **update()** method will be saved in
temporary variables which are created and maintained by the object, and can
be applied to parameters of current model by calling **apply()** method. And
the **restore()** method is used to
restore the parameters.
**Bias correction**. All EMAs are initialized to :math:`0` and hence they will be
zero biased, which can be corrected by divided by a factor
...
...
@@ -2382,6 +2382,7 @@ class ExponentialMovingAverage(object):
global_steps = fluid.layers.learning_rate_scheduler._decay_step_counter()
ema = fluid.optimizer.ExponentialMovingAverage(0.999, thres_steps=global_steps)
ema.update()
# pseudo code
for pass_id in range(args.pass_num):
...
...
@@ -2407,7 +2408,7 @@ class ExponentialMovingAverage(object):
self
.
_name
=
name
if
name
is
not
None
else
''
self
.
_decay_var
=
self
.
_get_ema_decay
()
self
.
params_tmps
=
[]
self
.
_
params_tmps
=
[]
for
param
in
default_main_program
().
global_block
().
all_parameters
():
if
param
.
do_model_average
!=
False
:
tmp
=
param
.
block
.
create_var
(
...
...
@@ -2416,22 +2417,22 @@ class ExponentialMovingAverage(object):
dtype
=
param
.
dtype
,
persistable
=
False
,
stop_gradient
=
True
)
self
.
params_tmps
.
append
((
param
,
tmp
))
self
.
_
params_tmps
.
append
((
param
,
tmp
))
ema_vars
=
{}
for
param
,
tmp
in
self
.
params_tmps
:
self
.
_
ema_vars
=
{}
for
param
,
tmp
in
self
.
_
params_tmps
:
with
param
.
block
.
program
.
_optimized_guard
(
[
param
,
tmp
]),
name_scope
(
'moving_average'
):
ema_vars
[
param
.
name
]
=
self
.
_append_ema_op
s
(
param
)
self
.
_ema_vars
[
param
.
name
]
=
self
.
_create_ema_var
s
(
param
)
self
.
apply_program
=
Program
()
block
=
self
.
apply_program
.
global_block
()
with
program_guard
(
main_program
=
self
.
apply_program
):
decay_pow
=
self
.
_get_decay_pow
(
block
)
for
param
,
tmp
in
self
.
params_tmps
:
for
param
,
tmp
in
self
.
_
params_tmps
:
param
=
block
.
_clone_variable
(
param
)
tmp
=
block
.
_clone_variable
(
tmp
)
ema
=
block
.
_clone_variable
(
ema_vars
[
param
.
name
])
ema
=
block
.
_clone_variable
(
self
.
_
ema_vars
[
param
.
name
])
layers
.
assign
(
input
=
param
,
output
=
tmp
)
# bias correction
ema
=
ema
/
(
1.0
-
decay_pow
)
...
...
@@ -2440,7 +2441,7 @@ class ExponentialMovingAverage(object):
self
.
restore_program
=
Program
()
block
=
self
.
restore_program
.
global_block
()
with
program_guard
(
main_program
=
self
.
restore_program
):
for
param
,
tmp
in
self
.
params_tmps
:
for
param
,
tmp
in
self
.
_
params_tmps
:
tmp
=
block
.
_clone_variable
(
tmp
)
param
=
block
.
_clone_variable
(
param
)
layers
.
assign
(
input
=
tmp
,
output
=
param
)
...
...
@@ -2472,7 +2473,7 @@ class ExponentialMovingAverage(object):
decay_pow_acc
=
layers
.
elementwise_pow
(
decay_var
,
global_steps
+
1
)
return
decay_pow_acc
def
_
append_ema_op
s
(
self
,
param
):
def
_
create_ema_var
s
(
self
,
param
):
param_ema
=
layers
.
create_global_var
(
name
=
unique_name
.
generate
(
self
.
_name
+
param
.
name
+
'_ema'
),
shape
=
param
.
shape
,
...
...
@@ -2480,10 +2481,21 @@ class ExponentialMovingAverage(object):
dtype
=
param
.
dtype
,
persistable
=
True
)
ema_t
=
param_ema
*
self
.
_decay_var
+
param
*
(
1
-
self
.
_decay_var
)
layers
.
assign
(
input
=
ema_t
,
output
=
param_ema
)
return
param_ema
def
update
(
self
):
"""
Update Exponential Moving Average. Should only call this method in
train program.
"""
for
param
,
tmp
in
self
.
_params_tmps
:
with
param
.
block
.
program
.
_optimized_guard
(
[
param
,
tmp
]),
name_scope
(
'moving_average'
):
param_ema
=
self
.
_ema_vars
[
param
.
name
]
ema_t
=
param_ema
*
self
.
_decay_var
+
param
*
(
1
-
self
.
_decay_var
)
layers
.
assign
(
input
=
ema_t
,
output
=
param_ema
)
@
signature_safe_contextmanager
def
apply
(
self
,
executor
,
need_restore
=
True
):
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录