-
Notifications
You must be signed in to change notification settings - Fork 1
/
lr_scheduler.py
50 lines (43 loc) · 1.44 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
import numpy as np
from torch.optim.lr_scheduler import _LRScheduler
class LogScheduler(_LRScheduler):
def __init__(self, optimizer, start_lr=0.03, end_lr=5e-4,
epochs=50, last_epoch=-1, **kwargs):
self.lr_spaces = np.logspace(np.log10(start_lr),
np.log10(end_lr),
epochs)
super(LogScheduler, self).__init__(optimizer, last_epoch)
def get_lr(self):
"""
Mainly rewrite this method, to get lr for
`scheduler.step()` usage
Returns:
current epoch lr, must iterable
"""
# Because LogScheduler() initialization will call once step()
if self.last_epoch == 0:
epoch = self.last_epoch
else:
epoch = self.last_epoch - 1
return [self.lr_spaces[epoch]]
if __name__ == '__main__':
import torch.nn as nn
from torch.optim import SGD
import matplotlib.pyplot as plt
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 1, kernel_size=1)
net = Net()
# for i in net:
# print(i)
optimizer = SGD(net.parameters(), lr=0.01)
scheduler = LogScheduler(optimizer)
lr = []
for i in range(1, 51):
lr.append(optimizer.param_groups[0]['lr'])
optimizer.step()
scheduler.step()
print(len(lr))
plt.plot(lr)
plt.show()