-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning_rate.py
More file actions
299 lines (264 loc) · 12.2 KB
/
learning_rate.py
File metadata and controls
299 lines (264 loc) · 12.2 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
import math
import numpy as np
class Inverse_Square_Root_Learning_rate():
def __init__(self, learning_rate,
warmup_steps):
self.learning_rate = learning_rate
self.warmup_steps = warmup_steps
self.iteration = 0
def __call__(self):
self.iteration += 1
return self.learning_rate * (1 / math.sqrt(max(self.iteration,
self.warmup_steps)))
def plot(self, iterations = 100):
import matplotlib.pyplot as plt
lr = []
for i in range(iterations):
lr.append(self.__call__())
plt.plot(lr)
plt.savefig('inverse-learning-rate.png')
plt.close()
class Polynomial_Decay_Learning_Rate():
def __init__(self, learning_rate,
max_epochs,
min_learning_rate = 1e-4,
power = 0.5):
self.learning_rate = learning_rate
self.max_epochs = max_epochs
self.min_learning_rate = min_learning_rate
self.power = power
self.iteration = -1
def __call__(self):
self.iteration += 1
return (self.learning_rate - self.min_learning_rate) * (1 - (self.iteration / self.max_epochs)) ** self.power + self.min_learning_rate
def plot(self, iterations = 100):
import matplotlib.pyplot as plt
lr = []
for i in range(iterations):
lr.append(self.__call__())
plt.plot(lr)
plt.savefig('polynomial-decay-learning-rate.png')
plt.close()
class Cosine_Learning_Rate():
def __init__(self, learning_rate,
decay_steps,
min_learning_rate,
alpha = 0.0):
self.learning_rate = learning_rate
self.decay_steps = decay_steps
self.min_learning_rate = min_learning_rate
self.alpha = alpha
self.iteration = 0
def __call__(self):
global_step = min(self.iteration,
self.decay_steps)
cosine_decay = 0.5 * (1 + np.cos(np.pi * global_step / self.decay_steps))
decayed = (1 - self.alpha) * cosine_decay + self.alpha
self.iteration += 1
return (self.learning_rate - self.min_learning_rate) * decayed + self.min_learning_rate
def plot(self, iterations = 100):
import matplotlib.pyplot as plt
lr = []
for i in range(iterations):
lr.append(self.__call__())
plt.plot(lr)
plt.savefig('cosine-learning-rate.png')
plt.close()
class Cyclic_Learning_Rate():
def __init__(self, learning_rate=0.01,
max_lr=0.1,
step_size=20.,
gamma=0.999,
mode='exp_range',
warmup_steps = 100,
stable_steps = 100):
# code is based heavily on https://github.com/mhmoodlan/cyclic-learning-rate
# cyclic learning rate was introduced in https://arxiv.org/pdf/1506.01186.pdf
self.learning_rate = learning_rate
# the learning rate of the model. Will obviously be altered in real-time by the algorithm
self.max_lr = max_lr
# the maximum possible learning-rate
self.step_size = step_size
# the step_size for the cycling. step_size per half-cycle. Recommended 2-8 cycles per epoch
self.gamma = gamma
# decay hyperparameter for exp
self.mode = mode
# the mode used for analysis
# 'triangular', 'triangular2', 'exp_range'
self.warmup_steps = warmup_steps
# the number of linear warmup steps, as defined in https://arxiv.org/pdf/1706.03762.pdf
self.stable_steps = stable_steps
# the number of stable steps of the learning rate
self.cyclic_step = 0
if self.warmup_steps != 0:
self.cyclic_step = -1 * self.warmup_steps
else:
self.cyclic_step = 0
def __call__(self):
self.cyclic_step += 1
cyclic_step = self.cyclic_step
if cyclic_step < self.warmup_steps:
cyclic_step -= self.warmup_steps
if cyclic_step <= 0:
return (self.max_lr / self.warmup_steps) * (self.warmup_steps + cyclic_step)
elif(cyclic_step - (self.warmup_steps + self.stable_steps) < 0):
return self.max_lr
cyclic_step -= (self.warmup_steps + self.stable_steps)
# the step is increased each __call__
# because class is not private, the step can be changed from outside the class if need be
cycle = math.floor(1 + cyclic_step / (2 * self.step_size))
x = abs(1 + (cyclic_step / self.step_size) - 2 * cycle)
clr = max(0.0, 1 - x) * (self.max_lr - self.learning_rate)
if self.mode == 'triangular2':
clr = clr / (2 ** (cycle - 1))
if self.mode == 'exp_range':
clr = clr * (self.gamma ** cyclic_step)
return clr + self.learning_rate
def plot(self, iterations = 100):
import matplotlib.pyplot as plt
lr = []
for i in range(iterations):
lr.append(self.__call__())
plt.plot(lr)
plt.savefig('cyclic-learning-rate-{}.png'.format(self.mode))
plt.close()
class Linear_Learning_Rate():
def __init__(self, learning_rate,
final_learning_rate,
decay_steps,
warmup_steps = 0,
stable_steps = 0):
self.learning_rate = learning_rate
self.final_learning_rate = final_learning_rate
self.decay_steps = decay_steps
self.warmup_steps = warmup_steps
self.stable_steps = stable_steps
self.steps = 0
def __call__(self):
self.steps += 1
if self.steps <= self.warmup_steps:
return (self.learning_rate * self.steps) / (self.warmup_steps)
elif self.steps <= self.warmup_steps + self.stable_steps:
return self.learning_rate
elif self.steps <= (self.warmup_steps + self.stable_steps + self.decay_steps):
steps = self.decay_steps - self.steps + self.warmup_steps + self.stable_steps
return self.final_learning_rate + ((self.learning_rate - self.final_learning_rate) * steps) / (self.decay_steps)
else:
return self.final_learning_rate
def plot(self, iterations = 100):
import matplotlib.pyplot as plt
lr = []
for i in range(iterations):
lr.append(self.__call__())
plt.plot(lr)
plt.savefig('linear-learning-rate.png')
plt.close()
class Learning_Rate():
def __init__(self, initial_learning_rate,
stable_learning_rate,
final_learning_rate,
warmup_rate,
decay_rate,
warmup_steps,
decay_steps,
step_size = 1000):
self.initial_learning_rate = initial_learning_rate
# the initial learning rate, at the start of the warmup steps
# if the warmup steps is set to 0 or warmup_rate == 'constant', initial_learning_rate == stable_learning_rate
self.stable_learning_rate = stable_learning_rate
# the learning rate at the end of warmup steps, and the beginning of the decay steps
self.final_learning_rate = final_learning_rate
# the final learning rate
# if decay_rate == 'constant', stable_learning_rate == final_learning_rate
self.warmup_rate = warmup_rate
# warmup refers to where the learning rate inceases over iterations
# 'constant', 'linear-warmup'
self.decay_rate = decay_rate
# learning rate decreases over iterations
# 'constant' 'linear-decay' 'polynomial' 'cosine' 'cyclic-triangular' 'cyclic-triangular2' 'cyclic-exp'
self.warmup_steps = warmup_steps
self.decay_steps = decay_steps
self.step_size = step_size
# only used for cyclic learning rates
self.iteration = 0
if self.warmup_rate == 'constant':
self.warmup_learning_rate = self.initial_learning_rate
assert self.initial_learning_rate == self.stable_learning_rate
# if the learning rate is constant, then warmup_learning_rate is set to a float, not a class from learning_rate.py
elif self.warmup_rate == 'linear-warmup':
self.warmup_learning_rate = Linear_Learning_Rate(learning_rate = self.stable_learning_rate - self.initial_learning_rate,
final_learning_rate = 0,
decay_steps = 0,
warmup_steps = self.warmup_steps,
stable_steps = 0)
if self.decay_rate == 'constant':
self.decay_learning_rate = self.stable_learning_rate
assert self.stable_learning_rate == self.final_learning_rate
elif self.decay_rate == 'linear-decay':
self.decay_learning_rate = Linear_Learning_Rate(learning_rate = self.stable_learning_rate,
final_learning_rate = self.final_learning_rate,
decay_steps = self.decay_steps,
warmup_steps = 0,
stable_steps = 0)
elif self.decay_rate == 'polynomial':
self.decay_learning_rate = Polynomial_Decay_Learning_Rate(learning_rate = self.stable_learning_rate,
max_epochs = self.decay_steps,
min_learning_rate = self.final_learning_rate)
elif self.decay_rate == 'cosine':
self.decay_learning_rate = Cosine_Learning_Rate(learning_rate = self.stable_learning_rate,
decay_steps = self.decay_steps,
min_learning_rate = self.final_learning_rate)
else:
if self.decay_rate == 'cyclic-triangular':
mode = 'triangular'
elif self.decay_rate == 'cyclic-triangular2':
mode = 'triangular2'
elif self.decay_rate == 'cyclic-exp':
mode = 'exp_range'
self.decay_learning_rate = Cyclic_Learning_Rate(learning_rate = self.stable_learning_rate,
step_size = self.step_size,
mode = mode,
warmup_steps = 0,
stable_steps = 100)
def call(self):
self.iteration += 1
if self.iteration < self.warmup_steps:
if self.warmup_rate == 'constant':
return self.warmup_learning_rate
else:
return self.warmup_learning_rate()
else:
if self.decay_rate == 'constant':
return self.decay_learning_rate
else:
return self.decay_learning_rate()
if __name__ == '__main__':
learning_rate = Inverse_Square_Root_Learning_rate(learning_rate = 1.0,
warmup_steps = 1e4)
learning_rate.plot(iterations = int(2.5e4))
exit()
learning_rate = Cosine_Learning_Rate(learning_rate = 1e-2,
decay_steps = 100,
min_learning_rate = 1e-5)
learning_rate.plot(iterations = 100)
learning_rate = Polynomial_Decay_Learning_Rate(learning_rate = 1e-2,
max_epochs = 100)
learning_rate.plot(iterations = 100)
learning_rate = Linear_Learning_Rate(1e-2,
1e-4,
25,
25,
25)
learning_rate.plot(iterations = 100)
learning_rate = Cyclic_Learning_Rate(warmup_steps = 0,
stable_steps = 0,
mode='exp_range')
learning_rate.plot(iterations = 1000)
learning_rate = Cyclic_Learning_Rate(warmup_steps = 0,
stable_steps = 0,
mode='triangular2')
learning_rate.plot(iterations = 1000)
learning_rate = Cyclic_Learning_Rate(warmup_steps = 0,
stable_steps = 0,
mode = 'triangular')
learning_rate.plot(iterations = 1000)