-
Notifications
You must be signed in to change notification settings - Fork 0
/
lr_schedules.py
298 lines (243 loc) · 6.76 KB
/
lr_schedules.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
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
import numpy as np
class LRSchedule():
""" Learning rate schedule parent class.
Attributes
----------
lr_initial : float
Initial, or base, learning rate.
lr : float
The latest learning rate.
step : int
Update step counter used for applying the learning rate schedule.
Methods
-------
__init__()
Constructor.
"""
def __init__(self, lr_initial, repr_str):
""" Constructor.
Parameters
----------
lr_initial : float
Initial, or base, learning rate.
Notes
-----
None
"""
self.lr_initial = lr_initial
self.lr = self.lr_initial
self.step = 0
self.repr_str = repr_str
def __repr__(self):
return self.repr_str
class LRConstantSchedule(LRSchedule):
""" Constant learning rate schedule.
Attributes
----------
lr_initial : float
Initial, or base, learning rate.
lr : float
The latest learning rate.
step : int
Update step counter used for applying the learning rate schedule.
Methods
-------
__init__()
Constuctor.
apply_schedule()
Applies the constant learning rate schedule.
get_lr()
Returns the latest learning rate.
"""
def __init__(self, lr_initial):
""" Constructor.
Inherits everything from the LRSchedule class
Parameters
----------
lr_initial : float
Initial, or base, learning rate.
Notes
-----
None
"""
repr_str = "constant lr schedule"
super().__init__(lr_initial, repr_str)
def apply_schedule(self, ):
""" Applies the constant learning rate schedule.
Parameters
----------
None
Returns
-------
None
Notes
-----
None
"""
pass
def get_lr(self, ):
""" Returns the latest learning rate.
Parameters
----------
None
Returns
-------
float
The latest learning rate.
Notes
-----
None
"""
return self.lr
class LRExponentialDecaySchedule(LRSchedule):
""" Exponential decay learning rate schedule.
Attributes
----------
lr_initial : float
Initial, or base, learning rate.
lr : float
The latest learning rate.
step : int
Update step counter used for applying the learning rate schedule.
decay_steps : int
The number of decay steps. The smaller, the faster the decay.
decay_rate : float
The rate of decay. The smaller, the faster the decay.? (weird, but looks like that)
Methods
-------
__init__()
Constuctor.
apply_schedule()
Applies the constant learning rate schedule.
get_lr()
Returns the latest learning rate.
"""
def __init__(self, lr_initial, decay_steps, decay_rate):
""" Constructor.
Inherits everything from the LRSchedule class
Parameters
----------
lr_initial : float
Initial, or base, learning rate.
decay_steps : int
The number of decay steps. The smaller, the faster the decay.
decay_rate : float
The rate of decay. The smaller, the faster the decay.? (weird, but looks like that)
Notes
-----
None
"""
repr_str = "exp. decay lr schedule"
super().__init__(lr_initial, repr_str)
self.decay_steps = decay_steps
self.decay_rate = decay_rate
def apply_schedule(self, ):
""" Applies the exponential decay learning rate schedule.
Parameters
----------
None
Returns
-------
None
Notes
-----
Based on: https://keras.io/api/optimizers/learning_rate_schedules/exponential_decay/
"""
self.lr = self.lr_initial * self.decay_rate ** (self.step / self.decay_steps)
self.step += 1
def get_lr(self, ):
""" Returns the latest learning rate.
Parameters
----------
None
Returns
-------
float
The latest learning rate.
Notes
-----
None
"""
return self.lr
class LRCyclingSchedule(LRSchedule):
""" Cyclical learning rate schedule.
Attributes
----------
lr_initial : float
Initial, or base, learning rate.
lr : float
The latest learning rate.
step : int
Update step counter used for applying the learning rate schedule.
lr_max : float
The maximum learning rate.
step_size : int
The step size in number of update steps.
A full cycle is 2 * step_size
Methods
-------
__init__()
Constuctor.
apply_schedule()
Applies the constant learning rate schedule.
get_lr()
Returns the latest learning rate.
Notes
-----
Based on: Cyclical Learning Rates for Training Neural Networks
Available at: https://arxiv.org/abs/1506.01186
The schedule starts at lr_initial, goes to lr_max in step_size update steps,
and then back to lr_initial in step_size update steps.
A full cycle is 2*step_size update steps.
"""
def __init__(self, lr_initial, lr_max, step_size):
""" Constructor.
Inherits everything from the LRSchedule class
Parameters
----------
lr_initial : float
Initial, or base, learning rate.
lr_max : float
The maximum learning rate.
step_size : int
The step size in number of update steps.
A full cycle is 2 * step_size
Notes
-----
None
"""
# self.lr_initial is lr_min, i.e.: the base lr
repr_str = "cycling lr schedule"
super().__init__(lr_initial, repr_str)
self.lr_max = lr_max
self.step_size = step_size
def apply_schedule(self, ):
""" Applies the cycling learning rate schedule.
Parameters
----------
None
Returns
-------
None
Notes
-----
Based on: https://www.datacamp.com/community/tutorials/cyclical-learning-neural-nets
"""
cycle = np.floor(1 + self.step / (2 * self.step_size))
x = np.abs(self.step / self.step_size - 2 * cycle + 1)
self.lr = self.lr_initial + (self.lr_max - self.lr_initial) * np.maximum(0, (1 - x))
self.step += 1
def get_lr(self, ):
""" Returns the latest learning rate.
Parameters
----------
None
Returns
-------
float
The latest learning rate.
Notes
-----
None
"""
return self.lr