forked from PaddlePaddle/PaddleVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlr.py
52 lines (41 loc) · 1.76 KB
/
lr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict
from paddle.optimizer.lr import LRScheduler
from . import custom_lr
def build_lr(cfg: Dict, num_iters: int) -> LRScheduler:
"""Build a learning rate scheduler accroding to ```OPTIMIZER``` configuration, and it always pass into the optimizer.
In configuration:
learning_rate:
name: 'PiecewiseDecay'
boundaries: [20, 60]
values: [0.00025, 0.000025, 0.0000025]
Args:
cfg (Dict): learning rate configuration.
num_iters (int): The number of iterations that may be used when calculating the learning rate
Returns:
LRScheduler: learning rate scheduler.
"""
cfg_copy = cfg.copy()
#when learning_rate is LRScheduler
if cfg_copy.get('learning_rate') and isinstance(cfg_copy['learning_rate'],
dict):
cfg_copy['learning_rate'] = build_lr(
cfg_copy['learning_rate'],
num_iters) #not support only inner iter_step
lr_name = cfg_copy.pop('name')
if cfg_copy.get('iter_step'):
cfg_copy['num_iters'] = num_iters
cfg_copy.pop('iter_step')
return getattr(custom_lr, lr_name)(**cfg_copy)