-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind_learnrate.py
99 lines (83 loc) · 3.01 KB
/
find_learnrate.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
from torch.utils.data.dataset import ConcatDataset
from torch.utils.data.dataloader import DataLoader
import datasets
import models
from utils import filenames_from_splitfile
from utils import ChunkedRandomSampler
from utils import find_learnrate
from utils import OneCyclePolicy
import os
import numpy as np
import argparse
import matplotlib.pyplot as plt
def main():
parser = argparse.ArgumentParser(description=
'find the right learnrate for a particular' +
'architecture / set of other hyperparameters')
parser.add_argument('splits', type=str, help='on which splits to train')
parser.add_argument('model', choices=models.get_model_classes(),
help='any classname of model as defined in "models.py"')
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--n_steps', type=int, default=1024)
parser.add_argument('--plotname', type=str,
default='find_learnrate.pdf',
help='the name of the plot')
parser.add_argument('--device', type=str, default='cuda')
args = parser.parse_args()
cuda = False
if args.device.startswith('cpu'):
cuda = False
elif args.device.startswith('cuda'):
cuda = True
else:
print('unknown device type "{}"'.format(args.device))
exit(-1)
##########################################
# prepare train data
train_filenames = filenames_from_splitfile(os.path.join(args.splits, 'train'))
train_sequences = datasets.get_sequences(train_filenames)
batch_size = args.batch_size
n_steps = args.n_steps
# one train loader for all train sequences
train_dataset = ConcatDataset(train_sequences)
train_loader = DataLoader(
train_dataset,
batch_size=batch_size,
sampler=ChunkedRandomSampler(train_dataset, batch_size * n_steps),
num_workers=1,
pin_memory=True,
drop_last=True
)
net_class = getattr(models, args.model, None)
if net_class is None:
raise RuntimeError('could not find model class named "{}" in "models.py"'.format(args.model))
net = net_class()
optimizer = OneCyclePolicy(
net.parameters(),
learnrates=np.linspace(
1e-5,
50,
n_steps
),
momenta=np.ones(n_steps) * 0.9,
nesterov=True
)
if cuda:
net.cuda()
losses, learning_rates = find_learnrate(
cuda=cuda,
net=net,
optimizer=optimizer,
loader=train_loader
)
fig, ax = plt.subplots()
ax.set_title('find minimum, divide by 10, that is your initial guess!\n' +
'then either reduce or increase learnrate,\n' +
'until first few hundred updates do not diverge.')
ax.semilogx(learning_rates, losses)
ax.set_xlabel('learning rate (log scale)')
ax.set_ylabel('loss')
fig.tight_layout()
fig.savefig(args.plotname)
if __name__ == '__main__':
main()