Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
74b45743
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 1 年 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
74b45743
编写于
4月 22, 2022
作者:
H
HydrogenSulfate
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support for multiple components when build optimizer
上级
8ae89343
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
24 addition
and
20 deletion
+24
-20
ppcls/optimizer/__init__.py
ppcls/optimizer/__init__.py
+24
-20
未找到文件。
ppcls/optimizer/__init__.py
浏览文件 @
74b45743
...
...
@@ -67,12 +67,13 @@ def build_optimizer(config, epochs, step_each_epoch, model_list=None):
# optim_cfg = {optim_name: {'scope': xxx, **optim_cfg}}
# step1 build lr
optim_name
=
list
(
optim_item
.
keys
())[
0
]
# get optim_name
optim_scope
=
optim_item
[
optim_name
].
pop
(
'scope'
)
# get optim_scope
optim_scope_list
=
optim_item
[
optim_name
].
pop
(
'scope'
).
split
(
' '
)
# get optim_scope list
optim_cfg
=
optim_item
[
optim_name
]
# get optim_cfg
lr
=
build_lr_scheduler
(
optim_cfg
.
pop
(
'lr'
),
epochs
,
step_each_epoch
)
logger
.
info
(
"build lr ({}) for scope ({}) success.."
.
format
(
lr
.
__class__
.
__name__
,
optim_scope
))
lr
.
__class__
.
__name__
,
optim_scope
_list
))
# step2 build regularization
if
'regularizer'
in
optim_cfg
and
optim_cfg
[
'regularizer'
]
is
not
None
:
if
'weight_decay'
in
optim_cfg
:
...
...
@@ -84,11 +85,13 @@ def build_optimizer(config, epochs, step_each_epoch, model_list=None):
reg
=
getattr
(
paddle
.
regularizer
,
reg_name
)(
**
reg_config
)
optim_cfg
[
"weight_decay"
]
=
reg
logger
.
info
(
"build regularizer ({}) for scope ({}) success.."
.
format
(
reg
.
__class__
.
__name__
,
optim_scope
))
format
(
reg
.
__class__
.
__name__
,
optim_scope
_list
))
# step3 build optimizer
if
'clip_norm'
in
optim_cfg
:
clip_norm
=
optim_cfg
.
pop
(
'clip_norm'
)
grad_clip
=
paddle
.
nn
.
ClipGradByNorm
(
clip_norm
=
clip_norm
)
logger
.
info
(
"build gradclip ({}) for scope ({}) success.."
.
format
(
grad_clip
.
__class__
.
__name__
,
optim_scope_list
))
else
:
grad_clip
=
None
optim_model
=
[]
...
...
@@ -101,33 +104,34 @@ def build_optimizer(config, epochs, step_each_epoch, model_list=None):
return
optim
,
lr
# for dynamic graph
if
optim_scope
==
"all"
:
optim_model
=
model_list
elif
optim_scope
==
"model"
:
optim_model
=
[
model_list
[
0
],
]
elif
optim_scope
in
[
"backbone"
,
"neck"
,
"head"
]:
optim_model
=
[
getattr
(
model_list
[
0
],
optim_scope
,
None
),
]
elif
optim_scope
==
"loss"
:
optim_model
=
[
model_list
[
1
],
]
for
scope
in
optim_scope_list
:
if
scope
==
"all"
:
optim_model
+=
model_list
elif
scope
==
"model"
:
optim_model
+=
[
model_list
[
0
],
]
elif
scope
in
[
"backbone"
,
"neck"
,
"head"
]:
optim_model
+=
[
getattr
(
model_list
[
0
],
scope
,
None
),
]
elif
scope
==
"loss"
:
optim_model
+=
[
model_list
[
1
],
]
else
:
optim_model
=
[
optim_model
+
=
[
model_list
[
1
].
loss_func
[
i
]
for
i
in
range
(
len
(
model_list
[
1
].
loss_func
))
if
model_list
[
1
].
loss_func
[
i
].
__class__
.
__name__
==
optim_
scope
if
model_list
[
1
].
loss_func
[
i
].
__class__
.
__name__
==
scope
]
# remove invalid items
optim_model
=
[
optim_model
[
i
]
for
i
in
range
(
len
(
optim_model
))
if
(
optim_model
[
i
]
is
not
None
)
and
(
len
(
optim_model
[
i
].
parameters
())
>
0
)
]
assert
len
(
optim_model
)
>
0
,
\
f
"optim_model is empty for optim_scope(
{
optim_scope
}
)"
f
"optim_model is empty for optim_scope(
{
optim_scope_list
}
)"
optim
=
getattr
(
optimizer
,
optim_name
)(
learning_rate
=
lr
,
grad_clip
=
grad_clip
,
**
optim_cfg
)(
model_list
=
optim_model
)
logger
.
info
(
"build optimizer ({}) for scope ({}) success.."
.
format
(
optim
.
__class__
.
__name__
,
optim_scope
))
optim
.
__class__
.
__name__
,
optim_scope
_list
))
optim_list
.
append
(
optim
)
lr_list
.
append
(
lr
)
return
optim_list
,
lr_list
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录