forked from nec-research/Adaptive-Message-Passing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
58 lines (44 loc) · 2.17 KB
/
Copy pathscheduler.py
File metadata and controls
58 lines (44 loc) · 2.17 KB
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
import math
import torch.distributed.optim.optimizer
from pydgn.experiment.util import s2c
from pydgn.training.callback.optimizer import Optimizer
from pydgn.training.callback.scheduler import Scheduler, EpochScheduler
from pydgn.training.engine import TrainingEngine
from pydgn.training.event.state import State
from torch.optim.lr_scheduler import MultiplicativeLR, StepLR, LambdaLR
class ExplicitLR(StepLR):
""""""
def __init__(self, optimizer, lrs, last_epoch=-1, verbose=False):
self.lrs = lrs
super().__init__(optimizer, last_epoch, verbose)
def get_lr(self):
if self.last_epoch >= len(self.lrs):
return [group["lr"] for group in self.optimizer.param_groups]
return [self.lrs[self.last_epoch] for _ in self.optimizer.param_groups]
class CifarScheduler(Scheduler):
def __init__(
self, scheduler_class_name: str, optimizer: Optimizer, **kwargs: dict
):
lrs = [0.01] * 5 + [0.1] * 195 + [0.01] * 100 + [0.001] * 100
self.scheduler = ExplicitLR(optimizer, lrs)
def on_epoch_start(self, state: State):
super(CifarScheduler, self).on_epoch_start(state)
self.scheduler.last_epoch = state.epoch
class CosineAnnealingLinearWarmup(EpochScheduler):
def __init__(
self, scheduler_class_name: str, optimizer: Optimizer, **kwargs: dict
):
assert scheduler_class_name == 'torch.optim.lr_scheduler.LambdaLR'
num_warmup_steps = kwargs['num_warmup_steps']
num_training_steps = kwargs['num_training_steps']
num_cycles = kwargs['num_cycles']
last_epoch = kwargs.get('last_epoch', -1)
def lr_lambda(current_step):
if current_step < num_warmup_steps:
return max(1e-6, float(current_step) / float(
max(1, num_warmup_steps)))
progress = float(current_step - num_warmup_steps) / float(
max(1, num_training_steps - num_warmup_steps))
return max(0.0, 0.5 * (1.0 + math.cos(
math.pi * float(num_cycles) * 2.0 * progress)))
self.scheduler = LambdaLR(optimizer, lr_lambda, last_epoch)