-
Notifications
You must be signed in to change notification settings - Fork 30
/
lr.py
42 lines (33 loc) · 960 Bytes
/
lr.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
#!/usr/bin/env python
"""
lr.py
learning rate scheduler
"""
import numpy as np
class LRSchedule(object):
@staticmethod
def set_lr(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
@staticmethod
def constant(x, lr_init=0.1, epochs=1):
return lr_init
@staticmethod
def step(x, breaks=(150, 250)):
if x < breaks[0]:
return 0.1
elif x < breaks[1]:
return 0.01
else:
return 0.001
@staticmethod
def linear(x, lr_init=0.1, epochs=1):
return lr_init * float(epochs - x) / epochs
@staticmethod
def cyclical(x, lr_init=0.1, epochs=1):
""" Cyclical learning rate w/ annealing """
if x < 1:
# Start w/ small learning rate
return 0.05
else:
return lr_init * (1 - x % 1) * (epochs - np.floor(x)) / epochs