-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
173 lines (148 loc) · 5.45 KB
/
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
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
import os
import math
import torch
import pickle
import random
import json
import yaml
import numpy as np
import re
import operator
import functools
from torch.optim.lr_scheduler import _LRScheduler
class CosineAnnealingWarmUpRestarts(_LRScheduler):
def __init__(
self, optimizer, T_0, T_mult=1, eta_max=0.1, T_up=0, gamma=1.0, last_epoch=-1
):
if T_0 <= 0 or not isinstance(T_0, int):
raise ValueError("Expected positive integer T_0, but got {}".format(T_0))
if T_mult < 1 or not isinstance(T_mult, int):
raise ValueError("Expected integer T_mult >= 1, but got {}".format(T_mult))
if T_up < 0 or not isinstance(T_up, int):
raise ValueError("Expected positive integer T_up, but got {}".format(T_up))
self.T_0 = T_0
self.T_mult = T_mult
self.base_eta_max = eta_max
self.eta_max = eta_max
self.T_up = T_up
self.T_i = T_0
self.gamma = gamma
self.cycle = 0
self.T_cur = last_epoch
super(CosineAnnealingWarmUpRestarts, self).__init__(optimizer, last_epoch)
def get_lr(self):
if self.T_cur == -1:
return self.base_lrs
elif self.T_cur < self.T_up:
return [
(self.eta_max - base_lr) * self.T_cur / self.T_up + base_lr
for base_lr in self.base_lrs
]
else:
return [
base_lr
+ (self.eta_max - base_lr)
* (
1
+ math.cos(
math.pi * (self.T_cur - self.T_up) / (self.T_i - self.T_up)
)
)
/ 2
for base_lr in self.base_lrs
]
def step(self, epoch=None):
if epoch is None:
epoch = self.last_epoch + 1
self.T_cur = self.T_cur + 1
if self.T_cur >= self.T_i:
self.cycle += 1
self.T_cur = self.T_cur - self.T_i
self.T_i = (self.T_i - self.T_up) * self.T_mult + self.T_up
else:
if epoch >= self.T_0:
if self.T_mult == 1:
self.T_cur = epoch % self.T_0
self.cycle = epoch // self.T_0
else:
n = int(
math.log(
(epoch / self.T_0 * (self.T_mult - 1) + 1), self.T_mult
)
)
self.cycle = n
self.T_cur = epoch - self.T_0 * (self.T_mult**n - 1) / (
self.T_mult - 1
)
self.T_i = self.T_0 * self.T_mult ** (n)
else:
self.T_i = self.T_0
self.T_cur = epoch
self.eta_max = self.base_eta_max * (self.gamma**self.cycle)
self.last_epoch = math.floor(epoch)
for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
param_group["lr"] = lr
def load_files(path):
if path.rsplit(".", 2)[-1] == "json":
with open(path, "r") as f:
data = json.load(f)
elif path.rsplit(".", 2)[-1] in ["pkl", "pickle"]:
with open(path, "rb") as f:
data = pickle.load(f)
elif path.rsplit(".", 2)[-1] == "yaml":
with open(path, "r") as f:
try:
data = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)
elif path.rsplit(".", 2)[-1] == "npz":
data = np.load(path)
return data
def save_pickle(data, path):
with open(path, "wb") as f:
pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
def save_json(data, path):
with open(path, "w") as f:
json.dump(data, f)
def clean_str(string, lower=True):
string = re.sub(r"[^A-Za-z0-9,!\']", " ", string)
string = re.sub(r"\'s", " 's", string)
string = re.sub(r"\'ve", " 've", string)
string = re.sub(r"n\'t", " n't", string)
string = re.sub(r"\'re", " 're", string)
string = re.sub(r"\'d", " 'd", string)
string = re.sub(r"\'m", " 'm", string)
string = re.sub(r"\'ll", " 'll", string)
string = re.sub(r",", " , ", string)
return string.strip().lower() if lower else string.strip()
def fix_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
os.environ["PYTHONHASHSEED"] = str(seed)
def print_model(model, logger=None):
total_params = sum(p.numel() for p in model.parameters())
nParams = 0
for w in model.parameters():
nParams += functools.reduce(operator.mul, w.size(), 1)
if logger is not None:
logger.info(model)
logger.info("nParams=\t" + str(nParams))
else:
print(model)
print("nParams=\t" + str(nParams))
def make_one_hot(labels, num_labels):
onehot = torch.eye(num_labels)
out = onehot[labels.long()]
return out.type(torch.cuda.FloatTensor)
def glorot(tensor):
if tensor is not None:
stdv = math.sqrt(6.0 / (tensor.size(-2) + tensor.size(-1)))
tensor.data.uniform_(-stdv, stdv)
def zeros(tensor):
if tensor is not None:
tensor.data.fill_(0)