-
Notifications
You must be signed in to change notification settings - Fork 1
/
lr_scheduler.py
165 lines (136 loc) · 4.46 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import math
from bisect import bisect_right
import torch
class WarmupLrScheduler(torch.optim.lr_scheduler._LRScheduler):
def __init__(
self,
optimizer,
warmup_iter=500,
warmup_ratio=5e-4,
warmup='exp',
last_epoch=-1,
):
self.warmup_iter = warmup_iter
self.warmup_ratio = warmup_ratio
self.warmup = warmup
super(WarmupLrScheduler, self).__init__(optimizer, last_epoch)
def get_lr(self):
ratio = self.get_lr_ratio()
lrs = [ratio * lr for lr in self.base_lrs]
return lrs
def get_lr_ratio(self):
if self.last_epoch < self.warmup_iter:
ratio = self.get_warmup_ratio()
else:
ratio = self.get_main_ratio()
return ratio
def get_main_ratio(self):
raise NotImplementedError
def get_warmup_ratio(self):
assert self.warmup in ('linear', 'exp')
alpha = self.last_epoch / self.warmup_iter
if self.warmup == 'linear':
ratio = self.warmup_ratio + (1 - self.warmup_ratio) * alpha
elif self.warmup == 'exp':
ratio = self.warmup_ratio ** (1. - alpha)
return ratio
class WarmupPolyLrScheduler(WarmupLrScheduler):
def __init__(
self,
optimizer,
power,
max_iter,
warmup_iter=500,
warmup_ratio=5e-4,
warmup='exp',
last_epoch=-1,
):
self.power = power
self.max_iter = max_iter
super(WarmupPolyLrScheduler, self).__init__(
optimizer, warmup_iter, warmup_ratio, warmup, last_epoch)
def get_main_ratio(self):
real_iter = self.last_epoch - self.warmup_iter
real_max_iter = self.max_iter - self.warmup_iter
alpha = real_iter / real_max_iter
ratio = (1 - alpha) ** self.power
return ratio
class WarmupExpLrScheduler(WarmupLrScheduler):
def __init__(
self,
optimizer,
gamma,
interval=1,
warmup_iter=500,
warmup_ratio=5e-4,
warmup='exp',
last_epoch=-1,
):
self.gamma = gamma
self.interval = interval
super(WarmupExpLrScheduler, self).__init__(
optimizer, warmup_iter, warmup_ratio, warmup, last_epoch)
def get_main_ratio(self):
real_iter = self.last_epoch - self.warmup_iter
ratio = self.gamma ** (real_iter // self.interval)
return ratio
class WarmupCosineLrScheduler(WarmupLrScheduler):
def __init__(
self,
optimizer,
max_iter,
eta_ratio=0,
warmup_iter=500,
warmup_ratio=5e-4,
warmup='exp',
last_epoch=-1,
):
self.eta_ratio = eta_ratio
self.max_iter = max_iter
super(WarmupCosineLrScheduler, self).__init__(
optimizer, warmup_iter, warmup_ratio, warmup, last_epoch)
def get_main_ratio(self):
real_iter = self.last_epoch - self.warmup_iter
real_max_iter = self.max_iter - self.warmup_iter
return self.eta_ratio + (1 - self.eta_ratio) * (
1 + math.cos(math.pi * self.last_epoch / real_max_iter)) / 2
class WarmupStepLrScheduler(WarmupLrScheduler):
def __init__(
self,
optimizer,
milestones: list,
gamma=0.1,
warmup_iter=500,
warmup_ratio=5e-4,
warmup='exp',
last_epoch=-1,
):
self.milestones = milestones
self.gamma = gamma
super(WarmupStepLrScheduler, self).__init__(
optimizer, warmup_iter, warmup_ratio, warmup, last_epoch)
def get_main_ratio(self):
real_iter = self.last_epoch - self.warmup_iter
ratio = self.gamma ** bisect_right(self.milestones, real_iter)
return ratio
if __name__ == "__main__":
model = torch.nn.Conv2d(3, 16, 3, 1, 1)
optim = torch.optim.SGD(model.parameters(), lr=5e-3)
max_iter = 15000
lr_scheduler = WarmupPolyLrScheduler(optim, 0.9, max_iter, 100, 0.1, 'exp', -1)
lrs = []
for i in range(max_iter):
lr = lr_scheduler.get_lr()[0]
print(i, lr)
lrs.append(lr)
lr_scheduler.step()
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
lrs = np.array(lrs)
n_lrs = len(lrs)
plt.plot(np.arange(n_lrs), lrs)
plt.grid()
plt.show()