Spaces:
Sleeping
Sleeping
ldm
Browse files- ldm/lr_scheduler.py +99 -0
ldm/lr_scheduler.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
|
4 |
+
class LambdaWarmUpCosineScheduler:
|
5 |
+
"""
|
6 |
+
note: use with a base_lr of 1.0
|
7 |
+
"""
|
8 |
+
def __init__(self, warm_up_steps, lr_min, lr_max, lr_start, max_decay_steps, verbosity_interval=0):
|
9 |
+
self.lr_warm_up_steps = warm_up_steps
|
10 |
+
self.lr_start = lr_start
|
11 |
+
self.lr_min = lr_min
|
12 |
+
self.lr_max = lr_max
|
13 |
+
self.lr_max_decay_steps = max_decay_steps
|
14 |
+
self.last_lr = 0.
|
15 |
+
self.verbosity_interval = verbosity_interval
|
16 |
+
|
17 |
+
def schedule(self, n, **kwargs):
|
18 |
+
if self.verbosity_interval > 0:
|
19 |
+
if n % self.verbosity_interval == 0: print(f"current step: {n}, recent lr-multiplier: {self.last_lr}")
|
20 |
+
if n < self.lr_warm_up_steps:
|
21 |
+
lr = (self.lr_max - self.lr_start) / self.lr_warm_up_steps * n + self.lr_start
|
22 |
+
self.last_lr = lr
|
23 |
+
return lr
|
24 |
+
else:
|
25 |
+
t = (n - self.lr_warm_up_steps) / (self.lr_max_decay_steps - self.lr_warm_up_steps)
|
26 |
+
t = min(t, 1.0)
|
27 |
+
lr = self.lr_min + 0.5 * (self.lr_max - self.lr_min) * (
|
28 |
+
1 + np.cos(t * np.pi))
|
29 |
+
self.last_lr = lr
|
30 |
+
return lr
|
31 |
+
|
32 |
+
def __call__(self, n, **kwargs):
|
33 |
+
return self.schedule(n,**kwargs)
|
34 |
+
|
35 |
+
|
36 |
+
class LambdaWarmUpCosineScheduler2:
|
37 |
+
"""
|
38 |
+
supports repeated iterations, configurable via lists
|
39 |
+
note: use with a base_lr of 1.0.
|
40 |
+
"""
|
41 |
+
def __init__(self, warm_up_steps, f_min, f_max, f_start, cycle_lengths, verbosity_interval=0):
|
42 |
+
assert len(warm_up_steps) == len(f_min) == len(f_max) == len(f_start) == len(cycle_lengths)
|
43 |
+
self.lr_warm_up_steps = warm_up_steps
|
44 |
+
self.lr_warm_up_steps =[1000]
|
45 |
+
self.f_start = f_start
|
46 |
+
self.f_min = f_min
|
47 |
+
self.f_max = f_max
|
48 |
+
self.cycle_lengths = cycle_lengths
|
49 |
+
self.cum_cycles = np.cumsum([0] + list(self.cycle_lengths))
|
50 |
+
self.last_f = 0.
|
51 |
+
self.verbosity_interval = verbosity_interval
|
52 |
+
|
53 |
+
def find_in_interval(self, n):
|
54 |
+
interval = 0
|
55 |
+
for cl in self.cum_cycles[1:]:
|
56 |
+
if n <= cl:
|
57 |
+
return interval
|
58 |
+
interval += 1
|
59 |
+
|
60 |
+
def schedule(self, n, **kwargs):
|
61 |
+
cycle = self.find_in_interval(n)
|
62 |
+
n = n - self.cum_cycles[cycle]
|
63 |
+
if self.verbosity_interval > 0:
|
64 |
+
if n % self.verbosity_interval == 0: print(f"current step: {n}, recent lr-multiplier: {self.last_f}, "
|
65 |
+
f"current cycle {cycle}")
|
66 |
+
if n < self.lr_warm_up_steps[cycle]:
|
67 |
+
f = (self.f_max[cycle] - self.f_start[cycle]) / self.lr_warm_up_steps[cycle] * n + self.f_start[cycle]
|
68 |
+
self.last_f = f
|
69 |
+
return f
|
70 |
+
else:
|
71 |
+
t = (n - self.lr_warm_up_steps[cycle]) / (self.cycle_lengths[cycle] - self.lr_warm_up_steps[cycle])
|
72 |
+
t = min(t, 1.0)
|
73 |
+
f = self.f_min[cycle] + 0.5 * (self.f_max[cycle] - self.f_min[cycle]) * (
|
74 |
+
1 + np.cos(t * np.pi))
|
75 |
+
self.last_f = f
|
76 |
+
return f
|
77 |
+
|
78 |
+
def __call__(self, n, **kwargs):
|
79 |
+
return self.schedule(n, **kwargs)
|
80 |
+
|
81 |
+
|
82 |
+
class LambdaLinearScheduler(LambdaWarmUpCosineScheduler2):
|
83 |
+
|
84 |
+
def schedule(self, n, **kwargs):
|
85 |
+
cycle = self.find_in_interval(n)
|
86 |
+
n = n - self.cum_cycles[cycle]
|
87 |
+
if self.verbosity_interval > 0:
|
88 |
+
if n % self.verbosity_interval == 0: print(f"current step: {n}, recent lr-multiplier: {self.last_f}, "
|
89 |
+
f"current cycle {cycle}")
|
90 |
+
|
91 |
+
if n < self.lr_warm_up_steps[cycle]:
|
92 |
+
f = (self.f_max[cycle] - self.f_start[cycle]) / self.lr_warm_up_steps[cycle] * n + self.f_start[cycle]
|
93 |
+
self.last_f = f
|
94 |
+
return f
|
95 |
+
else:
|
96 |
+
f = self.f_min[cycle] + (self.f_max[cycle] - self.f_min[cycle]) * (self.cycle_lengths[cycle] - n) / (self.cycle_lengths[cycle])
|
97 |
+
self.last_f = f
|
98 |
+
return f
|
99 |
+
|