-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlearning_schdule.py
353 lines (318 loc) · 12.3 KB
/
learning_schdule.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# from linear_warmup_cosine_annealing_warm_restarts_weight_decay import ChainedScheduler
# import torch
# import torch.optim as optim
# from torchvision.models import AlexNet
# from torch.optim import lr_scheduler
# import numpy as np
# import matplotlib.pyplot as plt
# import matplotlib
# model = AlexNet(num_classes=2)
# optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=1e-1)
# scheduler = ChainedScheduler(
# optimizer,
# T_0 = 50,
# T_mul = 2,
# eta_min = 0.0,
# gamma = 0.9,
# max_lr = 0.001,
# warmup_steps=0,
# )
#
#
# fig = matplotlib.pyplot.gcf()
# matplotlib.rcParams['figure.figsize'] = [18.5, 10]
# x = list(range(3000))
# y = []
#
# for epoch in range(3000):
# optimizer.step()
# scheduler.step()
# y.append(scheduler.get_lr()[0])
#
# fig, axes = plt.subplots(1, 1)
# xticks = range(min(x), max(x) + 1)
# y_mat = np.array(y).reshape(-1, 1)
# # axes.set_xticks(xticks)
# plt.plot(xticks, y)
# # plt.grid()
# plt.show()
import math
import torch
from typing import Optional
from torch.optim.lr_scheduler import _LRScheduler
class WarmUpScheduler(_LRScheduler):
"""
Args:
optimizer: [torch.optim.Optimizer] only pass if using as astand alone lr_scheduler
"""
def __init__(
self,
optimizer: torch.optim.Optimizer,
eta_min: float = 0.0,
last_epoch=-1,
max_lr: Optional[float] = 0.1,
warmup_steps: Optional[int] = 0,
):
if warmup_steps != 0:
assert warmup_steps >= 0
self.base_max_lr = max_lr
self.max_lr = max_lr
self.step_in_cycle = last_epoch
self.eta_min = eta_min
self.warmup_steps = warmup_steps # warmup
super(WarmUpScheduler, self).__init__(optimizer, last_epoch)
self.init_lr()
def init_lr(self):
self.base_lrs = []
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.eta_min
self.base_lrs.append(self.eta_min)
def get_lr(self):
if self.step_in_cycle == -1:
return self.base_lrs
elif self.step_in_cycle < self.warmup_steps:
return [(self.max_lr - base_lr) * self.step_in_cycle / self.warmup_steps + base_lr
for base_lr in self.base_lrs]
else:
return [base_lr + (self.max_lr - base_lr) for base_lr in self.base_lrs]
def step(self, epoch=None):
self.epoch = epoch
if self.epoch is None:
self.epoch = self.last_epoch + 1
self.step_in_cycle = self.step_in_cycle + 1
else:
self.step_in_cycle = self.epoch
self.max_lr = self.base_max_lr
self.last_epoch = math.floor(self.epoch)
for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
param_group['lr'] = lr
class CosineAnealingWarmRestartsWeightDecay(_LRScheduler):
"""
Helper class for chained scheduler not to used directly. this class is synchronised with
previous stage i.e. WarmUpScheduler (max_lr, T_0, T_cur etc) and is responsible for
CosineAnealingWarmRestarts with weight decay
"""
def __init__(
self,
optimizer: torch.optim.Optimizer,
T_0: int,
T_mul: float = 1.,
eta_min: float = 0.001,
last_epoch=-1,
max_lr: Optional[float] = 0.1,
gamma: Optional[float] = 1.,
):
if T_0 <= 0 or not isinstance(T_0, int):
raise ValueError("Expected positive integer T_0, but got {}".format(T_0))
if T_mul < 1 or not isinstance(T_mul, int):
raise ValueError("Expected integer T_mul >= 1, but got {}".format(T_mul))
self.T_0 = T_0
self.T_mul = T_mul
self.base_max_lr = max_lr
self.max_lr = max_lr
self.T_i = T_0 # number of epochs between two warm restarts
self.cycle = 0
self.eta_min = eta_min
self.gamma = gamma
self.T_cur = last_epoch # number of epochs since the last restart
super(CosineAnealingWarmRestartsWeightDecay, self).__init__(optimizer, last_epoch)
self.init_lr()
def init_lr(self):
self.base_lrs = []
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.eta_min
self.base_lrs.append(self.eta_min)
def get_lr(self):
return [
base_lr + (self.max_lr - base_lr) * (1 + math.cos(math.pi * self.T_cur / self.T_i)) / 2
for base_lr in self.base_lrs
]
def step(self, epoch=None):
self.epoch = epoch
if self.epoch is None:
self.epoch = self.last_epoch + 1
self.T_cur = self.T_cur + 1
if self.T_cur >= self.T_i:
self.cycle += 1
self.T_cur = self.T_cur - self.T_i
self.T_i = self.T_i * self.T_mul
# since warmup steps must be < T_0 and if epoch count > T_0 we just apply cycle count for weight decay
if self.epoch >= self.T_0:
if self.T_mul == 1.:
self.T_cur = self.epoch % self.T_0
self.cycle = self.epoch // self.T_0
else:
n = int(math.log((self.epoch / self.T_0 * (self.T_mul - 1) + 1), self.T_mul))
self.cycle = n
self.T_cur = self.epoch - int(self.T_0 * (self.T_mul ** n - 1) / (self.T_mul - 1))
self.T_i = self.T_0 * self.T_mul ** (n)
# base condition that applies original implementation for cosine cycles for details visit:
# https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.CosineAnnealingWarmRestarts.html
else:
self.T_i = self.T_0
self.T_cur = self.epoch
# this is where weight decay is applied
self.max_lr = self.base_max_lr * (self.gamma ** self.cycle)
self.last_epoch = math.floor(self.epoch)
for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
param_group['lr'] = lr
class ChainedScheduler(_LRScheduler):
"""
Driver class
Args:
T_0: First cycle step size, Number of iterations for the first restart.
T_mul: multiplicative factor Default: -1., A factor increases T_i after a restart
eta_min: Min learning rate. Default: 0.001.
max_lr: warmup's max learning rate. Default: 0.1. shared between both schedulers
warmup_steps: Linear warmup step size. Number of iterations to complete the warmup
gamma: Decrease rate of max learning rate by cycle. Default: 1.0 i.e. no decay
last_epoch: The index of last epoch. Default: -1
Usage:
ChainedScheduler without initial warmup and weight decay:
scheduler = ChainedScheduler(
optimizer,
T_0=20,
T_mul=2,
eta_min = 1e-5,
warmup_steps=0,
gamma = 1.0
)
ChainedScheduler with weight decay only:
scheduler = ChainedScheduler(
self,
optimizer: torch.optim.Optimizer,
T_0: int,
T_mul: float = 1.0,
eta_min: float = 0.001,
last_epoch=-1,
max_lr: Optional[float] = 1.0,
warmup_steps: int = 0,
gamma: Optional[float] = 0.9
)
ChainedScheduler with initial warm up and weight decay:
scheduler = ChainedScheduler(
self,
optimizer: torch.optim.Optimizer,
T_0: int,
T_mul: float = 1.0,
eta_min: float = 0.001,
last_epoch = -1,
max_lr: Optional[float] = 1.0,
warmup_steps: int = 10,
gamma: Optional[float] = 0.9
)
Example:
>>> model = AlexNet(num_classes=2)
>>> optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=1e-1)
>>> scheduler = ChainedScheduler(
>>> optimizer,
>>> T_0 = 20,
>>> T_mul = 1,
>>> eta_min = 0.0,
>>> gamma = 0.9,
>>> max_lr = 1.0,
>>> warmup_steps= 5 ,
>>> )
>>> for epoch in range(100):
>>> optimizer.step()
>>> scheduler.step()
Proper Usage:
https://wandb.ai/wandb_fc/tips/reports/How-to-Properly-Use-PyTorch-s-CosineAnnealingWarmRestarts-Scheduler--VmlldzoyMTA3MjM2
"""
def __init__(
self,
optimizer: torch.optim.Optimizer,
T_0: int,
T_mul: float = 1.0,
eta_min: float = 0.001,
last_epoch=-1,
max_lr: Optional[float] = 1.0,
warmup_steps: Optional[int] = 5,
gamma: Optional[float] = 0.95,
):
if T_0 <= 0 or not isinstance(T_0, int):
raise ValueError("Expected positive integer T_0, but got {}".format(T_0))
if T_mul < 1 or not isinstance(T_mul, int):
raise ValueError("Expected integer T_mul >= 1, but got {}".format(T_mul))
if warmup_steps != 0:
assert warmup_steps < T_0
warmup_steps = warmup_steps + 1 # directly refers to epoch account for 0 off set
self.T_0 = T_0
self.T_mul = T_mul
self.base_max_lr = max_lr
self.max_lr = max_lr
self.T_i = T_0 # number of epochs between two warm restarts
self.cycle = 0
self.eta_min = eta_min
self.warmup_steps = warmup_steps # warmup
self.gamma = gamma
self.T_cur = last_epoch # number of epochs since the last restart
self.last_epoch = last_epoch
self.cosine_scheduler1 = WarmUpScheduler(
optimizer,
eta_min=self.eta_min,
warmup_steps=self.warmup_steps,
max_lr=self.max_lr,
)
self.cosine_scheduler2 = CosineAnealingWarmRestartsWeightDecay(
optimizer,
T_0=self.T_0,
T_mul=self.T_mul,
eta_min=self.eta_min,
max_lr=self.max_lr,
gamma=self.gamma,
)
def get_lr(self):
if self.warmup_steps != 0:
if self.epoch < self.warmup_steps:
return self.cosine_scheduler1.get_lr()
if self.epoch >= self.warmup_steps:
return self.cosine_scheduler2.get_lr()
def step(self, epoch=None):
self.epoch = epoch
if self.epoch is None:
self.epoch = self.last_epoch + 1
if self.warmup_steps != 0:
if self.epoch < self.warmup_steps:
self.cosine_scheduler1.step()
self.last_epoch = self.epoch
if self.epoch >= self.warmup_steps:
self.cosine_scheduler2.step()
self.last_epoch = self.epoch
# import torch
# import torch.optim as optim
# from torchvision.models import AlexNet
# from torch.optim import lr_scheduler
# import numpy as np
# import matplotlib.pyplot as plt
# import matplotlib
# model = AlexNet(num_classes=2)
# optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=1e-1)
# scheduler = ChainedScheduler(
# optimizer,
# T_0 = 50,
# T_mul = 2,
# eta_min = 0.0,
# gamma = 0.9,
# max_lr = 0.001,
# warmup_steps=0,
# )
#
#
# fig = matplotlib.pyplot.gcf()
# matplotlib.rcParams['figure.figsize'] = [18.5, 10]
# x = list(range(3000))
# y = []
#
# for epoch in range(3000):
# optimizer.step()
# scheduler.step()
# y.append(scheduler.get_lr()[0])
#
# fig, axes = plt.subplots(1, 1)
# xticks = range(min(x), max(x) + 1)
# y_mat = np.array(y).reshape(-1, 1)
# # axes.set_xticks(xticks)
# plt.plot(xticks, y)
# # plt.grid()
# plt.show()