-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
58 lines (43 loc) · 1.28 KB
/
plot.py
File metadata and controls
58 lines (43 loc) · 1.28 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 matplotlib.pyplot as plt
from torch.optim import AdamW
import torch.nn as nn
import numpy as np
from onecyclec import OneCycleCosineAdam
class ToyModel(nn.Module):
def __init__(self):
super(ToyModel, self).__init__()
self.fc = nn.Linear(1, 1)
def forward(self, x):
return self.fc(x)
model = ToyModel()
optimizer = AdamW(model.parameters(), lr=0.01)
WARMUP = 0.3
PLATEAU = 0.1
WINDDOWN = 0.7
N = 1000
sched = OneCycleCosineAdam(optimizer,
warmup=WARMUP,
plateau=PLATEAU,
winddown=WINDDOWN,
num_steps=N)
momentum = []
lr = []
for n in range(0, N):
momentum.append(sched.momentum)
lr.append(sched.lr)
sched.step()
lr = np.array(lr)
momentum = np.array(momentum)
fig, ax1 = plt.subplots()
plt.grid()
ax2 = ax1.twinx()
ax1.set_xlabel('step')
ax1.set_ylabel('learning rate', color='tab:blue')
ax1.plot(lr, color='tab:blue')
ax1.tick_params(axis='y', labelcolor='tab:blue')
ax2.set_ylabel('momentum', color='green')
ax2.plot(momentum, color='green')
ax2.tick_params(axis='y', labelcolor='green')
plt.title('warmup = %.1f plateau = %.1f winddown = %.1f' % (WARMUP, PLATEAU, WINDDOWN))
plt.savefig('sched.png', dpi=100, tight=True)
plt.show()