-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlr_scheduler.py
31 lines (25 loc) · 1.13 KB
/
lr_scheduler.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
import math
class CosLrWarmupScheduler():
def __init__(self, optimizer, total_iter):
assert type(total_iter) is int
self.optimizer = optimizer
self.total_iter = total_iter
self.base_lr = [group['lr'] for group in optimizer.param_groups]
self.current_iter = 0
def step(self):
self.current_iter += 1
if self.current_iter > self.total_iter:
return
for i, group in enumerate(self.optimizer.param_groups):
group['lr'] = (math.cos(math.pi + self.current_iter/self.total_iter * math.pi) + 1.000001) * 0.5 * self.base_lr[i]
class CosLr():
def __init__(self, optimizer, period_iter):
assert type(period_iter) is int
self.optimizer = optimizer
self.total_iter = period_iter
self.base_lr = [group['lr'] for group in optimizer.param_groups]
self.current_iter = 0
def step(self):
self.current_iter += 1
for i, group in enumerate(self.optimizer.param_groups):
group['lr'] = (math.cos(self.current_iter/self.total_iter * math.pi) + 1.000001) * 0.5 * self.base_lr[i]