-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedules.py
59 lines (51 loc) · 1.74 KB
/
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
import os
import torch
os.environ["KERAS_BACKEND"] = "torch"
import keras
from keras import backend
from keras import ops
from keras.src.backend.torch.core import *
class LinearWarmup(keras.optimizers.schedules.LearningRateSchedule):
def __init__(
self,
data_object=None,
warmup_epochs=None,
decay_epochs=None,
starting_lr=0.001,
warmup_lr=0.1,
final_lr=0.0001,
warmup_steps=2000,
decay_steps=10000
):
self.starting_lr = starting_lr
self.warmup_lr = warmup_lr
self.final_lr = final_lr
self.warmup_steps = warmup_steps
self.decay_steps = decay_steps
self.last_step = 0
self.name = "LinearWarmup"
self.a1 = (self.warmup_lr-self.starting_lr)/self.warmup_steps
self.b1 = self.starting_lr
self.a2 = (self.final_lr-self.warmup_lr)/self.decay_steps
self.b2 = self.final_lr-self.a2*(self.decay_steps+self.warmup_steps)
def get_lr(self, step):
if step<self.warmup_steps:
return self.a1*step+self.b1
elif step<=self.warmup_steps+self.decay_steps:
return self.a2*step+self.b2
else:
return self.final_lr
def __call__(self, step):
self.last_step = step
return self.get_lr(step)
def get_config(self):
return {
"starting_lr": self.starting_lr,
"warmup_lr": self.warmup_lr,
"final_lr": self.final_lr,
"warmup_steps": self.warmup_steps,
"decay_steps": self.decay_steps,
"last_lr": self.get_lr(self.last_step),
"last_step": self.last_step,
"name": self.name,
}