Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
86ce1af7
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
86ce1af7
编写于
1月 20, 2020
作者:
littletomatodonkey
提交者:
ruri
1月 20, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix warmup bug in cosine decay (#4217)
上级
80cbdf27
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
26 addition
and
15 deletion
+26
-15
PaddleCV/image_classification/utils/optimizer.py
PaddleCV/image_classification/utils/optimizer.py
+26
-15
未找到文件。
PaddleCV/image_classification/utils/optimizer.py
浏览文件 @
86ce1af7
...
...
@@ -35,7 +35,10 @@ def cosine_decay(learning_rate, step_each_epoch, epochs=120):
return
decayed_lr
def
cosine_decay_with_warmup
(
learning_rate
,
step_each_epoch
,
epochs
=
120
):
def
cosine_decay_with_warmup
(
learning_rate
,
step_each_epoch
,
epochs
=
120
,
warm_up_epoch
=
5.0
):
"""Applies cosine decay to the learning rate.
lr = 0.05 * (math.cos(epoch * (math.pi / 120)) + 1)
decrease lr for every mini-batch and start with warmup.
...
...
@@ -49,7 +52,7 @@ def cosine_decay_with_warmup(learning_rate, step_each_epoch, epochs=120):
name
=
"learning_rate"
)
warmup_epoch
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
float
(
5
),
force_cpu
=
True
)
shape
=
[
1
],
dtype
=
'float32'
,
value
=
float
(
warm_up_epoch
),
force_cpu
=
True
)
epoch
=
ops
.
floor
(
global_step
/
step_each_epoch
)
with
fluid
.
layers
.
control_flow
.
Switch
()
as
switch
:
...
...
@@ -59,11 +62,16 @@ def cosine_decay_with_warmup(learning_rate, step_each_epoch, epochs=120):
fluid
.
layers
.
tensor
.
assign
(
input
=
decayed_lr
,
output
=
lr
)
with
switch
.
default
():
decayed_lr
=
learning_rate
*
\
(
ops
.
cos
((
global_step
-
warmup_epoch
*
step_each_epoch
)
*
(
math
.
pi
/
(
epochs
*
step_each_epoch
)))
+
1
)
/
2
(
ops
.
cos
((
global_step
-
warmup_epoch
*
step_each_epoch
)
*
(
math
.
pi
/
(
(
epochs
-
warmup_epoch
)
*
step_each_epoch
)))
+
1
)
/
2
fluid
.
layers
.
tensor
.
assign
(
input
=
decayed_lr
,
output
=
lr
)
return
lr
def
exponential_decay_with_warmup
(
learning_rate
,
step_each_epoch
,
decay_epochs
,
decay_rate
=
0.97
,
warm_up_epoch
=
5.0
):
def
exponential_decay_with_warmup
(
learning_rate
,
step_each_epoch
,
decay_epochs
,
decay_rate
=
0.97
,
warm_up_epoch
=
5.0
):
"""Applies exponential decay to the learning rate.
"""
global_step
=
_decay_step_counter
()
...
...
@@ -80,16 +88,19 @@ def exponential_decay_with_warmup(learning_rate, step_each_epoch, decay_epochs,
epoch
=
ops
.
floor
(
global_step
/
step_each_epoch
)
with
fluid
.
layers
.
control_flow
.
Switch
()
as
switch
:
with
switch
.
case
(
epoch
<
warmup_epoch
):
decayed_lr
=
learning_rate
*
(
global_step
/
(
step_each_epoch
*
warmup_epoch
))
decayed_lr
=
learning_rate
*
(
global_step
/
(
step_each_epoch
*
warmup_epoch
))
fluid
.
layers
.
assign
(
input
=
decayed_lr
,
output
=
lr
)
with
switch
.
default
():
div_res
=
(
global_step
-
warmup_epoch
*
step_each_epoch
)
/
decay_epochs
div_res
=
(
global_step
-
warmup_epoch
*
step_each_epoch
)
/
decay_epochs
div_res
=
ops
.
floor
(
div_res
)
decayed_lr
=
learning_rate
*
(
decay_rate
**
div_res
)
decayed_lr
=
learning_rate
*
(
decay_rate
**
div_res
)
fluid
.
layers
.
assign
(
input
=
decayed_lr
,
output
=
lr
)
return
lr
def
lr_warmup
(
learning_rate
,
warmup_steps
,
start_lr
,
end_lr
):
""" Applies linear learning rate warmup for distributed training
Argument learning_rate can be float or a Variable
...
...
@@ -193,7 +204,8 @@ class Optimizer(object):
learning_rate
=
cosine_decay_with_warmup
(
learning_rate
=
self
.
lr
,
step_each_epoch
=
self
.
step
,
epochs
=
self
.
num_epochs
)
epochs
=
self
.
num_epochs
,
warm_up_epoch
=
self
.
warm_up_epochs
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
learning_rate
,
momentum
=
self
.
momentum_rate
,
...
...
@@ -218,8 +230,7 @@ class Optimizer(object):
regularization
=
fluid
.
regularizer
.
L2Decay
(
self
.
l2_decay
),
momentum
=
self
.
momentum_rate
,
rho
=
0.9
,
epsilon
=
0.001
)
epsilon
=
0.001
)
return
optimizer
def
linear_decay
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录