-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmisc_utils.py
50 lines (36 loc) · 1.12 KB
/
misc_utils.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
import math
import time
import argparse
def as_minutes(s):
m = math.floor(s / 60)
s -= m * 60
return "%dm %ds" % (m, s)
def time_since(since, percent):
now = time.time()
s = now - since
es = s / (percent)
rs = es - s
return "%s (- %s)" % (as_minutes(s), as_minutes(rs))
def linear_decay(epoch, total_num_epochs, initial_value, final_value):
return initial_value - (initial_value - final_value) * epoch / float(
total_num_epochs
)
def exponential_decay(epoch, rate, initial_value, final_value):
return max(initial_value * (rate ** epoch), final_value)
def set_optimizer_lr(optimizer, lr):
for param_group in optimizer.param_groups:
param_group["lr"] = lr
class StringEnum(tuple):
def __getattr__(self, attr):
if attr in self:
return attr
def str2bool(v):
"""
Argument Parse helper function for boolean values
"""
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")