This repository was archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlr.py
More file actions
324 lines (258 loc) · 14.1 KB
/
lr.py
File metadata and controls
324 lines (258 loc) · 14.1 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
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
# -*- coding: utf-8 -*-
"""Scheduling Learning Rates.
.. rubric:: References
.. [ginsburg2018large] Ginsburg, Boris and Gitman, Igor and You, Yang
Large Batch Training of Convolutional Networks with Layer-wise Adaptive Rate Scaling
.. [leslie2017cyclical] Leslie N. Smith
Cyclical Learning Rates for Training Neural Networks
.. [goyal2017accurate] Goyal, Priya, et al.
Accurate, large minibatch SGD: training imagenet in 1 hour.
.. [smith2017super] Smith, Leslie N., and Nicholay Topin.
Super-Convergence: Very Fast Training of Residual Networks Using Large Learning Rates.
"""
from torch.optim.lr_scheduler import LambdaLR, MultiStepLR
import argparse
import numpy as np
import re
from bisect import bisect_left, bisect_right
def parse_batch_epoch(s, sep=',', type_=int):
"""
Assume the input string ``s`` should have a pattern of ``epoch:1``, ``batch:50``.
For list write ``epoch:1,10``. The numbers should be non-negative integers.
"""
if re.match(r'epoch:[\d,]+\Z', s):
s = s[len('epoch:'):]
return {'epoch': type_(s) if sep not in s else [type_(i) for i in s.split(sep)]}
elif re.match(r'batch:[\d,]+\Z', s):
s = s[len('batch:'):]
return {'batch': type_(s) if sep not in s else [type_(i) for i in s.split(sep)]}
else:
raise ValueError("The pattern should be `epoch:1`, `batch:50`, `epoch:1,2`, etc. Got {}"
.format(s))
class SchedulerParser(argparse.ArgumentParser):
def __init__(self, add_help=False, multisteplr_milestones=True, multisteplr_gamma=True,
warmup=True, warmup_init_lr=True, warmup_linear_scaling=True, warmup_durations=True,
clr_cycle_length=True, clr_base_lr=True, clr_max_lr=True, clr_mode=True,
clr_gamma=True, clr_extra=True, sgd_lr_alpha=True, sgd_lr_beta=True, sgd_lr_gamma=True):
super(SchedulerParser, self).__init__(add_help=add_help)
if multisteplr_milestones:
self.add_argument('--multisteplr_milestones', type=parse_batch_epoch,
default={"batch": [32000, 48000]}, metavar='<MSLRMS>',
help="[default: %(default)s] milestones for multistep learning rate schedule.")
if multisteplr_gamma:
self.add_argument('--multisteplr_gamma', type=float, default=0.1, metavar='<MSLRG>',
help="[default: %(default)s] Multiplicative factor of learning rate decay.")
if warmup:
self.add_argument("--warmup", default=False, action="store_true",
help="[default: %(default)s] linearly warmup learning rate before other scheduling."
"For the moment, only implemented for multistep learning rate with warmup."
"The warmup is used for training with more than one process.")
if warmup_init_lr:
warmup_init_lr_group = self.add_mutually_exclusive_group()
warmup_init_lr_group.add_argument("--warmup_init_lr", type=float, default=0.0, metavar='<WILR>',
help="[default: %(default)s] Initial learning rate before warmup.")
warmup_init_lr_group.add_argument("--warmup_init_lr_nonscale", action='store_true', default=False,
help="[default: %(default)s] Use nonscaled lr for initial warmup lr"
"for training. If this flag is true, then ignore")
if warmup_linear_scaling:
self.add_argument("--warmup_linear_scaling", action='store_true', default=False,
help="[default: %(default)s] scale the learning rate by a factor after warmup."
"For linear scaling rule, this factor is the number of machines.")
if warmup_durations:
self.add_argument("--warmup_durations", type=parse_batch_epoch, default={'batch': 1}, metavar='<MLSRSI>',
help="[default: % (default)s] duration for the warmup."
"The warumup should be a batch level.")
if clr_cycle_length:
self.add_argument("--clr_cycle_length", type=parse_batch_epoch, default='batch:2000', metavar='<CLRCL>',
help="[default: %(default)s] cycle length in a cyclical learning rates training."
"It can be `batch:int_batches` or `epoch:float_epochs`.")
if clr_base_lr:
self.add_argument("--clr_base_lr", type=float, default=0.001, metavar='<CLRBLR>',
help="[default: %(default)s] minimum and initial learning rate in cyclical"
"learning rates training.")
if clr_max_lr:
self.add_argument("--clr_max_lr", type=float, default=0.1, metavar='<CLRMLR>',
help="[default: %(default)s] maximum learning rate in cyclical"
"learning rates training. Note this maximum value might not be reached "
"depending on the chosen scaling mode.")
if clr_mode:
self.add_argument("--clr_mode", type=str, default='triangular', metavar='<CLRM>',
help="[default: %(default)s] scaling mode in cyclical learning rate schedule.")
if clr_gamma:
self.add_argument("--clr_gamma", type=float, default=0.99, metavar='<CLRG>',
help="[default: %(default)s] constant in 'exp_range' scaling function"
" in cyclical learning rate schedule.")
if clr_extra:
self.add_argument("--clr_extra", type=float, default=0.1, metavar='<CLRE>',
help="[default: %(default)s] Extra number of iterations of training for one cycle.")
if sgd_lr_alpha:
self.add_argument("--alpha", type=float, default=100,
help="[default: %(default)s] Constant is used to calculate the optimal learning rate for"
"SGD ( alpha / beta + t).")
if sgd_lr_beta:
self.add_argument("--beta", type=float, default=100,
help="[default: %(default)s] Constant is used to calculate the optimal learning rate for"
"SGD ( alpha / beta + t).")
if sgd_lr_gamma:
self.add_argument("--sgd_lr_gamma", type=float, default=100,
help="[default: %(default)s] Constant is used to calculate the optimal learning rate for"
"sparsified SGD ( gamma / (a + t) * lambda).")
def const(optimizer):
return LambdaLR(optimizer, lr_lambda=lambda x: 1.0)
def triangular_learning_rates(optimizer, base_lr, max_lr, cycle_length, scale_fn, extra, mode):
"""Linearly scale the learning rates.
If one cycle is applied with length smaller than the total number of iterations, then
use small learning rate for the remaining iterations.
:param optimizer: an optimizer whose learning rate is scheduled.
:type optimizer: torch.nn.optim.optimizer
:param base_lr: lower bound and initial lr in a cycle.
:type base_lr: float
:param max_lr: upper bound in a cycle
:type max_lr: float
:param cycle_length: length of a cycle in terms of batches.
:type cycle_length: int
:param scale_fn: custom scaling policy defined by a single argument lambda function, defaults to None
:type scale_fn: callable, optional
:returns: a learning rate scheduler
:rtype: LambdaLR
"""
step_size = cycle_length / 2
if mode == 'one_cycle':
def f(iterations):
if iterations <= cycle_length:
cycle = np.floor(1 + iterations / (2 * step_size))
x = np.abs(iterations / step_size - 2 * cycle + 1)
lr = base_lr + (max_lr - base_lr) * np.maximum(0, (1 - x)) * scale_fn(cycle, iterations)
else:
lr = base_lr * extra
return lr / base_lr
else:
def f(iterations):
cycle = np.floor(1 + iterations / (2 * step_size))
x = np.abs(iterations / step_size - 2 * cycle + 1)
lr = base_lr + (max_lr - base_lr) * np.maximum(0, (1 - x)) * scale_fn(cycle, iterations)
return lr / base_lr
# Use base_lr to overwrite the --lr
for group in optimizer.param_groups:
group['initial_lr'] = base_lr
optimizer.base_lrs = [base_lr for _ in optimizer.param_groups]
return LambdaLR(optimizer, lr_lambda=f)
def cyclical_learning_rates(options, optimizer):
"""
Since leslie2017cyclical_ mentioned that traingular, Welch, Hann windows produce equivalent results,
we only implement triangular learning rate policy, also known as **linear cycle**.
The original implementation of leslie2017cyclical_ can be found from `here <https://github.com/bckenstler/CLR>`_.
smith2017super_ uses one cycle with extra epochs.
"""
if options.lr_scheduler_level != 'batch':
raise ValueError("The scheduler should be updated at batch level. Got {}."
.format(options.lr_scheduler_level))
mode = options.clr_mode
gamma = options.clr_gamma
if mode in ['linear', 'triangular', 'one_cycle']:
def scale_fn(cycle, iterations):
return 1.
elif mode == 'triangular2':
def scale_fn(cycle, iterations):
return 1 / (2. ** (cycle - 1))
elif mode == 'exp_range':
def scale_fn(cycle, iterations):
return gamma ** iterations
else:
raise ValueError("Cycle mode {} not support.".format(mode))
_cycle_unit, _cycle_length = options.lr_scheduler_level, options.clr_cycle_length[options.lr_scheduler_level]
cycle_length = int(_cycle_length) if _cycle_unit == 'batch' \
else float(_cycle_length) * options.train_num_batches
return triangular_learning_rates(optimizer, options.clr_base_lr, options.clr_max_lr,
cycle_length=cycle_length, scale_fn=scale_fn,
extra=options.clr_extra,
mode=mode)
def multistep_learning_rates_with_warmup(options, optimizer):
"""Use multistep learning rate schedule with warmup.
In goyal2017accurate_, warmup is used in order to apply the ``Linear Scaling Rule``.
Starting from the ``base_lr``, lr gradually increases to ``base_lr * scaling_factor``.
Then use multiply the learning rate by ``gamma`` at specified milestones.
:param options: all configs
:type options: argparse.Namespace
:param optimizer: optimizer associated with the scheduler
:type optimizer: torch.nn.optim.optimizer
:returns: a learning rate scheduler
:rtype: LambdaLR
:raises: ValueError, ValueError, ValueError
"""
scaling_factor = options.world_size if options.warmup_linear_scaling else 1
if options.warmup_init_lr_nonscale:
lr = options.lr_per_sample * options.batch_size
else:
lr = options.lr
base_lr = lr * scaling_factor
warmup_durations = options.warmup_durations.get(options.lr_scheduler_level, 0)
milestones = options.multisteplr_milestones[options.lr_scheduler_level]
gamma = options.multisteplr_gamma
warmup = options.warmup
if options.warmup_init_lr_nonscale:
warmup_init_lr = lr
else:
warmup_init_lr = options.warmup_init_lr
if not list(milestones) == sorted(milestones):
raise ValueError('Milestones should be a list of increasing integers.'
'Got {}'.format(milestones))
if warmup_durations >= milestones[0]:
raise ValueError("The scaling phase should be earlier than the first milestone."
"Got {} and {}".format(warmup_durations, milestones[0]))
def f(durations):
if warmup and durations <= warmup_durations:
warmup_progress = durations / warmup_durations
lr = warmup_progress * base_lr + (1 - warmup_progress) * warmup_init_lr
else:
lr = base_lr * gamma ** bisect_right(milestones, durations)
return lr / base_lr
for group in optimizer.param_groups:
group['initial_lr'] = base_lr
optimizer.base_lrs = [base_lr for _ in optimizer.param_groups]
return LambdaLR(optimizer, lr_lambda=f)
def sgd_optimal_learning_rates(options, optimizer):
"""
Learning rate schedule for SGD (alpha / (t + beta))
:param options: all configs
:param optimizer: optimizer associated with the scheduler
"""
beta = options.beta
alpha = options.alpha
def f(iterations):
return beta / (beta + iterations)
for group in optimizer.param_groups:
group['initial_lr'] = alpha / beta
optimizer.base_lrs = [alpha / beta for _ in optimizer.param_groups]
return LambdaLR(optimizer, lr_lambda=f)
def sparsified_sgd_optimal_learning_rate(options, optimizer):
"""
Learning rate schedule for sparsifiedSGD (gamma / lambda * (t + a))
param options: all configs
param optimizer: optimizer associated with the scheduler
"""
# TODO get feature size from the config file
a = 2000 / options.sparse_grad_size
l2_coef = options.l2_coef
gamma = options.sgd_lr_gamma
def f(iterations):
return 1 / max(1, (a + iterations))
optimizer.base_lrs = [gamma / l2_coef for _ in optimizer.param_groups]
for group in optimizer.param_groups:
group['initial_lr'] = gamma / l2_coef
return LambdaLR(optimizer, lr_lambda=f)
def get_scheduler(options, optimizer):
if options.lr_scheduler == 'const':
return const(optimizer)
elif options.lr_scheduler == 'CLR':
return cyclical_learning_rates(options, optimizer)
elif options.lr_scheduler == 'MultiStepLRW':
return multistep_learning_rates_with_warmup(options, optimizer)
elif options.lr_scheduler == 'sgd_optimal':
return sgd_optimal_learning_rates(options, optimizer)
elif options.lr_scheduler == 'sparsified_sgd':
return sparsified_sgd_optimal_learning_rate(options, optimizer)
else:
raise NotImplementedError
if __name__ == '__main__':
pass