-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathfixed.py
68 lines (52 loc) · 2.14 KB
/
fixed.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
from corenet.optims.scheduler import SCHEDULER_REGISTRY
from corenet.optims.scheduler.base_scheduler import BaseLRScheduler
@SCHEDULER_REGISTRY.register("fixed")
class FixedLRScheduler(BaseLRScheduler):
"""
Fixed learning rate scheduler with optional linear warm-up strategy
"""
def __init__(self, opts, **kwargs) -> None:
is_iter_based = getattr(opts, "scheduler.is_iteration_based", True)
super(FixedLRScheduler, self).__init__(opts=opts)
max_iterations = getattr(opts, "scheduler.max_iterations", 150000)
self.fixed_lr = getattr(opts, "scheduler.fixed.lr", None)
assert self.fixed_lr is not None
if self.warmup_iterations > 0:
self.warmup_step = (
self.fixed_lr - self.warmup_init_lr
) / self.warmup_iterations
self.period = (
max_iterations - self.warmup_iterations + 1
if is_iter_based
else getattr(opts, "scheduler.max_epochs", 350)
)
self.is_iter_based = is_iter_based
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(
title="Fixed LR arguments", description="Fixed LR arguments"
)
group.add_argument(
"--scheduler.fixed.lr", type=float, default=None, help="LR value"
)
return parser
def get_lr(self, epoch: int, curr_iter: int) -> float:
if curr_iter < self.warmup_iterations:
curr_lr = self.warmup_init_lr + curr_iter * self.warmup_step
else:
curr_lr = self.fixed_lr
return max(0.0, curr_lr)
def __repr__(self) -> str:
repr_str = "{}(".format(self.__class__.__name__)
repr_str += "\n\tlr={}".format(self.fixed_lr)
if self.warmup_iterations > 0:
repr_str += "\n\twarmup_init_lr={}\n\twarmup_iters={}".format(
self.warmup_init_lr, self.warmup_iterations
)
repr_str += "\n )"
return repr_str