-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
132 lines (106 loc) · 3.64 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
import logging
import os
import sys
import torch
import numpy as np
import argparse
import re
def word_tokenize(sent):
pat = re.compile(r'[\w]+|[.,!?;|]')
if isinstance(sent, str):
return pat.findall(sent.lower())
else:
return []
def str2bool(v):
if isinstance(v, bool):
return v
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.")
def init_hvd_cuda(enable_hvd=True, enable_gpu=True):
hvd = None
if enable_hvd:
import horovod.torch as hvd
hvd.init()
logging.info(
f"hvd_size:{hvd.size()}, hvd_rank:{hvd.rank()}, hvd_local_rank:{hvd.local_rank()}"
)
hvd_size = hvd.size() if enable_hvd else 1
hvd_rank = hvd.rank() if enable_hvd else 0
hvd_local_rank = hvd.local_rank() if enable_hvd else 0
if enable_gpu:
torch.cuda.set_device(hvd_local_rank)
return hvd_size, hvd_rank, hvd_local_rank
def setuplogger():
root = logging.getLogger()
root.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
formatter = logging.Formatter("[%(levelname)s %(asctime)s] %(message)s")
handler.setFormatter(formatter)
root.addHandler(handler)
def dump_args(args):
for arg in dir(args):
if not arg.startswith("_"):
logging.info(f"args[{arg}]={getattr(args, arg)}")
def acc(y_true, y_hat):
y_hat = torch.argmax(y_hat, dim=-1)
tot = y_true.shape[0]
hit = torch.sum(y_true == y_hat)
return hit.data.float() * 1.0 / tot
def dcg_score(y_true, y_score, k=10):
order = np.argsort(y_score)[::-1]
y_true = np.take(y_true, order[:k])
gains = 2**y_true - 1
discounts = np.log2(np.arange(len(y_true)) + 2)
return np.sum(gains / discounts)
def ndcg_score(y_true, y_score, k=10):
best = dcg_score(y_true, y_true, k)
actual = dcg_score(y_true, y_score, k)
return actual / best
def mrr_score(y_true, y_score):
order = np.argsort(y_score)[::-1]
y_true = np.take(y_true, order)
rr_score = y_true / (np.arange(len(y_true)) + 1)
return np.sum(rr_score) / np.sum(y_true)
def load_matrix(embedding_file_path, word_dict, word_embedding_dim):
embedding_matrix = np.random.uniform(size=(len(word_dict) + 1,
word_embedding_dim))
have_word = []
if embedding_file_path is not None:
with open(embedding_file_path, 'rb') as f:
while True:
line = f.readline()
if len(line) == 0:
break
line = line.split()
word = line[0].decode()
if word in word_dict:
index = word_dict[word]
tp = [float(x) for x in line[1:]]
embedding_matrix[index] = np.array(tp)
have_word.append(word)
return embedding_matrix, have_word
def latest_checkpoint(directory):
if not os.path.exists(directory):
return None
print(os.listdir(directory))
if len(os.listdir(directory))==0:
return None
all_checkpoints = {
int(x.split('.')[-2].split('-')[-1]): x
for x in os.listdir(directory)
}
if not all_checkpoints:
return None
return os.path.join(directory,
all_checkpoints[max(all_checkpoints.keys())])
def get_checkpoint(directory, ckpt_name):
ckpt_path = os.path.join(directory, ckpt_name)
if os.path.exists(ckpt_path):
return ckpt_path
else:
return None